行家执行者不遵守规则
maven enforcer does not honor rules
我正在使用 maven enforcer version 3.0.0-M3。
i configured the rules within the pom.xml
如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>foo</groupId>
<artifactId>foo</artifactId>
<version>0.0.1</version>
<properties>
<java.version>1.8</java.version>
<mvn.version>3.6.3</mvn.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.0.0-M3</version>
<executions>
<execution>
<id>enforce-versions</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<failFast>true</failFast>
<rules>
<requireMavenVersion>
<version>${mvn.version}</version>
</requireMavenVersion>
<requireJavaVersion>
<version>${java.version}</version>
</requireJavaVersion>
</rules>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
然后我验证配置评估是否正确:
$ mvn validate -Djava.version=1.7 -X
...
DEBUG] Configuring mojo org.apache.maven.plugins:maven-enforcer-plugin:3.0.0-M3:enforce from plugin realm ClassRealm[plugin>org.apache.maven.plugins:maven-enforcer-plugin:3.0.0-M3, parent: sun.misc.Launcher$AppClassLoader@7852e922]
[DEBUG] Configuring mojo 'org.apache.maven.plugins:maven-enforcer-plugin:3.0.0-M3:enforce' with basic configurator -->
[DEBUG] (f) commandLineRules = []
[DEBUG] (s) fail = true
[DEBUG] (s) failFast = true
[DEBUG] (f) ignoreCache = false
[DEBUG] (f) mojoExecution = org.apache.maven.plugins:maven-enforcer-plugin:3.0.0-M3:enforce {execution: enforce-versions}
[DEBUG] (s) project = MavenProject: foo:foo:0.0.1 @ /app/pom.xml
[DEBUG] (s) version = 3.6.3
[DEBUG] (s) version = 1.7
[DEBUG] (s) rules = [org.apache.maven.plugins.enforcer.RequireMavenVersion@551a20d6, org.apache.maven.plugins.enforcer.RequireJavaVersion@578524c3]
[DEBUG] (s) session = org.apache.maven.execution.MavenSession@64c2b546
[DEBUG] (s) skip = false
[DEBUG] -- end configuration --
[DEBUG] Executing rule: org.apache.maven.plugins.enforcer.RequireMavenVersion
[DEBUG] Rule org.apache.maven.plugins.enforcer.RequireMavenVersion is cacheable.
[DEBUG] Detected Maven Version: 3.6.3
[DEBUG] Detected Maven Version: 3.6.3 is allowed in the range 3.6.3.
[DEBUG] Executing rule: org.apache.maven.plugins.enforcer.RequireJavaVersion
[DEBUG] Rule org.apache.maven.plugins.enforcer.RequireJavaVersion is cacheable.
[DEBUG] Detected Java String: '1.7'
[DEBUG] Normalized Java String: '1.7'
[DEBUG] Parsed Version: Major: 1 Minor: 7 Incremental: 0 Build: 0 Qualifier: null
[DEBUG] Detected JDK Version: 1.7 is allowed in the range 1.7.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.848 s
[INFO] Finished at: 2020-08-02T08:47:43Z
[INFO] ------------------------------------------------------------------------
但似乎不遵守规则,因为当我 运行 和 enforce
时,会显示 No rules are configured.
错误消息
$ mvn enforcer:enforce -Djava.version=1.7
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------< foo:foo >-------------------------------
[INFO] Building foo 0.0.1
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-enforcer-plugin:3.0.0-M3:enforce (default-cli) @ foo ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.605 s
[INFO] Finished at: 2020-08-02T08:49:54Z
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-enforcer-plugin:3.0.0-M3:enforce (default-cli) on project foo: No rules are configured. Use the skip flag if you want to disable execution. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
当通过命令行参数提供规则时,执行者似乎确实对它们进行了评估
$ mvn enforcer:enforce -Drules=alwaysFail
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------< foo:foo >-------------------------------
[INFO] Building foo 0.0.1
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-enforcer-plugin:3.0.0-M3:enforce (default-cli) @ foo ---
[WARNING] Rule 0: org.apache.maven.plugins.enforcer.AlwaysFail failed with message:
Always fails!
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.572 s
[INFO] Finished at: 2020-08-02T09:05:57Z
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-enforcer-plugin:3.0.0-M3:enforce (default-cli) on project foo: Some Enforcer rules have failed. Look above for specific messages explaining why the rule failed. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
但是在 /project/build/plugins/plugin/executions/execution/configuration/rules/AlwaysFail
(xpath) 中设置 <AlwaysFail />
似乎不会产生相同的结果——在没有命令行参数的情况下执行时继续得到 No rules are configured
。
我在 debian buster 下使用 maven 3.6.3 和 openjdk 1.8
$ mvn --version
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /usr/share/maven
Java version: 1.8.0_262, vendor: Oracle Corporation, runtime: /usr/local/openjdk-8/jre
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "4.19.76-linuxkit", arch: "amd64", family: "unix"
$ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 10 (buster)
Release: 10
Codename: buster
更新:
将 <configuration>
块移出 <executions>
块并在调试模式下 运行ning 后,我得到显示检测到 java.version=0.7
的输出由执行者执行,但它不会使构建失败。见下文:
$ mvn validate -Drequired.java.version=0.7 -X
...
[DEBUG] Configuring mojo org.apache.maven.plugins:maven-enforcer-plugin:3.0.0-M3:enforce from plugin realm ClassRealm[plugin>org.apache.maven.plugins:maven-enforcer-plugin:3.0.0-M3, parent: sun.misc.Launcher$AppClassLoader@7852e922]
[DEBUG] Configuring mojo 'org.apache.maven.plugins:maven-enforcer-plugin:3.0.0-M3:enforce' with basic configurator -->
[DEBUG] (f) commandLineRules = []
[DEBUG] (s) fail = true
[DEBUG] (s) failFast = true
[DEBUG] (f) ignoreCache = false
[DEBUG] (f) mojoExecution = org.apache.maven.plugins:maven-enforcer-plugin:3.0.0-M3:enforce {execution: enforce-versions}
[DEBUG] (s) project = MavenProject: foo:foo:0.0.1 @ /app/pom.xml
[DEBUG] (s) version = 3.6.3
[DEBUG] (s) version = 0.7
[DEBUG] (s) rules = [org.apache.maven.plugins.enforcer.RequireMavenVersion@551a20d6, org.apache.maven.plugins.enforcer.RequireJavaVersion@578524c3]
[DEBUG] (s) session = org.apache.maven.execution.MavenSession@64c2b546
[DEBUG] (s) skip = false
[DEBUG] -- end configuration --
[DEBUG] Executing rule: org.apache.maven.plugins.enforcer.RequireMavenVersion
[DEBUG] Rule org.apache.maven.plugins.enforcer.RequireMavenVersion is cacheable.
[DEBUG] Detected Maven Version: 3.6.3
[DEBUG] Detected Maven Version: 3.6.3 is allowed in the range 3.6.3.
[DEBUG] Executing rule: org.apache.maven.plugins.enforcer.RequireJavaVersion
[DEBUG] Rule org.apache.maven.plugins.enforcer.RequireJavaVersion is cacheable.
[DEBUG] Detected Java String: '1.8.0_262'
[DEBUG] Normalized Java String: '1.8.0-262'
[DEBUG] Parsed Version: Major: 1 Minor: 8 Incremental: 0 Build: 262 Qualifier: null
[DEBUG] Detected JDK Version: 1.8.0-262 is allowed in the range 0.7.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.854 s
[INFO] Finished at: 2020-08-02T11:51:19Z
[INFO] ------------------------------------------------------------------------
如何在未满足要求的 java 和 maven 版本时使执行器失败?
您将规则配置为 <execution>
的一部分。
如果您希望配置也用于其他执行(如命令行),您需要将 <configuration>
块移到 <executions>
块之外。
它应该是这样的:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.0.0-M3</version>
<executions>
<execution>
<id>enforce-versions</id>
<goals>
<goal>enforce</goal>
</goals>
</execution>
</executions>
<configuration>
<failFast>true</failFast>
<rules>
<requireMavenVersion>
<version>${mvn.version}</version>
</requireMavenVersion>
<requireJavaVersion>
<version>${required.java.version}</version>
</requireJavaVersion>
</rules>
</configuration>
</plugin>
请注意,您不应使用 属性 ${java.version}
,因为它显然是 Maven 的“官方”属性。
通读文档后,特别是version range specification,当版本指定为数字时,表示版本比较运算符是>=
(大于或等于)版本.
为了解决这个问题,我使用了方括号 ([]
),例如
<requireMavenVersion>
<version>[${mvn.version}]</version>
</requireMavenVersion>
如前所述,使用 java.version
和 mvn.version
是内置属性。
我正在使用 maven enforcer version 3.0.0-M3。
i configured the rules within the pom.xml
如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>foo</groupId>
<artifactId>foo</artifactId>
<version>0.0.1</version>
<properties>
<java.version>1.8</java.version>
<mvn.version>3.6.3</mvn.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.0.0-M3</version>
<executions>
<execution>
<id>enforce-versions</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<failFast>true</failFast>
<rules>
<requireMavenVersion>
<version>${mvn.version}</version>
</requireMavenVersion>
<requireJavaVersion>
<version>${java.version}</version>
</requireJavaVersion>
</rules>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
然后我验证配置评估是否正确:
$ mvn validate -Djava.version=1.7 -X
...
DEBUG] Configuring mojo org.apache.maven.plugins:maven-enforcer-plugin:3.0.0-M3:enforce from plugin realm ClassRealm[plugin>org.apache.maven.plugins:maven-enforcer-plugin:3.0.0-M3, parent: sun.misc.Launcher$AppClassLoader@7852e922]
[DEBUG] Configuring mojo 'org.apache.maven.plugins:maven-enforcer-plugin:3.0.0-M3:enforce' with basic configurator -->
[DEBUG] (f) commandLineRules = []
[DEBUG] (s) fail = true
[DEBUG] (s) failFast = true
[DEBUG] (f) ignoreCache = false
[DEBUG] (f) mojoExecution = org.apache.maven.plugins:maven-enforcer-plugin:3.0.0-M3:enforce {execution: enforce-versions}
[DEBUG] (s) project = MavenProject: foo:foo:0.0.1 @ /app/pom.xml
[DEBUG] (s) version = 3.6.3
[DEBUG] (s) version = 1.7
[DEBUG] (s) rules = [org.apache.maven.plugins.enforcer.RequireMavenVersion@551a20d6, org.apache.maven.plugins.enforcer.RequireJavaVersion@578524c3]
[DEBUG] (s) session = org.apache.maven.execution.MavenSession@64c2b546
[DEBUG] (s) skip = false
[DEBUG] -- end configuration --
[DEBUG] Executing rule: org.apache.maven.plugins.enforcer.RequireMavenVersion
[DEBUG] Rule org.apache.maven.plugins.enforcer.RequireMavenVersion is cacheable.
[DEBUG] Detected Maven Version: 3.6.3
[DEBUG] Detected Maven Version: 3.6.3 is allowed in the range 3.6.3.
[DEBUG] Executing rule: org.apache.maven.plugins.enforcer.RequireJavaVersion
[DEBUG] Rule org.apache.maven.plugins.enforcer.RequireJavaVersion is cacheable.
[DEBUG] Detected Java String: '1.7'
[DEBUG] Normalized Java String: '1.7'
[DEBUG] Parsed Version: Major: 1 Minor: 7 Incremental: 0 Build: 0 Qualifier: null
[DEBUG] Detected JDK Version: 1.7 is allowed in the range 1.7.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.848 s
[INFO] Finished at: 2020-08-02T08:47:43Z
[INFO] ------------------------------------------------------------------------
但似乎不遵守规则,因为当我 运行 和 enforce
时,会显示 No rules are configured.
错误消息
$ mvn enforcer:enforce -Djava.version=1.7
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------< foo:foo >-------------------------------
[INFO] Building foo 0.0.1
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-enforcer-plugin:3.0.0-M3:enforce (default-cli) @ foo ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.605 s
[INFO] Finished at: 2020-08-02T08:49:54Z
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-enforcer-plugin:3.0.0-M3:enforce (default-cli) on project foo: No rules are configured. Use the skip flag if you want to disable execution. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
当通过命令行参数提供规则时,执行者似乎确实对它们进行了评估
$ mvn enforcer:enforce -Drules=alwaysFail
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------< foo:foo >-------------------------------
[INFO] Building foo 0.0.1
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-enforcer-plugin:3.0.0-M3:enforce (default-cli) @ foo ---
[WARNING] Rule 0: org.apache.maven.plugins.enforcer.AlwaysFail failed with message:
Always fails!
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.572 s
[INFO] Finished at: 2020-08-02T09:05:57Z
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-enforcer-plugin:3.0.0-M3:enforce (default-cli) on project foo: Some Enforcer rules have failed. Look above for specific messages explaining why the rule failed. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
但是在 /project/build/plugins/plugin/executions/execution/configuration/rules/AlwaysFail
(xpath) 中设置 <AlwaysFail />
似乎不会产生相同的结果——在没有命令行参数的情况下执行时继续得到 No rules are configured
。
我在 debian buster 下使用 maven 3.6.3 和 openjdk 1.8
$ mvn --version
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /usr/share/maven
Java version: 1.8.0_262, vendor: Oracle Corporation, runtime: /usr/local/openjdk-8/jre
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "4.19.76-linuxkit", arch: "amd64", family: "unix"
$ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 10 (buster)
Release: 10
Codename: buster
更新:
将 <configuration>
块移出 <executions>
块并在调试模式下 运行ning 后,我得到显示检测到 java.version=0.7
的输出由执行者执行,但它不会使构建失败。见下文:
$ mvn validate -Drequired.java.version=0.7 -X
...
[DEBUG] Configuring mojo org.apache.maven.plugins:maven-enforcer-plugin:3.0.0-M3:enforce from plugin realm ClassRealm[plugin>org.apache.maven.plugins:maven-enforcer-plugin:3.0.0-M3, parent: sun.misc.Launcher$AppClassLoader@7852e922]
[DEBUG] Configuring mojo 'org.apache.maven.plugins:maven-enforcer-plugin:3.0.0-M3:enforce' with basic configurator -->
[DEBUG] (f) commandLineRules = []
[DEBUG] (s) fail = true
[DEBUG] (s) failFast = true
[DEBUG] (f) ignoreCache = false
[DEBUG] (f) mojoExecution = org.apache.maven.plugins:maven-enforcer-plugin:3.0.0-M3:enforce {execution: enforce-versions}
[DEBUG] (s) project = MavenProject: foo:foo:0.0.1 @ /app/pom.xml
[DEBUG] (s) version = 3.6.3
[DEBUG] (s) version = 0.7
[DEBUG] (s) rules = [org.apache.maven.plugins.enforcer.RequireMavenVersion@551a20d6, org.apache.maven.plugins.enforcer.RequireJavaVersion@578524c3]
[DEBUG] (s) session = org.apache.maven.execution.MavenSession@64c2b546
[DEBUG] (s) skip = false
[DEBUG] -- end configuration --
[DEBUG] Executing rule: org.apache.maven.plugins.enforcer.RequireMavenVersion
[DEBUG] Rule org.apache.maven.plugins.enforcer.RequireMavenVersion is cacheable.
[DEBUG] Detected Maven Version: 3.6.3
[DEBUG] Detected Maven Version: 3.6.3 is allowed in the range 3.6.3.
[DEBUG] Executing rule: org.apache.maven.plugins.enforcer.RequireJavaVersion
[DEBUG] Rule org.apache.maven.plugins.enforcer.RequireJavaVersion is cacheable.
[DEBUG] Detected Java String: '1.8.0_262'
[DEBUG] Normalized Java String: '1.8.0-262'
[DEBUG] Parsed Version: Major: 1 Minor: 8 Incremental: 0 Build: 262 Qualifier: null
[DEBUG] Detected JDK Version: 1.8.0-262 is allowed in the range 0.7.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.854 s
[INFO] Finished at: 2020-08-02T11:51:19Z
[INFO] ------------------------------------------------------------------------
如何在未满足要求的 java 和 maven 版本时使执行器失败?
您将规则配置为 <execution>
的一部分。
如果您希望配置也用于其他执行(如命令行),您需要将 <configuration>
块移到 <executions>
块之外。
它应该是这样的:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.0.0-M3</version>
<executions>
<execution>
<id>enforce-versions</id>
<goals>
<goal>enforce</goal>
</goals>
</execution>
</executions>
<configuration>
<failFast>true</failFast>
<rules>
<requireMavenVersion>
<version>${mvn.version}</version>
</requireMavenVersion>
<requireJavaVersion>
<version>${required.java.version}</version>
</requireJavaVersion>
</rules>
</configuration>
</plugin>
请注意,您不应使用 属性 ${java.version}
,因为它显然是 Maven 的“官方”属性。
通读文档后,特别是version range specification,当版本指定为数字时,表示版本比较运算符是>=
(大于或等于)版本.
为了解决这个问题,我使用了方括号 ([]
),例如
<requireMavenVersion>
<version>[${mvn.version}]</version>
</requireMavenVersion>
如前所述,使用 java.version
和 mvn.version
是内置属性。