问题
Is it possible to configure maven-compiler-plugin
so it works both with JDK 8 and JDK 12? I am not sure if it is relevant, but it is a Spring Boot project.
1. The configuration
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<release>8</release>
</configuration>
</plugin>
is compilable under JDK-12, but under JDK-8 fails:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project xxxx:
Fatal error compiling: invalid flag: --release
2. The configuration
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
which misses the <release>8</release>
parameter is compilable under JDK-8, but under JDK-12 fails:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project xxxx:
Fatal error compiling: CompilerException: NullPointerException
All the possible internet sources I found advice to use the <release>8</release>
parameter to be able to avoid the error under JDK-12, but this disables the compilability under JDK-8.
Our source and target compatibility is Java 8, and we need to build the code with the good old plain mvn clean install
(without providing a profile!) on the developers' machines with JDK-12, but also on Jenkins, where we still have to keep JDK-8, and we also have some conservative developers :)
回答1:
first you should define the maven-compiler-plugin within the pluginManagement like this:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<...>
...
and furthermore using profile which are automatically activated which looks like this:
<profiles>
<profile>
<id>jkd-12-compile</id>
<activation>
<jdk>12</jdk>
</activation>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<release>8</release>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
<profile>
<id>jdk-8-compile</id>
<activation>
<jdk>[,8]</jdk>
</activation>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
</profiles>
One not usually a mvn clean install
is not needed. You usually only need mvn clean verify
...
回答2:
You can use Maven profiles and activate them depending on the jdk.
回答3:
A tad simpler solution relies on using the properties rather than the explicit configuration entries. I won't complain for anything that it doesn't support.
<profiles>
<profile>
<id>jdk9+</id>
<activation>
<jdk>[9,)</jdk>
</activation>
<properties>
<maven.compiler.release>8</maven.compiler.release>
</properties>
</profile>
</profiles>
Enjoy!
来源:https://stackoverflow.com/questions/55404641/configure-maven-compiler-plugin-so-it-works-both-with-jdk-8-and-jdk-12-in-sprin