如何在多模块Maven项目中的子模块中禁用特定的执行规则?

huangapple go评论76阅读模式
英文:

How to disable certain enforcer rules in a child module in a multi-module maven project?

问题

我正在一个多模块的Maven项目中使用Maven Enforcer插件。假设我的项目结构如下:

main
  - query
  - storage

我的main模块中的Enforcer插件配置如下:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-enforcer-plugin</artifactId>
            <version>3.0.0-M2</version>
            <executions>
                <execution>
                    <id>default</id>
                    <goals>
                        <goal>enforce</goal>
                    </goals>
                    <configuration>
                        <rules>
                            <DependencyConvergence/>
                            <requireJavaVersion>
                                <version>[1.8,)</version>
                                <message>*** This project requires JDK 1.8/J2SE 8 or later. ***</message>
                            </requireJavaVersion>
                        </rules>
                        <fail>true</fail>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

如果在子模块(例如query)中需要禁用其中一个Enforcer规则(比如DependencyConvergence),请问如何操作?

Maven版本 - 3.6.1

英文:

I am using a maven-enforcer plugin in a multi-module maven project. Let's say my project structure is like below

main
  - query
  - storage

My enforcer plugin in main pom looks like below

&lt;build&gt;
&lt;plugins&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                &lt;artifactId&gt;maven-enforcer-plugin&lt;/artifactId&gt;
                &lt;version&gt;3.0.0-M2&lt;/version&gt;
                &lt;executions&gt;
                    &lt;execution&gt;
                        &lt;id&gt;default&lt;/id&gt;
                        &lt;goals&gt;
                            &lt;goal&gt;enforce&lt;/goal&gt;
                        &lt;/goals&gt;
                        &lt;configuration&gt;
                            &lt;rules&gt;
                                &lt;DependencyConvergence/&gt;
                                &lt;requireJavaVersion&gt;
                                    &lt;version&gt;[1.8,)&lt;/version&gt;
                                    &lt;message&gt;*** This project requires JDK 1.8/J2SE 8 or later. ***&lt;/message&gt;
                                &lt;/requireJavaVersion&gt;
                            &lt;/rules&gt;
                            &lt;fail&gt;true&lt;/fail&gt;
                        &lt;/configuration&gt;
                    &lt;/execution&gt;
                &lt;/executions&gt;
            &lt;/plugin&gt;
&lt;/builds&gt;

