本文介绍了配置maven-compiler-plugin,使其与JDK-8和JDK-12一起使用(在Spring Boot项目中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以配置maven-compiler-plugin,使其同时两者与JDK 8 JDK 12一起使用?我不确定是否相关,但这是一个Spring Boot项目.

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

<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下失败:

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

<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下失败:

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

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

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.

我们的源和目标兼容性是Java 8,我们需要在具有JDK-12的开发人员的计算机上使用良好的旧版本mvn clean install (不提供配置文件!)来构建代码. ,但在Jenkins上,我们仍然必须保留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 :)

推荐答案

首先,您应在pluginManagement内定义maven-compiler-plugin,如下所示:

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>

通常不需要一个mvn clean install.您通常只需要mvn clean verify ...

One not usually a mvn clean install is not needed. You usually only need mvn clean verify...

这篇关于配置maven-compiler-plugin,使其与JDK-8和JDK-12一起使用(在Spring Boot项目中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 17:38