Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want to use Java 11 syntax in my unit tests, but my 'main' code needs to be compiled for Java 8 since my production environment only has JDK 8 installed.

Is there a way of doing this with the maven-compiler-plugin? My Jenkins server has Java 11 installed.

I will accept the risk that I can accidental use Java 11 specific functionality in my production code.

like image 710
rjdkolb Avatar asked Jun 20 '14 08:06

rjdkolb


People also ask

Does Maven work with Java 11?

Chances are that it just runs fine with Java 11. Hint: You can speed up multi-module Maven projects by using parallel builds, e.g. mvn -T 4 compile compiles all modules in parallel on 4 CPU cores.

Can a Java 8 project use Java 11 library?

The class files created by Java 8 are still executable in Java 11; however, there have been other changes in the Java runtime (library changes, etc.) that might require modification of the code. These modifications may be made in Java 8 and compiled with Java 8 making it compatible with the Java 11 runtime.


1 Answers

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> 
like image 184
mkrakhin Avatar answered Sep 28 '22 13:09

mkrakhin