In a child module (query) if I need to disable one of the enforcer rules (let's say DependencyConvergence) can someone let me know how can this be done?

Maven Version - 3.6.1

答案1

得分: 2

据我所知,您无法禁用单个执法规则。

您可以将enforcer.skip设置为true,这会禁用所有执法规则。

在类似情况下,我所做的是:

我定义了自己的执法规则,继承自“官方”执法规则。这个执法规则包含一个开关,用于禁用它。

英文:

AFAIK you cannot disable a single enforcer rule.

You can set enforcer.skip to true -- this disables all enforcer rules.

What I have done in a similar situation:

I have defined my own enforcer rule which inherited from an "official" enforcer rule. This enforcer rule contained a switch to disable it.

答案2

得分: 2

以下是您要翻译的内容:

这也在maven邮件列表中有答案。

> 如果所有配置都通过pluginMangement部分进行管理,可以像以下这样做;
>
> <build><pluginManagement><plugins>
> <plugin>
> <groupId>org.apache.maven.plugins</groupId>
> <artifactId>maven-enforcer-plugin</artifactId>
> <version>3.0.0-M2</version>
> <executions>
> <execution>
> <id>alpha</id>
> <phase></phase>
> <goals>
> <goal>enforce</goal>
> </goals>
> <configuration>
> <rules>
> <DependencyConvergence/>
> <requireJavaVersion>
> <version>[1.8,)</version>
> <message>*** 该项目需要
> JDK 1.8/J2SE 8或更高版本。 </message>
> </requireJavaVersion>
> </rules>
> <fail>true</fail>
> </configuration>
> </execution>
> <execution>
> <id>bravo</id>
> <phase></phase>
> <goals>
> <goal>enforce</goal>
> </goals>
> <configuration>
> <rules>
> <requireJavaVersion>
> <version>[1.8,)</version>
> <message>
该项目需要
> JDK 1.8/J2SE 8或更高版本。 ***</message>
> </requireJavaVersion>
> </rules>
> <fail>true</fail>
> </configuration>
> </execution>
> </executions>
> </plugin>
> <plugins>
> <plugin>
> <groupId>org.apache.maven.plugins</groupId>
> <artifactId>maven-enforcer-plugin</artifactId>
> <executions>
> <execution>
> <id>alpha</id>
> <phase>validate</phase>
> </execution>
> </executions>
> </plugin>
> </plugins></builds>
>
> query/pom.xml
>
> <build><plugins>
> <plugin>
> <groupId>org.apache.maven.plugins</groupId>
> <artifactId>maven-enforcer-plugin</artifactId>
> <executions>
> <execution>
> <id>alpha</id>
> <phase></phase>
> </execution>
> <execution>
> <id>bravo</id>
> <phase>validate</phase>
> </execution>
> </executions>
> </plugin>
> </executions>
> </plugin>
> </plugins></builds>
>
> 您也可以通过属性和在查询中定义bravo执行来实现。我曾经在maven-surefire-plugin中使用类似的技巧,其中我使用属性定义插件版本,并在根/父pom中设置默认值,在一个特定的子pom中定义不同的surefire版本。所以这可能有效...

> <build><pluginManagement><plugins>
> <plugin>
> <groupId>org.apache.maven.plugins</groupId>
> <artifactId>maven-enforcer-plugin</artifactId>
> <version>3.0.0-M2</version>
> <executions>
> <execution>
> <id>alpha</id>
> <phase></phase>
> <goals>
> <goal>enforce</goal>
> </goals>
> <configuration>
> <rules>
> <DependencyConvergence/>
> <requireJavaVersion>
> <version>[1.8,)</version>
> <message>*** 该项目需要
> JDK 1.8/J2SE 8或更高版本。 ***</message>
> </requireJavaVersion>
> </rules>
> <fail>true</fail>
> </configuration>
> </execution>
> <execution>
> <id>bravo</id>
> <phase></phase>
> <goals>
> <goal>enforce</goal>
> </goals>
> <configuration>
> <rules>
> <requireJavaVersion>
> <version>[1.8,)</version>
> <message>

英文:

This is also answered in the maven mailing list.

> So something like the following if all configuration is managed via a
> pluginMangement section;
>
> <build><pluginManagement><plugins>
> <plugin>
> <groupId>org.apache.maven.plugins</groupId>
> <artifactId>maven-enforcer-plugin</artifactId>
> <version>3.0.0-M2</version>
> <executions>
> <execution>
> <id>alpha</id>
> <phase></phase>
> <goals>
> <goal>enforce</goal>
> </goals>
> <configuration>
> <rules>
> <DependencyConvergence/>
> <requireJavaVersion>
> <version>[1.8,)</version>
> <message>*** This project requires
> JDK 1.8/J2SE 8 or later. </message>
> </requireJavaVersion>
> </rules>
> <fail>true</fail>
> </configuration>
> </execution>
> <execution>
> <id>bravo</id>
> <phase></phase>
> <goals>
> <goal>enforce</goal>
> </goals>
> <configuration>
> <rules>
> <requireJavaVersion>
> <version>[1.8,)</version>
> <message>
This project requires
> JDK 1.8/J2SE 8 or later. </message>
> </requireJavaVersion>
> </rules>
> <fail>true</fail>
> </configuration>
> </execution>
> </executions>
> </plugin>
> <plugins>
> <plugin>
> <groupId>org.apache.maven.plugins</groupId>
> <artifactId>maven-enforcer-plugin</artifactId>
> <executions>
> <execution>
> <id>alpha</id>
> <phase>validate</phase>
> </execution>
> </executions>
> </plugin>
> </plugins></builds>
>
> query/pom.xml
>
> <build><plugins>
> <plugin>
> <groupId>org.apache.maven.plugins</groupId>
> <artifactId>maven-enforcer-plugin</artifactId>
> <executions>
> <execution>
> <id>alpha</id>
> <phase></phase>
> </execution>
> <execution>
> <id>bravo</id>
> <phase>validate</phase>
> </execution>
> </executions>
> </plugin>
> </executions>
> </plugin>
> </plugins></builds>
>
> you might also be able to do it via a property and in query define the
> bravo execution instead of the alpha. I've used a similar technique
> with maven-surefire-plugin, where i define the plugin version using a
> property and have a default in the root/parent pom, and in one
> specific child pom i define a different surefire version. so this
> might work...
>
> <build><pluginManagement><plugins>
> <plugin>
> <groupId>org.apache.maven.plugins</groupId>
> <artifactId>maven-enforcer-plugin</artifactId>
> <version>3.0.0-M2</version>
> <executions>
> <execution>
> <id>alpha</id>
> <phase></phase>
> <goals>
> <goal>enforce</goal>
> </goals>
> <configuration>
> <rules>
> <DependencyConvergence/>
> <requireJavaVersion>
> <version>[1.8,)</version>
> <message>
This project requires
> JDK 1.8/J2SE 8 or later. </message>
> </requireJavaVersion>
> </rules>
> <fail>true</fail>
> </configuration>
> </execution>
> <execution>
> <id>bravo</id>
> <phase></phase>
> <goals>
> <goal>enforce</goal>
> </goals>
> <configuration>
> <rules>
> <requireJavaVersion>
> <version>[1.8,)</version>
> <message>
This project requires
> JDK 1.8/J2SE 8 or later. ***</message>
> </requireJavaVersion>
> </rules>
> <fail>true</fail>
> </configuration>
> </execution>
> </executions>
> </plugin>
> <plugins>
> <plugin>
> <groupId>org.apache.maven.plugins</groupId>
> <artifactId>maven-enforcer-plugin</artifactId>
> <executions>
> <execution>
> <id>${which-enforcer-id}</id>
> <phase>validate</phase>
> </execution>
> </executions>
> </plugin>
> </plugins></builds>
> <properties>
> <which-enforcer-id>alpha</which-enforcer-id>
> </properties>
>
> query/pom.xml
>
> <properties>
> <which-enforcer-id>bravo</which-enforcer-id>
> </properties>
>
> John

huangapple
  • 本文由 发表于 2020年8月12日 20:42:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63376766.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定