Home > Net >  Create Java 8 and Java 11 build for single API
Create Java 8 and Java 11 build for single API

Time:04-20

We are working to upgrade our API from Java 8 to Java 11.

However we need to support Java 8 and Java 11 both at same time because all clients are not ready to move on Java 11.

Is it possible if we can create maven build with Java 8 and Java 11 without duplicate our repository and without any change in pom.xml each time before create build for Java 8 and Java 11?

Generated artifact should have different name to distinguish Java 8 and Java 11 versions something like xyz-jdk8-0.0.1.jar and xyz-jdk11-.0.1.jar

CodePudding user response:

You should:

  • Remove from you POM, if present, maven.compiler.source and maven.compiler.target.
  • Set JAVA_HOME=<<JAVA_8_HOME_FOLDER>>.
  • Build from Java 8 by mvn clean install.
  • Take the Java 8 artifact.
  • Set JAVA_HOME=<<JAVA_11_HOME_FOLDER>>.
  • Build from Java 11 by mvn clean install.
  • Take the Java 11 artifact.

CodePudding user response:

TL;DR

You don't need that, just build to Java 8 and be happy!


Solving

You can use Maven Build Profiles for this: https://maven.apache.org/guides/introduction/introduction-to-profiles.html

1. Set your properties to Java 11:

<properties>
  <maven.compiler.target>11</maven.compiler.target>
  <maven.compiler.source>11</maven.compiler.source>
  <maven.compiler.release>11</maven.compiler.release>
</properties>

2. Add a profile:

<profile>
  <id>jdk-8</id>
  <properties>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.release>1.8</maven.compiler.release>
  </properties>
</profile>

3. Build:

You will need to run 2 builds, one normal, and other activating the JDK 8 profile:

$ mvn ...
$ mvn ... -P jdk-8

Considerations:

  • Always use JDK 11 as it can confidently build Java 8 targets;
  • This process will override targets, so copy target between builds or use the profile to change final name.
  • You don't need target and source properties, but if some plugin fails, put it back.
  • Related