Skip to content

Commit d229d2e

Browse files
brunoborgesCopilotCopilot
authored
Backport #1111: Preserve Maven toolchains across repeated setup-java runs (#1099) (#1113)
* Preserve Maven toolchains across repeated setup-java runs (#1099) Backport of #1111 to releases/v5. 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> * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent bbf0f69 commit d229d2e

4 files changed

Lines changed: 101 additions & 68 deletions

File tree

‎__tests__/toolchains.test.ts‎

Lines changed: 73 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ describe('toolchains tests', () => {
4646

4747
await toolchains.createToolchainsSettings({
4848
jdkInfo,
49-
settingsDirectory: altHome,
50-
overwriteSettings: true
49+
settingsDirectory: altHome
5150
});
5251

5352
expect(fs.existsSync(m2Dir)).toBe(false);
@@ -95,8 +94,7 @@ describe('toolchains tests', () => {
9594

9695
await toolchains.createToolchainsSettings({
9796
jdkInfo,
98-
settingsDirectory: m2Dir,
99-
overwriteSettings: true
97+
settingsDirectory: m2Dir
10098
});
10199

102100
expect(fs.existsSync(m2Dir)).toBe(true);
@@ -177,8 +175,7 @@ describe('toolchains tests', () => {
177175

178176
await toolchains.createToolchainsSettings({
179177
jdkInfo,
180-
settingsDirectory: m2Dir,
181-
overwriteSettings: true
178+
settingsDirectory: m2Dir
182179
});
183180

184181
expect(fs.existsSync(m2Dir)).toBe(true);
@@ -263,8 +260,7 @@ describe('toolchains tests', () => {
263260

264261
await toolchains.createToolchainsSettings({
265262
jdkInfo,
266-
settingsDirectory: m2Dir,
267-
overwriteSettings: true
263+
settingsDirectory: m2Dir
268264
});
269265

270266
expect(fs.existsSync(m2Dir)).toBe(true);
@@ -341,8 +337,7 @@ describe('toolchains tests', () => {
341337

342338
await toolchains.createToolchainsSettings({
343339
jdkInfo,
344-
settingsDirectory: m2Dir,
345-
overwriteSettings: true
340+
settingsDirectory: m2Dir
346341
});
347342

348343
expect(fs.existsSync(m2Dir)).toBe(true);
@@ -412,8 +407,7 @@ describe('toolchains tests', () => {
412407

413408
await toolchains.createToolchainsSettings({
414409
jdkInfo,
415-
settingsDirectory: m2Dir,
416-
overwriteSettings: true
410+
settingsDirectory: m2Dir
417411
});
418412

419413
expect(fs.existsSync(m2Dir)).toBe(true);
@@ -505,8 +499,7 @@ describe('toolchains tests', () => {
505499

506500
await toolchains.createToolchainsSettings({
507501
jdkInfo,
508-
settingsDirectory: m2Dir,
509-
overwriteSettings: true
502+
settingsDirectory: m2Dir
510503
});
511504

512505
expect(fs.existsSync(m2Dir)).toBe(true);
@@ -565,8 +558,7 @@ describe('toolchains tests', () => {
565558

566559
await toolchains.createToolchainsSettings({
567560
jdkInfo,
568-
settingsDirectory: m2Dir,
569-
overwriteSettings: true
561+
settingsDirectory: m2Dir
570562
});
571563

572564
expect(fs.existsSync(m2Dir)).toBe(true);
@@ -624,8 +616,7 @@ describe('toolchains tests', () => {
624616

625617
await toolchains.createToolchainsSettings({
626618
jdkInfo,
627-
settingsDirectory: m2Dir,
628-
overwriteSettings: true
619+
settingsDirectory: m2Dir
629620
});
630621

631622
expect(fs.existsSync(m2Dir)).toBe(true);
@@ -708,8 +699,7 @@ describe('toolchains tests', () => {
708699

709700
await toolchains.createToolchainsSettings({
710701
jdkInfo,
711-
settingsDirectory: m2Dir,
712-
overwriteSettings: true
702+
settingsDirectory: m2Dir
713703
});
714704

715705
expect(fs.existsSync(m2Dir)).toBe(true);
@@ -788,8 +778,7 @@ describe('toolchains tests', () => {
788778

789779
await toolchains.createToolchainsSettings({
790780
jdkInfo,
791-
settingsDirectory: m2Dir,
792-
overwriteSettings: true
781+
settingsDirectory: m2Dir
793782
});
794783

795784
expect(fs.existsSync(m2Dir)).toBe(true);
@@ -814,7 +803,7 @@ describe('toolchains tests', () => {
814803
).toEqual(result);
815804
}, 100000);
816805

817-
it('does not overwrite existing toolchains.xml files', async () => {
806+
it('extends existing toolchains.xml files instead of overwriting them', async () => {
818807
const jdkInfo = {
819808
version: '17',
820809
vendor: 'Eclipse Temurin',
@@ -843,13 +832,20 @@ describe('toolchains tests', () => {
843832

844833
await toolchains.createToolchainsSettings({
845834
jdkInfo,
846-
settingsDirectory: m2Dir,
847-
overwriteSettings: false
835+
settingsDirectory: m2Dir
848836
});
849837

850838
expect(fs.existsSync(m2Dir)).toBe(true);
851839
expect(fs.existsSync(toolchainsFile)).toBe(true);
852-
expect(fs.readFileSync(toolchainsFile, 'utf-8')).toEqual(originalFile);
840+
841+
const updated = fs.readFileSync(toolchainsFile, 'utf-8');
842+
// The pre-existing (Sun 1.6) toolchain must be preserved ...
843+
expect(updated).toContain('<id>sun_1.6</id>');
844+
expect(updated).toContain('<jdkHome>/opt/jdk/sun/1.6</jdkHome>');
845+
// ... and the newly installed JDK must be included in the merged result.
846+
expect(updated).toContain('<id>temurin_17</id>');
847+
expect(updated).toContain('<vendor>Eclipse Temurin</vendor>');
848+
expect(updated).toContain(`<jdkHome>${jdkInfo.jdkHome}</jdkHome>`);
853849
}, 100000);
854850

855851
it('generates valid toolchains.xml with minimal configuration', () => {
@@ -914,4 +910,55 @@ describe('toolchains tests', () => {
914910
)
915911
);
916912
}, 100000);
913+
914+
it('preserves toolchains from previous executions across multiple setup-java runs', async () => {
915+
// Regression test for https://github.com/actions/setup-java/issues/1099
916+
// Running setup-java several times in the same job (e.g. multiple steps / multiple
917+
// java-version entries) must accumulate every JDK in toolchains.xml rather
918+
// than replacing previously registered entries.
919+
jest.spyOn(core, 'getInput').mockImplementation((name: string) => {
920+
if (name === 'settings-path') return m2Dir;
921+
return '';
922+
});
923+
924+
const runs = [
925+
{
926+
version: '8',
927+
distributionName: 'temurin',
928+
id: 'temurin_8',
929+
jdkHome: '/opt/hostedtoolcache/Java_Temurin-Hotspot_jdk/8.0.1-12/x64'
930+
},
931+
{
932+
version: '11',
933+
distributionName: 'temurin',
934+
id: 'temurin_11',
935+
jdkHome: '/opt/hostedtoolcache/Java_Temurin-Hotspot_jdk/11.0.1-12/x64'
936+
},
937+
{
938+
version: '17',
939+
distributionName: 'temurin',
940+
id: 'temurin_17',
941+
jdkHome: '/opt/hostedtoolcache/Java_Temurin-Hotspot_jdk/17.0.1-12/x64'
942+
}
943+
];
944+
945+
for (const run of runs) {
946+
await toolchains.configureToolchains(
947+
run.version,
948+
run.distributionName,
949+
run.jdkHome,
950+
undefined
951+
);
952+
}
953+
954+
expect(fs.existsSync(toolchainsFile)).toBe(true);
955+
const contents = fs.readFileSync(toolchainsFile, 'utf-8');
956+
957+
for (const run of runs) {
958+
expect(contents).toContain(`<id>${run.id}</id>`);
959+
expect(contents).toContain(`<jdkHome>${run.jdkHome}</jdkHome>`);
960+
}
961+
// Exactly one <toolchain> entry per run – no duplicates, none dropped.
962+
expect((contents.match(/<toolchain>/g) || []).length).toBe(runs.length);
963+
}, 100000);
917964
});

‎dist/setup/index.js‎

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81751,37 +81751,34 @@ const path = __importStar(__nccwpck_require__(16928));
8175181751
const core = __importStar(__nccwpck_require__(37484));
8175281752
const io = __importStar(__nccwpck_require__(94994));
8175381753
const constants = __importStar(__nccwpck_require__(27242));
81754-
const util_1 = __nccwpck_require__(54527);
8175581754
const xmlbuilder2_1 = __nccwpck_require__(54697);
8175681755
function configureToolchains(version, distributionName, jdkHome, toolchainId) {
8175781756
return __awaiter(this, void 0, void 0, function* () {
8175881757
const vendor = core.getInput(constants.INPUT_MVN_TOOLCHAIN_VENDOR) || distributionName;
8175981758
const id = toolchainId || `${vendor}_${version}`;
8176081759
const settingsDirectory = core.getInput(constants.INPUT_SETTINGS_PATH) ||
8176181760
path.join(os.homedir(), constants.M2_DIR);
81762-
const overwriteSettings = (0, util_1.getBooleanInput)(constants.INPUT_OVERWRITE_SETTINGS, true);
8176381761
yield createToolchainsSettings({
8176481762
jdkInfo: {
8176581763
version,
8176681764
vendor,
8176781765
id,
8176881766
jdkHome
8176981767
},
81770-
settingsDirectory,
81771-
overwriteSettings
81768+
settingsDirectory
8177281769
});
8177381770
});
8177481771
}
8177581772
exports.configureToolchains = configureToolchains;
81776-
function createToolchainsSettings({ jdkInfo, settingsDirectory, overwriteSettings }) {
81773+
function createToolchainsSettings({ jdkInfo, settingsDirectory }) {
8177781774
return __awaiter(this, void 0, void 0, function* () {
8177881775
core.info(`Creating ${constants.MVN_TOOLCHAINS_FILE} for JDK version ${jdkInfo.version} from ${jdkInfo.vendor}`);
8177981776
// when an alternate m2 location is specified use only that location (no .m2 directory)
8178081777
// otherwise use the home/.m2/ path
8178181778
yield io.mkdirP(settingsDirectory);
8178281779
const originalToolchains = yield readExistingToolchainsFile(settingsDirectory);
8178381780
const updatedToolchains = generateToolchainDefinition(originalToolchains, jdkInfo.version, jdkInfo.vendor, jdkInfo.id, jdkInfo.jdkHome);
81784-
yield writeToolchainsFileToDisk(settingsDirectory, updatedToolchains, overwriteSettings);
81781+
yield writeToolchainsFileToDisk(settingsDirectory, updatedToolchains);
8178581782
});
8178681783
}
8178781784
exports.createToolchainsSettings = createToolchainsSettings;
@@ -81867,19 +81864,21 @@ function readExistingToolchainsFile(directory) {
8186781864
return '';
8186881865
});
8186981866
}
81870-
function writeToolchainsFileToDisk(directory, settings, overwriteSettings) {
81867+
function writeToolchainsFileToDisk(directory, settings) {
8187181868
return __awaiter(this, void 0, void 0, function* () {
8187281869
const location = path.join(directory, constants.MVN_TOOLCHAINS_FILE);
8187381870
const settingsExists = fs.existsSync(location);
81874-
if (settingsExists && overwriteSettings) {
81875-
core.info(`Overwriting existing file ${location}`);
81876-
}
81877-
else if (!settingsExists) {
81878-
core.info(`Writing to ${location}`);
81871+
// The toolchains file is produced by a non-destructive merge (existing JDK,
81872+
// custom, and non-jdk toolchains are preserved – see generateToolchainDefinition),
81873+
// so it is always safe to write it. Unlike settings.xml, it is therefore not
81874+
// gated behind the `overwrite-settings` input; that would prevent subsequent
81875+
// setup-java runs from registering additional JDKs and silently drop the
81876+
// toolchain entries created by earlier runs.
81877+
if (settingsExists) {
81878+
core.info(`Updating existing file ${location}`);
8187981879
}
8188081880
else {
81881-
core.info(`Skipping generation of ${location} because file already exists and overwriting is not enabled`);
81882-
return;
81881+
core.info(`Writing to ${location}`);
8188381882
}
8188481883
return fs.writeFileSync(location, settings, {
8188581884
encoding: 'utf-8',

‎docs/advanced-usage.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,8 @@ The `setup-java` action generates a basic [Maven Toolchains declaration](https:/
684684
### Installing Multiple JDKs With Toolchains
685685
Subsequent calls to `setup-java` with distinct distribution and version parameters will continue to extend the toolchains declaration and make all specified Java versions available.
686686

687+
Toolchain entries are always merged non-destructively: existing JDK, custom, and user-managed toolchains are preserved, and only an entry with the exact same `type` and `provides.id` is replaced. This behavior is independent of the `overwrite-settings` input, which only controls regeneration of `settings.xml`. As a result, running `setup-java` several times in the same job (for example in multiple steps or with multiple `java-version` values) accumulates every JDK in `toolchains.xml` instead of dropping previously registered entries.
688+
687689
```yaml
688690
steps:
689691
- uses: actions/setup-java@v5

‎src/toolchains.ts‎

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import * as core from '@actions/core';
55
import * as io from '@actions/io';
66
import * as constants from './constants';
77

8-
import {getBooleanInput} from './util';
98
import {create as xmlCreate} from 'xmlbuilder2';
109

1110
interface JdkInfo {
@@ -27,10 +26,6 @@ export async function configureToolchains(
2726
const settingsDirectory =
2827
core.getInput(constants.INPUT_SETTINGS_PATH) ||
2928
path.join(os.homedir(), constants.M2_DIR);
30-
const overwriteSettings = getBooleanInput(
31-
constants.INPUT_OVERWRITE_SETTINGS,
32-
true
33-
);
3429

3530
await createToolchainsSettings({
3631
jdkInfo: {
@@ -39,19 +34,16 @@ export async function configureToolchains(
3934
id,
4035
jdkHome
4136
},
42-
settingsDirectory,
43-
overwriteSettings
37+
settingsDirectory
4438
});
4539
}
4640

4741
export async function createToolchainsSettings({
4842
jdkInfo,
49-
settingsDirectory,
50-
overwriteSettings
43+
settingsDirectory
5144
}: {
5245
jdkInfo: JdkInfo;
5346
settingsDirectory: string;
54-
overwriteSettings: boolean;
5547
}) {
5648
core.info(
5749
`Creating ${constants.MVN_TOOLCHAINS_FILE} for JDK version ${jdkInfo.version} from ${jdkInfo.vendor}`
@@ -68,11 +60,7 @@ export async function createToolchainsSettings({
6860
jdkInfo.id,
6961
jdkInfo.jdkHome
7062
);
71-
await writeToolchainsFileToDisk(
72-
settingsDirectory,
73-
updatedToolchains,
74-
overwriteSettings
75-
);
63+
await writeToolchainsFileToDisk(settingsDirectory, updatedToolchains);
7664
}
7765

7866
// only exported for testing purposes
@@ -172,22 +160,19 @@ async function readExistingToolchainsFile(directory: string) {
172160
return '';
173161
}
174162

175-
async function writeToolchainsFileToDisk(
176-
directory: string,
177-
settings: string,
178-
overwriteSettings: boolean
179-
) {
163+
async function writeToolchainsFileToDisk(directory: string, settings: string) {
180164
const location = path.join(directory, constants.MVN_TOOLCHAINS_FILE);
181165
const settingsExists = fs.existsSync(location);
182-
if (settingsExists && overwriteSettings) {
183-
core.info(`Overwriting existing file ${location}`);
184-
} else if (!settingsExists) {
185-
core.info(`Writing to ${location}`);
166+
// The toolchains file is produced by a non-destructive merge (existing JDK,
167+
// custom, and non-jdk toolchains are preserved – see generateToolchainDefinition),
168+
// so it is always safe to write it. Unlike settings.xml, it is therefore not
169+
// gated behind the `overwrite-settings` input; that would prevent subsequent
170+
// setup-java runs from registering additional JDKs and silently drop the
171+
// toolchain entries created by earlier runs.
172+
if (settingsExists) {
173+
core.info(`Updating existing file ${location}`);
186174
} else {
187-
core.info(
188-
`Skipping generation of ${location} because file already exists and overwriting is not enabled`
189-
);
190-
return;
175+
core.info(`Writing to ${location}`);
191176
}
192177

193178
return fs.writeFileSync(location, settings, {

0 commit comments

Comments
 (0)