2010-08-05 72 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

相关问题