javamavenjettypom.xmlalpn

Maven dependency version dependent on JVM version


I'm trying to create a Maven POM which will pull in the correct version of alpn-boot when building the project and running the tests.

The whole thing works if I'm injecting the property from "outside" when running the build (e. g. via mvn -Dalpn-boot.version=8.1.8.v20160420 test or as an environment variable) but not when trying to interpolate it inside the POM using ${java.version}.

I've tried using properties-maven-plugin to accomplish this with the following settings in the pom.xml file (snippet):

<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>

Contents of one of the properties files:

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

Basically the same problem with the 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>

With both approaches I get the following error message:

[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

Solution

  • I ended up following the advice of @khmarbaise and add a Maven profile for each version of the JDK which needs to be supported:

    <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>