0

How can I compile both main and test sources with my system's default Java version (e.g. Java 8.0 specified in $JAVA_HOME), but use a different Java installation to execute the JUnit tests (e.g. Java 11.0) in Maven?

This question is different from some previous questions, which want to use different target source versions, or different compilers for the main and test sources:

I want to use the same compiler (e.g. Java 8.0) for both compile and test-compile goals of Maven, but execute all JUnit tests of Maven's test goal via another Java installation (e.g. Java 11.0).

I'm using maven-surefire for execution of unit-tests. I'm looking for a short and clean solution, and I'd rather not use maven-toolchains. But if using maven-toolchains is the only possible solution, then so be it.

Seyed Mohammad
  • 798
  • 10
  • 29
  • First what is the real problem? Why not using JDK 11 to run everything? So you can set `8` and then to keep compatibility with JDK 8 ? The clean solution would either be maven-toolchains ... or maybe even better compile/build your project via CI with both JDK's? Do you like to keep compatibility with JDK 8? – khmarbaise Nov 18 '20 at 11:38
  • @khmarbaise The problem is smooth transition to post Java-8.0. If you check out Oracle's migration guidelines, the first step is to run your application with Java-11.0 without compiling to see if it works. This means that don't change your compiler, and just run your tests and see how it goes. – Seyed Mohammad Nov 18 '20 at 16:29
  • I would simply compile the application with JDK 11 with release to 8 ... cause many projects in the meantime doing that already ... I would suggest to create a branch of your application and run with different JDK on your CI ... that's it..... Why should i not compile my app with JDK 11 ? Just build it with JDK 11 (release 8) and then go... I migrated several apps to JDK 11 minimum level in the meantime... CI is the key... – khmarbaise Nov 18 '20 at 19:42

0 Answers0