Skip to content

Preserve Maven toolchains across repeated setup-java runs (#1099)#1111

Merged
brunoborges merged 3 commits into
mainfrom
brunoborges-fix-toolchain-overwrite-issue-1099
Jul 14, 2026
Merged

Preserve Maven toolchains across repeated setup-java runs (#1099)#1111
brunoborges merged 3 commits into
mainfrom
brunoborges-fix-toolchain-overwrite-issue-1099

Conversation

@brunoborges

Copy link
Copy Markdown
Contributor

Summary

Fixes #1099. Running setup-java multiple times in the same job (matrix builds or multiple java-version values) could drop toolchain entries registered by earlier runs, leaving only one JDK in ~/.m2/toolchains.xml.

Root cause

Toolchains generation was gated behind the overwrite-settings input. When overwrite-settings: false, writeToolchainsFileToDisk skipped writing entirely if the file already existed — so a second setup-java run never persisted its JDK, silently discarding the merged result.

This gating was inappropriate because:

  • overwrite-settings is documented (in action.yml) to control regeneration of settings.xml only.
  • generateToolchainDefinition already performs a non-destructive merge: existing JDK, custom, and user-managed toolchains are preserved, and only an entry with the exact same type + provides.id is replaced.

So writing the toolchains file is always safe and should not be blocked.

Changes

  • src/toolchains.ts: Remove overwriteSettings from configureToolchains, createToolchainsSettings, and writeToolchainsFileToDisk. The toolchains file is now always written (non-destructively). settings.xml behavior in auth.ts is unchanged.
  • __tests__/toolchains.test.ts: Update call sites, rewrite the "does not overwrite" test to assert non-destructive extension, and add a regression test that runs configureToolchains three times and asserts every JDK is preserved with no duplicates.
  • docs/advanced-usage.md: Clarify that the merge is non-destructive and independent of overwrite-settings.
  • dist/setup/index.js: Rebuilt.

Testing

  • npm run build, npm run format-check, npm run lint all pass.
  • npm test: 909 passing, including the new regression test.

Co-authored-by: Copilot App 223556219+Copilot@users.noreply.github.com

Toolchain generation was gated behind the `overwrite-settings` input,
which is documented to control only regeneration of `settings.xml`.
Because `generateToolchainDefinition` already performs a non-destructive
merge (existing JDK, custom, and user-managed toolchains are preserved,
and only an entry with the same `type` + `provides.id` is replaced),
skipping the write when `overwrite-settings: false` caused later
setup-java executions to drop toolchain entries registered by earlier
runs.

Decouple toolchains generation from `overwrite-settings`: the toolchains
file is now always written, so consecutive runs accumulate every JDK.
`settings.xml` behavior (auth.ts) is unchanged.

- src/toolchains.ts: drop overwriteSettings from configureToolchains /
  createToolchainsSettings / writeToolchainsFileToDisk; always write.
- __tests__/toolchains.test.ts: update call sites, rewrite the
  "does not overwrite" test to assert non-destructive extension, and add
  a regression test for consecutive configureToolchains executions.
- docs/advanced-usage.md: clarify merge is non-destructive and
  independent of overwrite-settings.
- dist/setup/index.js: rebuilt.

Fixes #1099

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 0bcec457-b86b-4902-b6e6-6dfd6b2570f7
Copilot AI review requested due to automatic review settings July 13, 2026 18:59
@brunoborges brunoborges requested a review from a team as a code owner July 13, 2026 18:59

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a regression where running actions/setup-java multiple times within the same job could cause Maven ~/.m2/toolchains.xml to lose previously-registered JDK toolchain entries. It makes toolchains persistence independent of the overwrite-settings input (which is intended to control settings.xml only), relying on the existing non-destructive merge behavior when generating toolchains.

Changes:

  • Always write toolchains.xml after merging, regardless of overwrite-settings.
  • Update and expand toolchains tests, including a multi-run regression test ensuring all JDK entries are preserved without duplicates.
  • Clarify the non-destructive merge behavior in advanced usage docs; rebuild dist.
Show a summary per file
File Description
src/toolchains.ts Removes overwrite-settings gating so toolchains are always persisted after merge.
__tests__/toolchains.test.ts Updates call sites and adds regression coverage for repeated runs preserving all toolchains.
docs/advanced-usage.md Documents that toolchains merging is non-destructive and independent of overwrite-settings.
dist/setup/index.js Rebuilt distribution output reflecting the source changes.

Review details

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 3/4 changed files
  • Comments generated: 2
  • Review effort level: Low

Comment thread docs/advanced-usage.md Outdated
Comment thread __tests__/toolchains.test.ts Outdated
brunoborges and others added 2 commits July 13, 2026 15:17
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@brunoborges

Copy link
Copy Markdown
Contributor Author

@lmvysakh can you please review this?

@mhoffrog

Copy link
Copy Markdown
Contributor

@brunoborges This PR will raise a conflict to my #1109 one I guess.
=> #1109 will have to await this one being merged. I will take care of updating myone after this one has been merged.
=> I will put #1109 to draft now and will re-wake it later.

@lmvysakh

Copy link
Copy Markdown
Contributor

Hy @brunoborges ,

Reviewed and Approved PR.

@brunoborges brunoborges merged commit 71cfe8e into main Jul 14, 2026
409 checks passed
@brunoborges brunoborges deleted the brunoborges-fix-toolchain-overwrite-issue-1099 branch July 14, 2026 11:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Further executions of setup-java overwrite previous toolchain entries for different JDK versions

5 participants