简体   繁体   English

配置 maven-compiler-plugin 使其适用于 JDK-8 和 JDK-12(在 Spring Boot 项目中)

[英]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 8JDK 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.

相关问题 为 maven-compiler-plugin 设置默认 jdk - set the default jdk for maven-compiler-plugin 如何使用 maven-compiler-plugin 配置 Lombok? - How to configure Lombok with maven-compiler-plugin? `spring-boot-maven-plugin` 和 `maven-compiler-plugin` 有什么区别? - What's the difference between `spring-boot-maven-plugin` and `maven-compiler-plugin`? 源和目标的Maven编译器插件jdk版本 - Maven compiler plugin jdk versions for source and target 子 maven 项目找不到 maven-compiler-plugin - child maven project cannot find maven-compiler-plugin 是否可以在Maven编译器插件中配置Javac版本而不安装特定的Jdk版本? - Is it possible to configure javac version in maven compiler plugin without installing that specific Jdk version? 找不到 maven 编译器插件 - maven-compiler-plugin not found 将 Spring Boot 项目从 JDK 8 升级到 JDK 11 - Upgrading a Spring Boot project from JDK 8 to JDK 11 如何编写 dockerfile 以在包含 jdk 的现有 Spring Boot 项目上添加 maven 构建 - How to write a dockerfile to add maven build on existing spring boot project which contains jdk 如何修复[错误]域= plugin&gt; org.apache.maven.plugins:maven-compiler-plugin:3.x(在Spring Boot上运行) - How to fix [Error] realm = plugin>org.apache.maven.plugins:maven-compiler-plugin:3.x (Happened while working on Spring Boot)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM