4

Suppose I have a multi-module Maven project, with two modules:

  • foo; requires Java 1.8 to compile
  • bar; requires Java 11 to compile

I'd like to be able to build these by running mvn clean install from the parent directory and have them compile using the appropriate Java versions.

Let's be clear: upgrading both to Java 11 is a non-starter. Why:

  • I want to upgrade application bar to Java 11 to get the latest features and improvements of the Java language. bar is a standalone Spring Boot application and I am able to do that.
  • Application foo is a JBoss deployment module plugin. It's plugged in to a third-party app that at present is on Java 8. I cannot drop a module compiled in Java 11 into an app running in Java 8.

I see that there is a way to do this using the maven-toolchains-plugin as described in this post. That could work. But I use jenv to manage versions, and it's possible to set the Java version per directory (using jenv local). I'm wondering if there is a way to use Jenv to do this instead of the toolchains plugin. It does just about everything I need; I just need to figure out how to get Maven to read the Java version from the jenv config file in the directory.

cb4
  • 6,689
  • 7
  • 45
  • 57
Mark
  • 4,970
  • 5
  • 42
  • 66
  • 3
    what's wrong with just compiling both projects with java 11 and moving on? – Beta033 Feb 20 '19 at 21:48
  • You may not migrate to jdk11 since it may well be incompatible. E.g. a class compiled with newer version may use instructions that are not available on newer jvm. – Some Name Feb 20 '19 at 22:05
  • I cannot use Java 11 for project foo; it builds a drop-in to a third party app that runs only with Java 8 – Mark Feb 20 '19 at 23:25
  • Though it's related, it's is not a duplicate of Compile Maven Module with Different Java Version. I'm asking specifically about leveraging Jenv to solve this problem. – Mark Feb 21 '19 at 11:13
  • 1
    You can use java 11 to compile all projects, but you can set compatibility level for each sub module using the maven-compiler plugin -> just set the `source` and `target` to the required java version such as 7,8, etc and the java 11 compiler will produce java 7,8,etc compatible binaries. – Svetlin Zarev Feb 21 '19 at 11:20
  • @Svetlin the source/target compatibility level is useless when the project uses classes from the JRE that has been removed since Java 8 (e.g. java.xml.bind). – Robert Feb 21 '19 at 15:21
  • If he wishes to compile using older java, then he intends to run it on such, so I see no issue here. Also the removed APIs can be added as maven dependencies if necessary – Svetlin Zarev Feb 22 '19 at 07:25

0 Answers0