Skip to content

Instantly share code, notes, and snippets.

@mathieucarbou
Last active February 22, 2024 11:08
Show Gist options
  • Save mathieucarbou/d7ce8fdf3e807e67ae07a9e79c66d82c to your computer and use it in GitHub Desktop.
Save mathieucarbou/d7ce8fdf3e807e67ae07a9e79c66d82c to your computer and use it in GitHub Desktop.
Maven toolchains.xml generator
#!/usr/bin/env groovy
import groovy.xml.MarkupBuilder
import java.nio.file.Files
import java.nio.file.Paths
def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
xml.doubleQuotes = true
xml.mkp.xmlDeclaration([version: '1.0', encoding: 'UTF-8'])
xml.toolchains([
'xmlns' : "http://maven.apache.org/TOOLCHAINS/1.1.0",
'xmlns:xsi' : "http://www.w3.org/2001/XMLSchema-instance",
'xsi:schemaLocation': "http://maven.apache.org/TOOLCHAINS/1.1.0 http://maven.apache.org/xsd/toolchains-1.1.0.xsd"
]) {
"jenv versions --bare".execute().text.eachLine { line ->
def homePath = Paths.get(System.getProperty('user.home'), '.jenv', 'versions', line.trim())
def output = "${homePath.resolve(Paths.get('bin', 'java'))} -version".execute().errorStream.getText('UTF-8')
toolchain() {
type('jdk')
provides() {
version(line.trim())
vendor(output.substring(0, output.indexOf(' ')).toLowerCase())
}
configuration() {
jdkHome(homePath.toString())
}
}
}
}
if (args.length && args[0] == 'install') {
def toolchains = Paths.get(System.getProperty('user.home'), '.m2', 'toolchains.xml')
Files.createDirectories(toolchains.getParent())
Files.write(toolchains, writer.toString().getBytes('UTF-8'))
} else {
println writer.toString()
}
@mathieucarbou
Copy link
Author

mathieucarbou commented Feb 12, 2020

Requirement

All your JDK must be managed by Jenv

=> http://www.jenv.be/

Usage

Run: ./toolchains.groovy to output the xml

Run: ./toolchains.groovy install to automatically update the Maven toolchains.xml located at ~/.m2/toolchains.xml

Example:

If you have:

~/Downloads 
❯  jenv versions
  system
* 1.8 (set by /Users/matc/.jenv/version)
  1.8.0.242
  10.0
  10.0.2
  11.0
  11.0.6
  13.0
  13.0.2
  9
  openjdk64-1.8.0.222
  openjdk64-1.8.0.232
  openjdk64-1.8.0.242
  openjdk64-10.0.2
  openjdk64-11.0.4
  openjdk64-11.0.6
  openjdk64-12.0.2
  openjdk64-13.0.2
  openjdk64-9

It will output:

