maven toolchains 简单说明

发布时间 2023-12-21 14:44:24作者: 荣锋亮

很多时候我们项目可以会包含需要不同jdk 构建,比如有些只能使用jdk8,有些需要使用jdk11,toolchains 可以帮助我们解决此问题

一般玩法

创建一个toolchains.xml 目录,放到home 目录下,里边配置实际需要的jdk 版本(我们的环境可以安装多jdk)
项目构建的时候(使用的插件)使用配置的工具

  • 参考配置
 
<?xml version="1.0" encoding="UTF-8"?>
<toolchains>
  <toolchain>
    <type>jdk</type>
    <provides>
      <version>1.8</version>
      <vendor>zulu</vendor>
    </provides>
    <configuration>
      <jdkHome>...../8.0.382-zulu</jdkHome>
    </configuration>
  </toolchain>
  <toolchain>
    <type>jdk</type>
    <provides>
      <version>11</version>
      <vendor>zulu</vendor>
    </provides>
    <configuration>
      <jdkHome>...../11.0.20-zulu</jdkHome>
    </configuration>
  </toolchain>
</toolchains>
  • 项目使用
<plugin>
  <artifactId>maven-surefire-plugin</artifactId>
  <configuration>
    <!-- Need to run with Java 8, because `GuavaPatcher` runs too late, and
     `com.google.common.base.Stopwatch` has already been loaded. GuavaPatcher
      tries to re-define the Stopwatch class in the same classloader, but Java
      Java 11 no longer allows this. The existing workaround with that
      `GuavaPatcherRunner` runs into the same issue. A proper solution would be
      to implement a Java agent, which _is_ allowed to re-define classes.
      -->
    <jdkToolchain>
      <version>1.8</version>
    </jdkToolchain>
  </configuration>
</plugin>

说明

比如dremio 目前构建就需要配置toolchains,依赖的一些包构建只能使用jdk8

参考资料

https://maven.apache.org/guides/mini/guide-using-toolchains.html