繁体   English   中英

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

[英]How to use maven checkstyle plugin in multi-module project?

这是我在多模块项目中的父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文档..确保你的子模块中没有包含Maven Checkstyle插件...... ”。 但是,如果我的子模块继承我的root pom.xml ,我怎么能确保? 我迷路了,请帮忙。

但是,如果我的子模块继承我的root pom.xml,我怎么能确保?

要严格回答此问题,可以在<plugin>定义中指定<inherited>元素。 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> 

一些更多的建议/评论(可能不适用):

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

如果你为hibernate下载最新的项目布局,你会看到这个设计模式在运行。

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

编辑
在声明<parent>时使用<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

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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM