Building jar targeting multiple Java versions, including 9

Robert Scholte rfscholte at
Wed Aug 31 11:10:04 UTC 2016

I've been working on the implementation of this in the  
maven-compiler-plugin, but I'm not really pleased with the result.
The problem is that in the worst case scenario we have to work with 3  
different versions of Java:
- The Maven Runtime (set as JAVA_HOME)
- JDK for the
- JDK for all other source files.

The example below worked because all three were set to JDK9.
But based on the source/target of 1.6 I cannot predict which JDK is used,  
only that it is at least JDK6. Should the plugin switch to another JDK?
And if you want to compile with source/target 1.5 or less, you're in  
trouble. There's something called toolchain, where you can specify the  
JavaHome per version, but in case of maven-compiler-plugin it assumes that  
all java-related plugins and execution blocks want to use the same  
toolchain through the whole Maven project.
The good news is that for the maven-jdeps-plugin I improved this part in  
Maven 3.3.1, since this plugin only works with Java8 and above, which  
doesn't have to be the same JDK to compile the sources with. Now you can  
simple say: I want the toolchain for version X. This feature needs to be  
added to the plugin.

That said I think I will write a recipe for this. This is first of all an  
issue for library writers who want to have their jar compatible with  
multiple Java versions for their end users.
Result: One javac call per execution block as it was meant to be.


On Fri, 26 Aug 2016 15:31:07 +0200, Oliver Gondža <ogondza at>  

> Thank you all for your suggestions. I managed to get the project to  
> build with following maven setup:
> ```
>    <!--
>          When using compiler from java 8 and older, ignore module files  
> altogether.
>          Otherwise, use 2 phase compilation to build
>            - all classes for target version and
>            - with 9+ source and target level
>       -->
>    <build>
>      <plugins>
>        <plugin>
>          <groupId>org.apache.maven.plugins</groupId>
>          <artifactId>maven-compiler-plugin</artifactId>
>          <version>3.5.1</version>
>          <configuration>
>            <source>1.6</source>
>            <target>1.6</target>
>          </configuration>
>          <executions>
>            <execution>
>              <id>default-compile</id>
>              <configuration>
>                <excludes>
>                  <exclude>**/</exclude>
>                </excludes>
>              </configuration>
>            </execution>
>          </executions>
>        </plugin>
>      </plugins>
>    </build>
>    <profiles>
>      <profile>
>        <id>jigsaw</id>
>        <activation>
>          <jdk>[1.9,)</jdk>
>        </activation>
>        <build>
>          <plugins>
>            <plugin>
>              <groupId>org.apache.maven.plugins</groupId>
>              <artifactId>maven-compiler-plugin</artifactId>
>              <version>3.5.1</version>
>              <configuration>
>                <source>1.9</source>
>                <target>1.9</target>
>              </configuration>
>              <executions>
>                <execution>
>                  <id>module-infos</id>
>                  <phase>compile</phase>
>                  <goals>
>                    <goal>compile</goal>
>                  </goals>
>                  <configuration>
>                    <includes>
>                      <include>**/</include>
>                    </includes>
>                  </configuration>
>                </execution>
>              </executions>
>            </plugin>
>          </plugins>
>        </build>
>      </profile>
>    </profiles>
> ```
> It does compile with older javac versions as a bonus. Given this is  
> nothing else than using `-source 9 -target 9` for if  
> present, I dare to say maven-compiler-plugin can be adapted to figure  
> this out on its own.

More information about the jigsaw-dev mailing list