Maven依赖版本依赖于JVM版本

Maven dependency version dependent on JVM version

我正在尝试创建一个 Maven POM,它将在构建项目和 运行 测试时引入 the correct version of alpn-boot

如果我在 运行 构建时从 "outside" 注入 属性(例如通过 mvn -Dalpn-boot.version=8.1.8.v20160420 test 或作为环境变量),那么整个事情都有效,但不是当尝试使用 ${java.version}.

在 POM 中插入它时

我尝试使用 properties-maven-plugin 通过 pom.xml 文件(片段)中的以下设置来完成此操作:

<project>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>properties-maven-plugin</artifactId>
        <version>1.0.0</version>
        <executions>
          <execution>
            <phase>initialize</phase>
            <goals>
              <goal>read-project-properties</goal>
            </goals>
            <configuration>
              <files>
                <file>${project.basedir}/alpn-boot/jdk-${java.version}.properties</file>
              </files>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
      <groupId>org.mortbay.jetty.alpn</groupId>
      <artifactId>alpn-boot</artifactId>
      <version>${alpn-boot.version}</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

其中一个属性文件的内容:

$ cat alpn-boot/jdk-1.8.0_92.properties
alpn-boot.version=8.1.8.v20160420

gmaven-plugin基本相同的问题:

<plugin>
  <groupId>org.codehaus.groovy.maven</groupId>
  <artifactId>gmaven-plugin</artifactId>
  <version>1.0</version>
  <executions>
    <execution>
      <phase>initialize</phase>
      <goals>
        <goal>execute</goal>
      </goals>
      <configuration>
        <source>
          alpnVersions = [ '1.8.0_92': '8.1.8.v20160420' ]
          project.properties['alpn-boot.version'] = alpnVersions[System.getProperty('java.version')]
        </source>
      </configuration>
    </execution>
  </executions>
</plugin>

使用这两种方法我都收到以下错误消息:

[INFO] Scanning for projects...
[ERROR] [ERROR] Some problems were encountered while processing the POMs:
[ERROR] 'dependencies.dependency.version' for org.mortbay.jetty.alpn:alpn-boot:jar must be a valid version but is '${alpn-boot.version}'. @ org.example:my-project:[unknown-version], /path/to/pom.xml, line 132, column 22
 @
[ERROR] The build could not read 1 project -> [Help 1]
[ERROR]
[ERROR]   The project org.example:my-project:1.0.0-rc3-SNAPSHOT (/path/to/pom.xml) has 1 error
[ERROR]     'dependencies.dependency.version' for org.mortbay.jetty.alpn:alpn-boot:jar must be a valid version but is '${alpn-boot.version}'. @ org.example:my-project:[unknown-version], /path/to/pom.xml, line 132, column 22
[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/ProjectBuildingException

我最终关注了 and add a Maven profile 需要支持的 JDK 的每个版本:

<properties>
    <!-- Default alpn-boot version. See <profiles> for specific profiles. -->
    <alpn-boot.version>8.1.3.v20150130</alpn-boot.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.mortbay.jetty.alpn</groupId>
        <artifactId>alpn-boot</artifactId>
        <version>${alpn-boot.version}</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<!-- Profiles for selecting the correct version of alpn-boot for each JDK.
     see http://www.eclipse.org/jetty/documentation/current/alpn-chapter.html for reference. -->
<profiles>
    <profile>
        <id>jdk-1.8.0</id>
        <activation>
            <jdk>1.8.0</jdk>
        </activation>
        <properties>
            <alpn-boot.version>8.1.0.v20141016</alpn-boot.version>
        </properties>
    </profile>
    <!-- Lots of profiles [...] -->
    <profile>
        <id>jdk-1.8.0_92</id>
        <activation>
            <jdk>1.8.0_92</jdk>
        </activation>
        <properties>
            <alpn-boot.version>8.1.8.v20160420</alpn-boot.version>
        </properties>
    </profile>
</profiles>