如何在多模块项目中使用Maven Checkstyle插件?

发布于 2024-09-12 15:09:18 字数 1038 浏览 5 评论 0 原文

这是我在多模块项目中的父 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 的文档 "< em>..确保您的子模块中不包含 Maven Checkstyle 插件..”。但是我如何确保我的子模块继承我的根pom.xml?我迷路了,请帮忙。

This is my parent pom.xml (part of it) in a multi-module project:

...
<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. 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. 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..". But how can I ensure that if my sub-modules inherit my root pom.xml? I'm lost, please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

物价感观 2024-09-19 15:09:18

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

要严格回答这个问题,您可以在 元素> 定义。来自 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> 

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

But how can I ensure that if my sub-modules inherit my root pom.xml?

To strictly answer this question, you can specify an <inherited> element inside a <plugin> definition. From the POM Reference:

inherited: true or false, whether or not this plugin configuration should apply to POMs which inherit from this one.

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

娇俏 2024-09-19 15:09:18

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

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

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

编辑
声明 时使用

只是为了演示,下面是一个取自 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>

Perhaps you should separate your root pom into 2 separate entities: parent pom and aggregator pom. Your aggregator pom may even inherit from parent pom.

If you download latest project layout for hibernate, you will see this design pattern in action.

After this separation is done, you can define and execute checkstyle plugin just in aggregator/root pom. Because it is no longer a parent of your submodules it will not get inherited by them.

EDIT
Use <relativePath> when declaring <parent>

Just for demonstration, below is an example taken from the hibernate project structure.
The whole distribution can be found here-> 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

<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

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

project-root/core/pom.xml fragment

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文