Last active
February 22, 2024 11:08
-
-
Save mathieucarbou/d7ce8fdf3e807e67ae07a9e79c66d82c to your computer and use it in GitHub Desktop.
Maven toolchains.xml generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Requirement
All your JDK must be managed by Jenv
=> http://www.jenv.be/
Usage
Run:
./toolchains.groovy
to output the xmlRun:
./toolchains.groovy install
to automatically update the Maventoolchains.xml
located at~/.m2/toolchains.xml
Example:
If you have:
It will output: