[英]Configure maven-compiler-plugin so it works both with JDK-8 and JDK-12 (in Spring Boot project)
Is it possible to configure maven-compiler-plugin
so it works both with JDK 8 and JDK 12?是否有可能配置maven-compiler-plugin
所以它的工作原理都与JDK 8和JDK 12? I am not sure if it is relevant, but it is a Spring Boot project.我不确定它是否相关,但它是一个 Spring Boot 项目。
1. The configuration 1.配置
<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:可在 JDK-12 下编译,但在 JDK-8 下失败:
[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 2.配置
<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:缺少<release>8</release>
参数在 JDK-8 下可编译,但在 JDK-12 下失败:
[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.我发现所有可能的互联网资源都建议使用<release>8</release>
参数来避免 JDK-12 下的错误,但这会禁用 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 :)我们的源代码和目标兼容性是 Java 8,我们需要在使用 JDK-12 的开发人员机器上使用旧的普通mvn clean install
(不提供配置文件!)构建代码,但也在 Jenkins 上构建代码,我们仍然有保留 JDK-8,我们也有一些保守的开发人员:)
first you should define the maven-compiler-plugin within the pluginManagement like this:首先,您应该像这样在 pluginManagement 中定义 maven-compiler-plugin:
<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.通常不需要mvn clean install
。 You usually only need mvn clean verify
...你通常只需要mvn clean verify
...
A tad simpler solution relies on using the properties rather than the explicit configuration entries.一个更简单的解决方案依赖于使用属性而不是显式配置条目。 It won't complain for anything that it doesn't support.它不会抱怨任何它不支持的东西。
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<profiles>
<profile>
<id>jdk9+</id>
<activation>
<jdk>[9,)</jdk>
</activation>
<properties>
<maven.compiler.release>8</maven.compiler.release>
</properties>
</profile>
</profiles>
Enjoy!享受!
您可以使用Maven配置文件并根据jdk激活它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.