Created
December 19, 2019 15:18
-
-
Save leonard84/2af69ac675bc4121d8b2f08beb8b1e83 to your computer and use it in GitHub Desktop.
Update toolchains.xml using jabba managed jdks
This file contains hidden or 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
# check if jabba is installed | |
Get-Command -ErrorAction Stop jabba > $null | |
function Resolve-PathSymlink | |
{ | |
param( | |
[string] | |
$Path | |
) | |
$item = Get-Item $Path | |
if ($item.Target.Count -gt 0) | |
{ | |
$item.Target[0] | |
} | |
else | |
{ | |
$item.FullName | |
} | |
} | |
function Get-Jdk-Version | |
{ | |
param( | |
[string] | |
$Path | |
) | |
$output = & "$( $_.path )\bin\java" -version 2>&1 | |
$parts = $output -split '\n' | |
if ( $parts[0].StartsWith("Picked up JAVA_TOOL_OPTIONS")) | |
{ | |
$parts[1] | |
} | |
else | |
{ | |
$parts[0] | |
} | |
} | |
function Resolve-Version | |
{ | |
param( | |
[string] | |
$Version | |
) | |
if ($Version -match ".*version `"([.\d_-]+)`".*") | |
{ | |
$fullVersion = $Matches[1] | |
if ($fullVersion -match "(\d+\.\d+).*") | |
{ | |
$Matches[1] | |
} | |
else | |
{ | |
$fullVersion | |
} | |
} | |
else | |
{ | |
$Version | |
} | |
} | |
function Build-Toolchain | |
{ | |
param( | |
[PSObject] | |
$JDKs | |
) | |
$out = @" | |
<?xml version="1.0" encoding="UTF8"?> | |
<toolchains> | |
"@ | |
$JDKs | ForEach-Object { | |
$out += @" | |
<toolchain> | |
<type>jdk</type> | |
<provides> | |
<version>$($_.version)</version> | |
</provides> | |
<configuration> | |
<jdkHome>$($_.path)</jdkHome> | |
</configuration> | |
</toolchain> | |
"@ | |
} | |
$out += "`n</toolchains>" | |
$out | |
} | |
$jdkCandidates = jabba ls | |
$jdkWithPath = $jdkCandidates ` | |
| ForEach-Object { New-Object PsObject -Property @{ id = $_; path = ""; version = "" } } ` | |
| ForEach-Object { $_.path = Resolve-PathSymlink -Path (jabba which $_.id); $_ } | |
$jdkWithPath | Where-Object { !(Test-Path $_.path) } ` | |
| ForEach-Object { Write-Warning "Invalid JDK reference $( $_ )" } | |
$validJdks = $jdkWithPath | Where-Object { Test-Path $_.path } ` | |
| ForEach-Object { $_.version = Resolve-Version (Get-Jdk-Version $_.path); $_ } | |
$validJdks | Format-Table | |
# using -Encoding ascii, since the default utf8 encoding in powershell adds a BOM | |
# and since we shouldn't have any non-ascii characters in our output it is equivalent to utf8 without bom | |
Build-Toolchain -JDKs $validJdks | Out-File -FilePath "$($env:USERPROFILE)\.m2\toolchains.xml" -Encoding ascii | |
Write-Host "Updated toolchains.xml" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment