带有模块信息的旧项目

想要兼容旧版 Java(即 1.8 或以下字节码和 API)的项目,但也想提供一个module-info.java用于 Java 9+ 运行时,必须知道他们需要调用javac两次:

  1. module-info.java必须使用release =9编译,
  2. 而其余的源必须使用较低的预期兼容性版本进行编译source / target

执行此操作的首选方法是使用 2 个执行块,如下所述:

  1. 使用release=9的默认默认编译执行,
  2. 具有预期目标兼容性的附加自定义基础编译执行。

请注意,此外,JDK 9 仅支持 Java 6 及更高版本的编译,因此希望与 Java 5 或更低版本兼容的项目需要使用两个不同的 JDK 进行 2 次执行。使用工具链配置,即使稍微复杂一点,也很容易实现这一点。

Java 6 到 8 的兼容性

如果您希望项目与 Java 6、7 或 8 兼容,您可以简单地将 JDK 9 用于两个执行块。

最简单的方法是使用 Java 9 作为 Maven 的运行时,方法是在运行mvn之前设置JAVA_HOME=/path/to/jdk-9。但是,如果您想为 Maven 使用较旧的 Java 运行时,您可以使用 maven-toolchain-plugin 指定共享 JDK(自 Maven 2.0.9 起支持)或自定义 jdkToolchain(自 Maven 3.3.1 起支持)并参考在您的系统上安装 JDK 9。

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.10.1</version>
        <executions>
          <execution>
            <id>default-compile</id>
            <configuration>
              <release>9</release>
              <!-- no excludes: compile everything to ensure module-info contains right entries -->
            </configuration>
          </execution>
          <execution>
            <id>base-compile</id>
            <goals>
              <goal>compile</goal>
            </goals>
            <configuration>
              <!-- recompile everything for target VM except the module-info.java -->
              <excludes>
                <exclude>module-info.java</exclude>
              </excludes>
            </configuration>
          </execution>
        </executions>
        <!-- defaults for compile and testCompile -->
        <configuration>
          <release>6</release><!-- or 7 or 8 depending on compatibility expectations -->
          <!-- Only required when Maven runtime JAVA_HOME isn't at least Java 9 and when haven't configured the maven-toolchains-plugin -->
          <jdkToolchain>
            <version>9</version>
          </jdkToolchain>
        </configuration>
      </plugin>
    </plugins>
    [...]
  </build>
  [...]
</project>

Java 5 或更低版本的兼容性

鉴于 Maven 3 在运行时需要更新的 Java 版本,您绝对需要使用工具链来使用与 Maven 运行时不同的 JDK。

请注意,您至少需要 Maven 3.3.1 才能在插件配置中指定自定义 jdkToolchain。您可以添加一个 jdkToolchain 来执行基本编译执行块以及参考 JDK 5。

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.10.1</version>
        <executions>
          <execution>
            <id>default-compile</id>
            <configuration>
              <release>9</release>
              <!-- no excludes: compile everything to ensure module-info contains right entries -->
              <!-- toolchain required when JAVA_HOME is JDK 8 or below -->
              <jdkToolchain>
                <version>9</version>
              </jdkToolchain>
            </configuration>
          </execution>
          <execution>
            <id>base-compile</id>
            <goals>
              <goal>compile</goal>
            </goals>
            <configuration>
              <!-- recompile everything for target VM except the module-info.java -->
              <excludes>
                <exclude>module-info.java</exclude>
              </excludes>
            </configuration>
          </execution>
        </executions>
        <!-- defaults for compile and testCompile -->
        <configuration>
          <source>1.5</source>
          <target>1.5</target>
          <!-- jdkToolchain required when JAVA_HOME is JDK 9 or above -->
          <jdkToolchain>
            <version>[1.5,9)</version>
          </jdkToolchain>
        </configuration>
      </plugin>
    </plugins>
    [...]
  </build>
  [...]
</project>