I'm lazy, I use Intellij feature to find and download required JDKs and update with patches.
So Maven has a concept for toolchains, so I want something to generate a fresh ~/.m2/toolchains.xml file when there's changes.
function GenerateMavenToolchains() {
$toolchainsDocument = [xml]@'
<?xml version="1.0" encoding="UTF-8"?>
<toolchains>
</toolchains>
'@
Get-ChildItem -Path "$HOME\.jdks\*.intellij"
| Where-Object -Property Name -Match 'corretto'
| Add-Member -PassThru -MemberType ScriptProperty -Name 'Intellij' -Value {
(Get-Content $this -Raw | ConvertFrom-JSON)
}
| ForEach-Object {
$toolchainsDocument.DocumentElement.AppendChild($toolchainsDocument.ImportNode(([xml]@"
<toolchain>
<type>jdk</type>
<provides>
<version>$($_.Intellij.jdk_version_major)</version>
<vendor>$($_.Intellij.vendor)</vendor>
<product>$($_.Intellij.product)</product>
</provides>
<configuration>
<jdkHome>$($_.DirectoryName)\$($_.Intellij.packages.install_folder_name)</jdkHome>
</configuration>
</toolchain>
"@).toolchain, $true)) | Out-Null;
}
$stringWriter = [System.IO.StringWriter]::new()
$xmlWriter = [System.Xml.XmlTextWriter]::new($stringWriter)
$xmlWriter.Formatting = "indented"
$toolchainsDocument.WriteTo($xmlWriter)
$xmlWriter.Flush()
$stringWriter.Flush()
Write-Host $stringWriter.ToString()
}