Skip to content

linux-china/toolchains-maven-plugin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

72 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Toolchains Maven Plugin

Extend maven-toolchains-plugin to add JDK auto download and toolchains.xml management.

Features

  • JDK auto download by Foojay API support, and install directory is ~/.m2/jdks
  • Add new toolchain into toolchains.xml dynamically
  • SDKMAN integration: add JDK to toolchains.xml from SDKMAN if SDKMAN detected
  • JBang integration: add/auto-install JDK to toolchains.xml from JBang if jbang detected

Requirements

  • Maven 3.5+
  • JDK 1.7+

How to use?

Add following plugin configuration to your pom.xml:

<build>
    <plugins>
        <plugin>
            <groupId>org.mvnsearch</groupId>
            <artifactId>toolchains-maven-plugin</artifactId>
            <version>4.5.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal>toolchain</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <toolchains>
                    <jdk>
                        <version>17</version>
                    </jdk>
                </toolchains>
            </configuration>
        </plugin>
    </plugins>
</build>

And you can try it quickly:

$ git clone https://github.com/linux-china/java17-demo.git
$ cd java17-demo
$ mvn compile

GraalVM support

  • vendor should be graalvm_ce17 or graalvm_ce11
  • version is GraalVM version(not Java version), such as 22.3 or 22.3.0
  • GraalVM native-image component will be installed automatically
<plugin>
    <groupId>org.mvnsearch</groupId>
    <artifactId>toolchains-maven-plugin</artifactId>
    <version>4.5.0</version>
    <executions>
        <execution>
            <goals>
                <goal>toolchain</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <toolchains>
            <jdk>
                <version>22.3</version>
                <vendor>graalvm_ce17</vendor>
            </jdk>
        </toolchains>
    </configuration>
</plugin>

How to get a list of all supported distributions and JDK versions?

Please visit https://api.foojay.io/disco/v3.0/distributions to get all information.

  • jdk vendor is the value of api_parameter
  • jdk version value could be any value in versions array

Or you can use Maven toolchains CLI to get all supported JDKs information.

Maven toolchains CLI

How to skip toolchains maven plugin on CI/CD platform?

$ mvn -Dtoolchain.skip -DskipTests package

Different JDK for main/test code

Maven has support for using different source and target java versions for your project's main code and tests. You can add testJdk toolchain in toolchains-maven-plugin to specify the JDK for test code as below:

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.11.0</version>
        <executions>
            <execution>
                <id>default-testCompile</id>
                <phase>test-compile</phase>
                <goals>
                    <goal>testCompile</goal>
                </goals>
                <configuration>
                    <jdkToolchain>
                        <version>18</version>
                    </jdkToolchain>
                </configuration>
            </execution>
        </executions>
        <configuration>
            <source>8</source>
            <target>8</target>
            <parameters>true</parameters>
            <testSource>18</testSource>
            <testTarget>18</testTarget>
            <testRelease>18</testRelease>
            <testCompilerArgument>--enable-preview</testCompilerArgument>
        </configuration>
    </plugin>

    <plugin>
        <groupId>org.mvnsearch</groupId>
        <artifactId>toolchains-maven-plugin</artifactId>
        <version>4.5.0</version>
        <executions>
            <execution>
                <goals>
                    <goal>toolchain</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <toolchains>
                <jdk>
                    <version>8</version>
                </jdk>
                <testJdk>
                    <version>18</version>
                </testJdk>
            </toolchains>
        </configuration>
    </plugin>
</plugins>

References

sdkman support: sdk install java 17.0.7-graalce