javamavenmaven-toolchains-plugin

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.


Solution

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