I found a way to do that, which lies somewhat in the middle of Java 8 and Java 11. The main problem with the above solution occurs in case you have (or want to define) a module-info.java
that can't be parsed in Java 8.
I started considering a solution with <classifier>
s, likewise Claudio Weiler above answer but did not wanted to bother having two separate builds (and even two separate branches because I wanted to manage module-info).
I also noticed that some libraries like the JAXB API or activation API (jakarta.xml.bind:jakarta.xml.bind-api:2.3.3
) shows a module-info
when you use it in Java 11, but is compatible with Java 8 .
Digging into how the details of how the build of the com.sun.activiation:jakarta.activation
library was made, I found the following solution :
- compile the
module-info.java
with Java 11
- compile the rest of your code (including tests) with Java 8. Of course, this has the drawback of not being able to use fully Java 11 features...
You thus need to add the following snippet in the <build>
section of your pom.xml:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>default-compile</id>
<configuration>
<source>1.8</source>
<target>1.8</target>
<excludes>
<exclude>module-info.java</exclude>
</excludes>
</configuration>
</execution>
<execution>
<id>module-info-compile</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<release>11</release>
<includes>
<include>module-info.java</include>
</includes>
</configuration>
</execution>
<execution>
<id>default-testCompile</id>
<configuration>
<release>11</release>
</configuration>
</execution>
</executions>
</plugin>
I hope that helps.