英文:
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
<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>
</builds>
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论