Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine JDK home from Maven toolchain

I have a Maven build configured to use maven-toolchains-plugin, with ~/.m2/toolchains.xml simply configured for multiple JDKs:

<?xml version="1.0" encoding="UTF8"?>
<toolchains>
    <toolchain>
        <type>jdk</type>
        <provides>
            <version>9</version>
            <vendor>oracle</vendor>
        </provides>
        <configuration>
            <jdkHome>/Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home</jdkHome>
        </configuration>
    </toolchain>
    <toolchain>
        <!-- ... several more JDKs ... -->
    </toolchain>
</toolchains>

My aim is to use the value of jdkHome within the POM, as an argument to the JDK9 jlink tool, but I can't find any way to extract the value, or set a usable property which references the jdkHome/JAVA_HOME of the toolchain JDK. Has anyone found a way to achieve this? I don't really want to have to resort to making yet another exec-maven-plugin or maven-antrun-plugin process just to set a property, and I figure there must be some easy way to achieve this which I've overlooked.

like image 770
stan Avatar asked Sep 02 '25 15:09

stan


1 Answers

Here's the link to a Maven plugin that makes the jdkHome of the active JDK toolchain available as javaHome property in Maven:

https://github.com/dadrus/javahome-resolver-maven-plugin

Disclaimer: I'm not the developer of that plugin.

Some caveats: The toolchains plugin has to be run before the javahome-resolver-plugin because the logic to determine the default JDK (i.e. the one used without toolchains) is not working as intended.

like image 95
Wolf Geldmacher Avatar answered Sep 05 '25 07:09

Wolf Geldmacher



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!