<?xml version="1.0" encoding="UTF-8"?>
<toolchains xmlns="http://maven.apache.org/TOOLCHAINS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/TOOLCHAINS/1.1.0 http://maven.apache.org/xsd/toolchains-1.1.0.xsd">
  <toolchain>
    <type>jdk</type>
    <provides>
      <version>1.8</version>
      <vendor>openjdk</vendor>
    </provides>
    <configuration>
      <jdkHome>/Users/matc/.jenv/versions/1.8</jdkHome>
    </configuration>
  </toolchain>
  <toolchain>
    <type>jdk</type>
    <provides>
      <version>1.8.0.242</version>
      <vendor>openjdk</vendor>
    </provides>
    <configuration>
      <jdkHome>/Users/matc/.jenv/versions/1.8.0.242</jdkHome>
    </configuration>
  </toolchain>
  <toolchain>
    <type>jdk</type>
    <provides>
      <version>10.0</version>
      <vendor>openjdk</vendor>
    </provides>
    <configuration>
      <jdkHome>/Users/matc/.jenv/versions/10.0</jdkHome>
    </configuration>
  </toolchain>
  <toolchain>
    <type>jdk</type>
    <provides>
      <version>10.0.2</version>
      <vendor>openjdk</vendor>
    </provides>
    <configuration>
      <jdkHome>/Users/matc/.jenv/versions/10.0.2</jdkHome>
    </configuration>
  </toolchain>
  <toolchain>
    <type>jdk</type>
    <provides>
      <version>11.0</version>
      <vendor>openjdk</vendor>
    </provides>
    <configuration>
      <jdkHome>/Users/matc/.jenv/versions/11.0</jdkHome>
    </configuration>
  </toolchain>
  <toolchain>
    <type>jdk</type>
    <provides>
      <version>11.0.6</version>
      <vendor>openjdk</vendor>
    </provides>
    <configuration>
      <jdkHome>/Users/matc/.jenv/versions/11.0.6</jdkHome>
    </configuration>
  </toolchain>
  <toolchain>
    <type>jdk</type>
    <provides>
      <version>13.0</version>
      <vendor>openjdk</vendor>
    </provides>
    <configuration>
      <jdkHome>/Users/matc/.jenv/versions/13.0</jdkHome>
    </configuration>
  </toolchain>
  <toolchain>
    <type>jdk</type>
    <provides>
      <version>13.0.2</version>
      <vendor>openjdk</vendor>
    </provides>
    <configuration>
      <jdkHome>/Users/matc/.jenv/versions/13.0.2</jdkHome>
    </configuration>
  </toolchain>
  <toolchain>
    <type>jdk</type>
    <provides>
      <version>9</version>
      <vendor>openjdk</vendor>
    </provides>
    <configuration>
      <jdkHome>/Users/matc/.jenv/versions/9</jdkHome>
    </configuration>
  </toolchain>
  <toolchain>
    <type>jdk</type>
    <provides>
      <version>openjdk64-1.8.0.222</version>
      <vendor>openjdk</vendor>
    </provides>
    <configuration>
      <jdkHome>/Users/matc/.jenv/versions/openjdk64-1.8.0.222</jdkHome>
    </configuration>
  </toolchain>
  <toolchain>
    <type>jdk</type>
    <provides>
      <version>openjdk64-1.8.0.232</version>
      <vendor>openjdk</vendor>
    </provides>
    <configuration>
      <jdkHome>/Users/matc/.jenv/versions/openjdk64-1.8.0.232</jdkHome>
    </configuration>
  </toolchain>
  <toolchain>
    <type>jdk</type>
    <provides>
      <version>openjdk64-1.8.0.242</version>
      <vendor>openjdk</vendor>
    </provides>
    <configuration>
      <jdkHome>/Users/matc/.jenv/versions/openjdk64-1.8.0.242</jdkHome>
    </configuration>
  </toolchain>
  <toolchain>
    <type>jdk</type>
    <provides>
      <version>openjdk64-10.0.2</version>
      <vendor>openjdk</vendor>
    </provides>
    <configuration>
      <jdkHome>/Users/matc/.jenv/versions/openjdk64-10.0.2</jdkHome>
    </configuration>
  </toolchain>
  <toolchain>
    <type>jdk</type>
    <provides>
      <version>openjdk64-11.0.4</version>
      <vendor>openjdk</vendor>
    </provides>
    <configuration>
      <jdkHome>/Users/matc/.jenv/versions/openjdk64-11.0.4</jdkHome>
    </configuration>
  </toolchain>
  <toolchain>
    <type>jdk</type>
    <provides>
      <version>openjdk64-11.0.6</version>
      <vendor>openjdk</vendor>
    </provides>
    <configuration>
      <jdkHome>/Users/matc/.jenv/versions/openjdk64-11.0.6</jdkHome>
    </configuration>
  </toolchain>
  <toolchain>
    <type>jdk</type>
    <provides>
      <version>openjdk64-12.0.2</version>
      <vendor>openjdk</vendor>
    </provides>
    <configuration>
      <jdkHome>/Users/matc/.jenv/versions/openjdk64-12.0.2</jdkHome>
    </configuration>
  </toolchain>
  <toolchain>
    <type>jdk</type>
    <provides>
      <version>openjdk64-13.0.2</version>
      <vendor>openjdk</vendor>
    </provides>
    <configuration>
      <jdkHome>/Users/matc/.jenv/versions/openjdk64-13.0.2</jdkHome>
    </configuration>
  </toolchain>
  <toolchain>
    <type>jdk</type>
    <provides>
      <version>openjdk64-9</version>
      <vendor>openjdk</vendor>
    </provides>
    <configuration>
      <jdkHome>/Users/matc/.jenv/versions/openjdk64-9</jdkHome>
    </configuration>
  </toolchain>
</toolchains>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment