如何在多模块项目中使用maven checkstyle插件?

时间:2010-08-05 12:00:53

标签: java maven-2 checkstyle

这是我在多模块项目中的父pom.xml(部分内容):

...
<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插件。我不希望它以这种方式工作。相反,我希望这个插件只为root项目执行,并为每个子模块跳过。同时,我有很多子模块,我不喜欢在每个子模块中明确地跳过插件执行的想法。

checkstyle says 的文档..确保您不在子模块中包含Maven Checkstyle插件.. “。但是,如果我的子模块继承了我的根pom.xml,我怎么能确保?我迷路了,请帮帮忙。

2 个答案:

答案 0 :(得分:4)

  

但是,如果我的子模块继承了我的root 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> 

更多建议/备注(可能不适用):

答案 1 :(得分:2)

也许您应该将根pom分成两个独立的实体:父pom和聚合器pom。您的聚合器pom甚至可以从父pom继承。

如果您为hibernate下载最新的项目布局,您将看到此设计模式的实际效果。

完成此分离后,您可以在聚合器/根pom中定义和执行checkstyle插件。因为它不再是子模块的父级,所以它们不会被它们继承。

修改
在声明<relativePath>

时使用<parent>

仅用于演示,下面是一个来自hibernate项目结构的示例 整个分布可以在这里找到 - &gt; http://sourceforge.net/projects/hibernate/files/hibernate3

就是这样,你有一些上下文,这里是他们目录布局的一个子集

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

   .. rest is scipped for brevity

project-root / 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>

project-root / parent / pom.xml片段

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

project-root / core / 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>