简体   繁体   中英

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? 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 :)

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

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激活它们。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM