配置 maven-compiler-plugin,使其与 JDK-8 和 JDK-12 一起工作(在 Spring 引导项目中)

Configure maven-compiler-plugin so it works both with JDK-8 and JDK-12 (in Spring Boot project)

是否可以配置 maven-compiler-plugin 使其工作 both with JDK 8 and JDK 12?我不确定它是否相关,但它是一个 Spring 启动项目。

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>

在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.配置

<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>

缺少 <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

我发现所有可能的互联网资源建议使用 <release>8</release> 参数来避免 JDK-12 下的错误,但这会禁用 JDK- 下的可编译性8.

我们的源和目标兼容性是 Java 8,我们需要 使用老式的 mvn clean install 构建代码(不提供配置文件!) 在开发人员的机器上使用 JDK-12,但在 Jenkins 上,我们仍然必须保留 JDK-8,我们也有一些保守的开发人员 :)

首先,您应该像这样在 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>
      <...>
    ...

并进一步使用自动激活的配置文件,如下所示:

  <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>

通常不需要 mvn clean install。你通常只需要 mvn clean verify...

一个更简单的解决方案依赖于使用属性而不是显式配置条目。它不会抱怨任何它不支持的东西。

<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>

尽情享受吧!