为什么 maven-enforcer-plugin 在 travis-ci.org 上检测到 maven 3.2.5 运行 和 3.1.1?

Why does maven-enforcer-plugin detect maven 3.2.5 when it's run with 3.1.1 on travis-ci.org?

我有一个项目 document-scanner-aggregator which runs fine locally when run with Maven 3.1.1, i.e. ~/apache-maven-3.1.1/bin/mvn clean install, but doesn't on travis-ci.org,它在其中检测到 Maven 3.2.5,这可疑地是由 travis-ci.org 提供的版本。然而,它不应该这样做,因为运行构建的 Maven 版本应该被强制执行,对吗?

失败是

[WARNING] Rule 0: org.apache.maven.plugins.enforcer.RequireMavenVersion failed with message:
Detected Maven Version: 3.2.5 is not in the allowed range (,3.2).
...
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-enforcer-plugin:1.4:enforce (enforce-versions) on project javaocr-parent: Some Enforcer rules have failed. Look above for specific messages explaining why the rule failed. -> [Help 1]

配置为

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-enforcer-plugin</artifactId>
    <version>1.4</version>
    <executions>
        <execution>
            <id>enforce-versions</id>
            <goals>
                <goal>enforce</goal>
            </goals>
            <configuration>
                <rules>
                    <requireMavenVersion>
                        <!--different rules for different issues-->
                        <!--3.3.x causes `java.lang.NoClassDefFoundError: org/eclipse/aether/spi/connector/Transfer$State` which is caused by certain maven versions, see https://cwiki.apache.org/confluence/display/MAVEN/AetherClassNotFound for details-->
                        <version>(,3.3)</version>
                        <!--3.2.x causes `No implementation for org.eclipse.aether.connector.wagon.WagonConfigurator was bound.`-->
                        <version>(,3.2)</version>
                    </requireMavenVersion>
                </rules>
            </configuration>
        </execution>
    </executions>
</plugin>

javaocr.

.travis.yml

language: java
install:
- wget http://mirrors.ae-online.de/apache/maven/maven-3/3.1.1/binaries/apache-maven-3.1.1-bin.tar.gz && tar xf apache-maven-3.1.1-bin.tar.gz
- apache-maven-3.1.1/bin/mvn clean install

您误解了 Enforcer Plugin 的作用:

The Enforcer plugin provides goals to control certain environmental constraints such as Maven version, JDK version and OS family along with many more standard rules and user created rules.

它的目标不是变魔术来验证配置的规则,而是当其中一条规则未被验证时构建失败。换句话说,它会检查已配置规则的列表,这样如果其中一个未被验证,构建就无法继续。原因是如果构建的先决条件之一不满足,还不如趁早失败。

在您的情况下,requireMavenVersion rule enforces that the build is done with a Maven version strictly lower than 3.2. It will not spawn a new Maven instance if you're running the build with a Maven 3.2.5 so that the rule is verified; which version should it use, and where would it get it? Instead, it detects that the build is done with a Maven version that is disallowed, and fails accordingly. Since the Travis build is configured to use Maven 3.2.5预计会失败,您需要为 Travis 构建安装低于 3.2 的 Maven 版本。

要在 Travis CI 上使用不同的 Maven 版本,例如 3.1.1,您可以在 .travis.yml 中添加以下内容:

before_install:
  - wget http://mirrors.ae-online.de/apache/maven/maven-3/3.1.1/binaries/apache-maven-3.1.1-bin.tar.gz
  - tar xf apache-maven-3.1.1-bin.tar.gz
  - export M2_HOME=$PWD/apache-maven-3.1.1
  - export PATH=$M2_HOME/bin:$PATH 

install: /bin/true

script: mvn clean install

作为旁注,您当前的

配置
<requireMavenVersion>
    <version>(,3.3)</version>
    <version>(,3.2)</version>
</requireMavenVersion>

完全等同于

<requireMavenVersion>
    <version>(,3.2)</version>
</requireMavenVersion>

严格匹配3.2以下版本的范围(,3.2)means。因此,要求它也严格低于 3.3 是多余的(因为 3.2 在 3.3 之前)。