Maven 强制执行器不遵守规则。

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

maven enforcer does not honor rules

问题

i am using [maven enforcer](https://maven.apache.org/enforcer/maven-enforcer-plugin) version [3.0.0-M3](https://github.com/apache/maven-enforcer/tree/enforcer-3.0.0-M3).

i [configured the rules within the `pom.xml`](https://maven.apache.org/enforcer/maven-enforcer-plugin/usage.html) as follows:

```xml
<!-- Contents of your XML configuration -->

then i verified the configurations are evaluated correctly:

$ mvn validate -Djava.version=1.7 -X
...
<!-- Debug output indicating rule evaluation -->

but it seems the rules are not honored, since when i run the enforce, the No rules are configured. error message is shown

$ mvn enforcer:enforce -Djava.version=1.7
[INFO] Scanning for projects...
...
[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]
...

when providing rules through command line argument, it seems the enforcer does evaluate them

$ mvn enforcer:enforce -Drules=alwaysFail
[INFO] Scanning for projects...
...
[WARNING] Rule 0: org.apache.maven.plugins.enforcer.AlwaysFail failed with message:
Always fails!
...

but setting <AlwaysFail /> in /project/build/plugins/plugin/executions/execution/configuration/rules/AlwaysFail (xpath) doesn't seem to produce the same thing -- keep getting No rules are configured when executed without a command line arguments.

i am using maven 3.6.3 and openjdk 1.8 under debian buster

$ mvn --version
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
...
Java version: 1.8.0_262, vendor: Oracle Corporation, runtime: /usr/local/openjdk-8/jre
...

$ lsb_release -a
Distributor ID:	Debian
...

UPDATE:

after moving the <configuration> block outside the <executions> block and running in a debug mode, i get the output where it shows that the java.version=0.7 is detected by by the enforcer but it does not fail the build. see below:

$ mvn validate -Drequired.java.version=0.7 -X
...
<!-- Debug output indicating rule evaluation -->
...
[INFO] BUILD SUCCESS
...

how to make the enforcer fail when the required java and maven version are unmet?


<details>
<summary>英文:</summary>

i am using [maven enforcer](https://maven.apache.org/enforcer/maven-enforcer-plugin) version [3.0.0-M3](https://github.com/apache/maven-enforcer/tree/enforcer-3.0.0-M3).

i [configured the rules within the `pom.xml`](https://maven.apache.org/enforcer/maven-enforcer-plugin/usage.html) as follows:

```xml
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  &lt;groupId&gt;foo&lt;/groupId&gt;
  &lt;artifactId&gt;foo&lt;/artifactId&gt;
  &lt;version&gt;0.0.1&lt;/version&gt;
  &lt;properties&gt;
    &lt;java.version&gt;1.8&lt;/java.version&gt;
    &lt;mvn.version&gt;3.6.3&lt;/mvn.version&gt;
  &lt;/properties&gt;
  &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-M3&lt;/version&gt;
        &lt;executions&gt;
          &lt;execution&gt;
            &lt;id&gt;enforce-versions&lt;/id&gt;
            &lt;goals&gt;
              &lt;goal&gt;enforce&lt;/goal&gt;
            &lt;/goals&gt;
            &lt;configuration&gt;
              &lt;failFast&gt;true&lt;/failFast&gt;
              &lt;rules&gt;
                &lt;requireMavenVersion&gt;
                  &lt;version&gt;${mvn.version}&lt;/version&gt;
                &lt;/requireMavenVersion&gt;
                &lt;requireJavaVersion&gt;
                  &lt;version&gt;${java.version}&lt;/version&gt;
                &lt;/requireJavaVersion&gt;
              &lt;/rules&gt;
            &lt;/configuration&gt;
          &lt;/execution&gt;
        &lt;/executions&gt;
      &lt;/plugin&gt;
    &lt;/plugins&gt;
  &lt;/build&gt;
&lt;/project&gt;

then i verified the configurations are evaluated correctly:

$ 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&gt;org.apache.maven.plugins:maven-enforcer-plugin:3.0.0-M3, parent: sun.misc.Launcher$AppClassLoader@7852e922]
[DEBUG] Configuring mojo &#39;org.apache.maven.plugins:maven-enforcer-plugin:3.0.0-M3:enforce&#39; with basic configurator --&gt;
[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: &#39;1.7&#39;
[DEBUG] Normalized Java String: &#39;1.7&#39;
[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] ------------------------------------------------------------------------

but it seems the rules are not honored, since when i run the enforce, the No rules are configured. error message is shown

$ mvn enforcer:enforce -Djava.version=1.7
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------&lt; foo:foo &gt;-------------------------------
[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. -&gt; [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

when providing rules through command line argument, it seems the enforcer does evaluate them

$ mvn enforcer:enforce -Drules=alwaysFail
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------&lt; foo:foo &gt;-------------------------------
[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. -&gt; [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

but setting &lt;AlwaysFail /&gt; in /project/build/plugins/plugin/executions/execution/configuration/rules/AlwaysFail (xpath) doesn't seem to produce the same thing -- keep getting No rules are configured when executed without a command line arguments.

i am using maven 3.6.3 and openjdk 1.8 under debian buster

$ 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: &quot;linux&quot;, version: &quot;4.19.76-linuxkit&quot;, arch: &quot;amd64&quot;, family: &quot;unix&quot;

$ lsb_release -a
No LSB modules are available.
Distributor ID:	Debian
Description:	Debian GNU/Linux 10 (buster)
Release:	10
Codename:	buster

UPDATE:

after moving the &lt;configuration&gt; block outside the &lt;executions&gt; block and running in a debug mode, i get the output where it shows that the java.version=0.7 is detected by by the enforcer but it does not fail the build. see below:

$ 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&gt;org.apache.maven.plugins:maven-enforcer-plugin:3.0.0-M3, parent: sun.misc.Launcher$AppClassLoader@7852e922]
[DEBUG] Configuring mojo &#39;org.apache.maven.plugins:maven-enforcer-plugin:3.0.0-M3:enforce&#39; with basic configurator --&gt;
[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: &#39;1.8.0_262&#39;
[DEBUG] Normalized Java String: &#39;1.8.0-262&#39;
[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] ------------------------------------------------------------------------

how to make the enforcer fail when the required java and maven version are unmet?

答案1

得分: 5

你已将规则配置为&lt;execution&gt;的一部分。

如果你想让这个配置在其他执行(比如命令行执行)中也生效,你需要将&lt;configuration&gt;块移到&lt;executions&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-M3&lt;/version&gt;
        &lt;executions&gt;
          &lt;execution&gt;
            &lt;id&gt;enforce-versions&lt;/id&gt;
            &lt;goals&gt;
              &lt;goal&gt;enforce&lt;/goal&gt;
            &lt;/goals&gt;
          &lt;/execution&gt;
        &lt;/executions&gt;
        &lt;configuration&gt;
           &lt;failFast&gt;true&lt;/failFast&gt;
           &lt;rules&gt;
                &lt;requireMavenVersion&gt;
                  &lt;version&gt;${mvn.version}&lt;/version&gt;
                &lt;/requireMavenVersion&gt;
                &lt;requireJavaVersion&gt;
                  &lt;version&gt;${required.java.version}&lt;/version&gt;
                &lt;/requireJavaVersion&gt;
            &lt;/rules&gt;
          &lt;/configuration&gt;
      &lt;/plugin&gt;

请注意,你不应该使用${java.version}属性,因为它显然是Maven的一个“official”属性。

英文:

You configured the rules as part of an &lt;execution&gt;.

If you want the configuration to be used also for other executions (like on command line), you need to move the &lt;configuration&gt; block outside the &lt;executions&gt; block.

It should look like this:

      &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-M3&lt;/version&gt;
        &lt;executions&gt;
          &lt;execution&gt;
            &lt;id&gt;enforce-versions&lt;/id&gt;
            &lt;goals&gt;
              &lt;goal&gt;enforce&lt;/goal&gt;
            &lt;/goals&gt;
          &lt;/execution&gt;
        &lt;/executions&gt;
        &lt;configuration&gt;
           &lt;failFast&gt;true&lt;/failFast&gt;
           &lt;rules&gt;
                &lt;requireMavenVersion&gt;
                  &lt;version&gt;${mvn.version}&lt;/version&gt;
                &lt;/requireMavenVersion&gt;
                &lt;requireJavaVersion&gt;
                  &lt;version&gt;${required.java.version}&lt;/version&gt;
                &lt;/requireJavaVersion&gt;
            &lt;/rules&gt;
          &lt;/configuration&gt;
      &lt;/plugin&gt;

Note that you should not use the property ${java.version} as it is apparently an "official" property of Maven.

答案2

得分: 4

阅读了文档后,特别是版本范围规范,当版本被指定为数字时,意味着版本比较运算符为>=(大于等于)该版本。

为了解决这个问题,我使用了方括号([]),例如:

<requireMavenVersion>
  <version>[${mvn.version}]</version>
</requireMavenVersion>

如注释所述,使用java.versionmvn.version是内置属性。

英文:

after reading reading thoroughly the documentation, specifically version range specification, when the version is specified as a number, it implies that the version comparison operator is &gt;= (greater than or equal to) the version.

to solve the issue, i used square brackets ([]), e.g.

&lt;requireMavenVersion&gt;
  &lt;version&gt;[${mvn.version}]&lt;/version&gt;
&lt;/requireMavenVersion&gt;

as noted, using java.version and mvn.version are built-in properties.

huangapple
  • 本文由 发表于 2020年8月2日 16:54:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63214152.html
匿名

发表评论

匿名网友

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

确定