Skip to content

Instantly share code, notes, and snippets.

@beercanx
Created April 20, 2026 13:47
Show Gist options
  • Select an option

  • Save beercanx/24108ff6e6a61d4728e52ac4a90e9ca3 to your computer and use it in GitHub Desktop.

Select an option

Save beercanx/24108ff6e6a61d4728e52ac4a90e9ca3 to your computer and use it in GitHub Desktop.
Generating Maven Toolchains from Intellij sourced JDKs

Generating Maven Toolchains from Intellij sourced JDKs

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()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment