3

My current task is to ensure a given Java project runs on Java (JRE) from version 6 to version 9. Is there any ways to do this with Maven?

I guess configuring maven-compilter-plugin is part of it. Should it be

<source>1.6</source>

to ensure no higher version features are used? And what about <target>?

And which Java version should Maven run with? Java 9?

The animal-sniffer-maven-plugin also looks interesting, but not sure how it fits here. It's also difficult, because Java 6 is not really the minimum API, as AFAIK it has some classes Java 9 has not (e.g. some from the sun and com.sun packages).

What else? Has anyone done this already?

Marcus
  • 1,857
  • 4
  • 22
  • 44
  • See also: http://stackoverflow.com/questions/36583118/is-maven-ready-for-jdk9 – ZhekaKozlov May 05 '17 at 11:02
  • 3
    The `com.sun` packages aren't part of the API and should never be used by applications. – Kevin Krumwiede May 05 '17 at 11:12
  • Well, it's not my application. I did not use those com.sun classes in the project, someone else did many years ago. I'm probably the one to find and replace them ... – Marcus May 05 '17 at 11:17
  • 1
    @mbee If you use classes from `com.sun` then it will not run in Java 9 with JPMS. You will have to refactor your application first to make it fit for Java 9 – Uwe Plonus May 05 '17 at 11:29
  • 2
    The last comment is mostly false. Most `com.sun` packages still exist and can be made accessible with the command line flags `--add-exports` - see `java -X` or `javac -X`. Have a look at this post to see [how to use Maven `` with these flags](https://medium.com/codefx-weekly/java-9-maven-compile-errors-module-names-kill-switches-73411c511750#6ef4). – Nicolai Parlog May 06 '17 at 07:01
  • You may find this answer useful: https://stackoverflow.com/a/26565660/363573. It uses the maven enforcer plugin to find *"dependencies which are using a different Java version than you like to have"*. – Stephan Sep 21 '17 at 08:18

1 Answers1

3

Some notes about this (Maybe not really an answer):

First the animal-sniffer-maven-plugin plugin can check if your code is only using code from the JDK 6 (API) which is the lowest part you have to check for (or other JDK versions). Furthermore you need to check if you project has dependencies to jar's which contains byte code does not require more than JDK 6 based code.

The <target>/<source> will not ensure things like this. They make only sure that the resulting jar file (class file format) will work with at least JRE 6 but does not make sure it will also run on JRE 9.

If you need to run on JRE 9 you need to have a module-info.java file in your project.

Furthermore you your project uses com.sun.* you have much things to do cause as far as I remember the majority or all of them have been declared as deprecated/or for com.sun.* only internal and not being part of the public API (If you would run on a Mac with a JDK those classes do not exist) for a very long time (see here).

You can take a look at Maven Core cause we need to make sure Maven is running on JRE 9 also...(We are using JDK 9 EA Package to test this)..There we use animal-sniffer-maven-plugin etc. to make sure this works.

khmarbaise
  • 92,914
  • 28
  • 189
  • 235