简体   繁体   English

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

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

This is my parent pom.xml (part of it) in a 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>
…

This configuration instructs mvn to execute checkstyle plugin in the root project and every sub-module. 此配置指示mvn在根项目每个子模块中执行checkstyle插件。 I don't want it to work this way. 我不希望它以这种方式工作。 Instead, I want this plugin to be executed only for the root project, and be skipped for every sub-module. 相反,我希望这个插件只为root项目执行,并为每个子模块跳过。 At the same time, I have many sub-modules, and I don't like the idea of explicitly skipping the plugin execution in every one of them. 同时,我有很多子模块,我不喜欢在每个子模块中明确地跳过插件执行的想法。

Documentation for checkstyle says " ..ensure that you do not include the Maven Checkstyle Plugin in your sub modules.. ". checkstyle文档..确保你的子模块中没有包含Maven Checkstyle插件...... ”。 But how can I ensure that if my sub-modules inherit my root pom.xml ? 但是,如果我的子模块继承我的root pom.xml ,我怎么能确保? I'm lost, please help. 我迷路了,请帮忙。

But how can I ensure that if my sub-modules inherit my root pom.xml? 但是,如果我的子模块继承我的root pom.xml,我怎么能确保?

To strictly answer this question, you can specify an <inherited> element inside a <plugin> definition. 要严格回答此问题,可以在<plugin>定义中指定<inherited>元素。 From the POM Reference : POM参考

inherited: true or false , whether or not this plugin configuration should apply to POMs which inherit from this one. 继承: truefalse ,此插件配置是否应该应用于从此继承的POM。

Something like this: 像这样的东西:

<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> 

Some more advices/remarks (that may not apply): 一些更多的建议/评论(可能不适用):

Perhaps you should separate your root pom into 2 separate entities: parent pom and aggregator pom. 也许您应该将根pom分成两个独立的实体:父pom和聚合器pom。 Your aggregator pom may even inherit from parent pom. 您的聚合器pom甚至可能从父pom继承。

If you download latest project layout for hibernate, you will see this design pattern in action. 如果你为hibernate下载最新的项目布局,你会看到这个设计模式在运行。

After this separation is done, you can define and execute checkstyle plugin just in aggregator/root pom. 完成此分离后,您可以在aggregator / root pom中定义和执行checkstyle插件。 Because it is no longer a parent of your submodules it will not get inherited by them. 因为它不再是子模块的父级,所以它们不会被它们继承。

EDIT 编辑
Use <relativePath> when declaring <parent> 在声明<parent>时使用<relativePath> <parent>

Just for demonstration, below is an example taken from the hibernate project structure. 仅用于演示,下面是从hibernate项目结构中获取的示例。
The whole distribution can be found here-> http://sourceforge.net/projects/hibernate/files/hibernate3 整个发行版可以在这里找到 - > http://sourceforge.net/projects/hibernate/files/hibernate3

Just so, that you have some context, here is a subset of their directory layout 就这样,你有一些上下文,这是他们的目录布局的一个子集

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

   .. rest is scipped for brevity

project-root/pom.xml fragment 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 fragment 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 fragment 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