How to compile and run my Maven unit tests for a Java 11, while having my code compiled for an older version of Java 8 How to compile and run my Maven unit tests for a Java 11, while having my code compiled for an older version of Java 8 jenkins jenkins

How to compile and run my Maven unit tests for a Java 11, while having my code compiled for an older version of Java 8


In Maven compile and testCompile goals are different. And Maven even has parameters for testCompile: testTarget and testSource. So:

<plugin>    <groupId>org.apache.maven.plugins</groupId>    <artifactId>maven-compiler-plugin</artifactId>    <version>3.0</version>    <configuration>        <source>1.7</source>        <target>1.7</target>        <testSource>1.8</testSource>        <testTarget>1.8</testTarget>    </configuration></plugin>


A slightly more terse version of mkrakhin's answerYou can set the source, target, testSource and testTarget :

  <properties>    <maven.compiler.source>1.8</maven.compiler.source>    <maven.compiler.target>1.8</maven.compiler.target>    <maven.compiler.testSource>11</maven.compiler.testSource>    <maven.compiler.testTarget>11</maven.compiler.testTarget>  </properties></project>