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

yeg*_*256 7 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>
…
Run Code Online (Sandbox Code Playgroud)

此配置指示在根项目每个子模块中mvn执行checkstyle插件.我不希望它以这种方式工作.相反,我希望这个插件只为root项目执行,并为每个子模块跳过.同时,我有很多子模块,我不喜欢在每个模块中明确地跳过插件执行的想法.

对于文档checkstyle " ..ensure你不包括的Maven Checkstyle的插件在您的子模块.. ".但是,如果我的子模块继承了我的root,我怎么能确保pom.xml?我迷路了,请帮忙.

Pas*_*ent 5

但是如何确保我的子模块继承了我的根 pom.xml?

要严格回答这个问题,您可以<inherited><plugin>定义中指定一个元素。从POM 参考

继承: 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> 
Run Code Online (Sandbox Code Playgroud)

还有一些建议/评论(可能不适用):


Ale*_*yak 2

也许您应该将根 pom 分成 2 个独立的实体:父 pom 和聚合器 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
Run Code Online (Sandbox Code Playgroud)

项目根/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>
Run Code Online (Sandbox Code Playgroud)

项目根/parent/pom.xml 片段

<groupId>org.hibernate</groupId>
<artifactId>hibernate-parent</artifactId>
<packaging>pom</packaging>
<version>3.5.4-Final</version>
Run Code Online (Sandbox Code Playgroud)

项目根/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>
Run Code Online (Sandbox Code Playgroud)