4

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.

cb4
  • 6,689
  • 7
  • 45
  • 57
stan
  • 95
  • 1
  • 9
  • What about: http://maven.apache.org/plugins/maven-jlink-plugin/ ? – khmarbaise Nov 06 '17 at 10:26
  • Potentially, although I've not tried it yet. So far I've been put off by the need to reorganise my project, as that plugin requires/expects a multi-module Maven project, and this one is a single module only. I'd rather keep things as simple as possible. I'll take a closer look though. Also...I expect that plugin requires addition of project modules, while in reality I'm using *jlink* to create a standalone runtime, which I then incorporate into my deployment build, which proves simpler for reducing runtime size when deploying a legacy application comprising many non-modular JARs. – stan Nov 06 '17 at 10:59
  • I'm currently improving this plugin and one of the things is having a single module which will be created a run time image... – khmarbaise Nov 06 '17 at 11:21
  • Excellent, that sounds like a good feature to include, @khmarbaise, and will no doubt appease a lot of developers, especially during the Java 9 transition phase they'll all have to go through. I look forward to it! – stan Nov 06 '17 at 12:02

1 Answers1

3

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.

  • Thank you, @Wolf, your help is appreciated, and seems likely to resolve this conundrum. The change to JDK9+ has been a long road for many developers, the lack of streamlined support for so long from all mainstream build tools has been challenging. As a result of this I transitioned to Gradle to give myself finer control over the build process, and reduce the excessive verbosity of using a Maven build configuration. – stan Jan 16 '20 at 12:16
  • So this seems like the way to go, although I've used the more recent [fork](https://github.com/diamondq/javahome-resolver-maven-plugin) to make it work. – stan Dec 17 '21 at 18:33