2010-08-05 71 views
5

這是我在一個多模塊項目父pom.xml(一部分):如何在多模塊項目中使用maven checkstyle插件?

... 
<build> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-checkstyle-plugin</artifactId> 
      <executions> 
       <execution> 
        <phase>compile</phase> 
        <goals> 
         <goal>check</goal> 
        </goals> 
       </execution> 
      </executions> 
     </plugin> 
    </plugins> 
</build> 
… 

該配置指示mvn根項目每個子模塊執行checkstyle插件。我不希望它以這種方式工作。相反,我希望此插件僅針對根項目執行,並且可以跳過每個子模塊。同時,我有很多子模塊,我不喜歡在每一個模塊中明確跳過插件執行的想法。

的文檔checkstylesays..ensure你不包括的Maven Checkstyle的插件在您的子模塊..」。但我怎麼能確保,如果我的子模塊繼承我的根pom.xml?我迷路了,請幫忙。

回答

2

也許你應該將你的root pom分成兩個獨立的實體:parent pom和aggregator pom。你的聚合器pom甚至可能繼承父pom。

如果您下載hibernate的最新項目佈局,您將看到這個設計模式正在運行。

完成分離後,您可以在aggregator/root pom中定義並執行checkstyle插件。因爲它不再是你的子模塊的父親,它不會被它們繼承。

編輯
使用<relativePath>聲明只是爲了演示<parent>

時,下面是從Hibernate項目結構採取的一個例子。
整個分佈可以發現這裏 - >http://sourceforge.net/projects/hibernate/files/hibernate3

正是如此,你有一些背景,這裏是他們的目錄佈局的一個子集

project-root 
    | 
    +-pom.xml 
    | 
    + parent 
    | | 
    | +-pom.xml 
    | 
    + core 
     | 
     +-pom.xml 

    .. rest is scipped for brevity 

項目根/ pom.xml的片段

<parent> 
    <groupId>org.hibernate</groupId> 
    <artifactId>hibernate-parent</artifactId> 
    <version>3.5.4-Final</version> 
    <relativePath>parent/pom.xml</relativePath> 
</parent> 

<groupId>org.hibernate</groupId> 
<artifactId>hibernate</artifactId> 
<packaging>pom</packaging> 

<name>Hibernate Core Aggregator</name> 
<description>Aggregator of the Hibernate Core modules.</description> 

<modules> 
    <module>parent</module> 
    <module>core</module> 

項目根/父/ pom.xml的片段

<groupId>org.hibernate</groupId> 
<artifactId>hibernate-parent</artifactId> 
<packaging>pom</packaging> 
<version>3.5.4-Final</version> 

項目根/核心/ pom.xml的片段

<parent> 
    <groupId>org.hibernate</groupId> 
    <artifactId>hibernate-parent</artifactId> 
    <version>3.5.4-Final</version> 
    <relativePath>../parent/pom.xml</relativePath> 
</parent> 

<groupId>org.hibernate</groupId> 
<artifactId>hibernate-core</artifactId> 
<packaging>jar</packaging> 
+0

謝謝,建議真的很好,但現在還有另一個問題。我的根/聚合器項目不能從「父項目」繼承,因爲它是_under_root,並且在第一個建設週期中不可用。任何想法? – yegor256 2010-08-05 12:26:44

+0

@ FaZend.com我已經添加了一些示例。 – 2010-08-05 13:50:46

+1

這不是必需的,你可以告訴maven不要繼承插件配置。 – 2010-08-06 15:42:45

4

但我怎麼能保證,如果我的子模塊繼承我的根pom.xml的?

要嚴格回答這個問題,您可以在<plugin>定義中指定<inherited>元素。從POM Reference

繼承truefalse,這個插件的配置是否不應適用於從這一個繼承的POM。

事情是這樣的:

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-checkstyle-plugin</artifactId> 
    <!-- Lock down plugin version for build reproducibility --> 
    <version>2.5</version> 
    <inherited>true</inherited> 
    <configuration> 
    ... 
    </configuration> 
</plugin> 

一些更多的意見/評論(可能不適用):

+0

帕斯卡爾,非常感謝您的建議,他們非常有幫助(我已經在我的項目中使用它們)! – yegor256 2010-08-06 15:38:03

+0

@ FaZend.com好吧,不客氣。 – 2010-08-06 15:42:18

相關問題