From 9f0b1632c1ce703835eaeaf087343f42fe081c9f Mon Sep 17 00:00:00 2001 From: neloydas Date: Sat, 15 Feb 2025 01:35:28 +0100 Subject: [PATCH 1/2] Fix detection of Maven versions plugin from parent pom.xml --- plugins/maven/src/index.ts | 3 ++- plugins/maven/src/maven.ts | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/plugins/maven/src/index.ts b/plugins/maven/src/index.ts index 2fc129cc8..f877e8be6 100644 --- a/plugins/maven/src/index.ts +++ b/plugins/maven/src/index.ts @@ -112,7 +112,8 @@ export default class MavenPlugin implements IPlugin { /** Detect whether the parent pom.xml has the versions-maven-plugin **/ private static async detectVersionMavenPlugin(): Promise { - const pom = await getPom(); + await maven.createEffectivePom(pluginOptions, Auto); + const pom = await getPom("target/output.xml"); const pomDom = new jsdom.JSDOM(pom.pomXml, { contentType: "text/xml" }) .window.document; const versionsMavenPluginNode = pomDom.evaluate( diff --git a/plugins/maven/src/maven.ts b/plugins/maven/src/maven.ts index bd04c9c7d..165de86e2 100644 --- a/plugins/maven/src/maven.ts +++ b/plugins/maven/src/maven.ts @@ -49,3 +49,18 @@ export async function updatePoms( await execPromise("git", ["commit", "-am", message, "--no-verify"]); } + +/** Create effective pom file with maven */ +export async function createEffectivePom( + options: IMavenPluginOptions, + auto: Auto +) { + auto.logger.verbose.info( + `Creating effective-pom file in target directory` + ); + + await executeMaven(options, [ + "help:effective-pom", + "-Doutput='target/output.xml'" + ]); +} From 4d1450f03e9484aad4822b1bb3d46a12e6fbf936 Mon Sep 17 00:00:00 2001 From: neloydas Date: Sat, 22 Feb 2025 01:08:00 +0100 Subject: [PATCH 2/2] added tests and fixed other test cases --- plugins/maven/__tests__/maven.test.ts | 60 ++++++++++++++++++++++----- plugins/maven/src/index.ts | 6 +-- plugins/maven/src/maven.ts | 13 ++++-- 3 files changed, 62 insertions(+), 17 deletions(-) diff --git a/plugins/maven/__tests__/maven.test.ts b/plugins/maven/__tests__/maven.test.ts index a338e9e76..9cf46bf04 100644 --- a/plugins/maven/__tests__/maven.test.ts +++ b/plugins/maven/__tests__/maven.test.ts @@ -270,9 +270,13 @@ describe("maven", () => { await hooks.version.promise({ bump: Auto.SEMVER.patch }); const call = exec.mock.calls[0][1]; - expect(call).toContain("tag"); - expect(call).toContain("v1.0.0"); - expect(call).toContain('"Update version to v1.0.0"'); + expect(call).toContain("help:effective-pom"); + expect(call).toContain("-Doutput=target/output.xml"); + + const secondCall = exec.mock.calls[1][1]; + expect(secondCall).toContain("tag"); + expect(secondCall).toContain("v1.0.0"); + expect(secondCall).toContain('"Update version to v1.0.0"'); }); test("should version release", async () => { @@ -290,9 +294,13 @@ describe("maven", () => { await hooks.version.promise({ bump: Auto.SEMVER.patch }); const call = exec.mock.calls[0][1]; - expect(call).toContain("tag"); - expect(call).toContain("v1.0.1"); - expect(call).toContain('"Update version to v1.0.1"'); + expect(call).toContain("help:effective-pom"); + expect(call).toContain("-Doutput=target/output.xml"); + + const secondCall = exec.mock.calls[1][1]; + expect(secondCall).toContain("tag"); + expect(secondCall).toContain("v1.0.1"); + expect(secondCall).toContain('"Update version to v1.0.1"'); }); test("should replace the previousVersion with the newVersion", async () => { @@ -359,9 +367,37 @@ describe("maven", () => { ); const call = exec.mock.calls[0][1]; - expect(call).toContain("versions:set"); - expect(call).toContain("-DgenerateBackupPoms=false"); - expect(call).toContain("-DnewVersion=1.0.0"); + expect(call).toContain("help:effective-pom"); + expect(call).toContain("-Doutput=target/output.xml"); + + const secondCall = exec.mock.calls[1][1]; + expect(secondCall).toContain("versions:set"); + expect(secondCall).toContain("-DgenerateBackupPoms=false"); + expect(secondCall).toContain("-DnewVersion=1.0.0"); + }); + + test("should create effective POM file successfully", async () => { + mockReadFile(` + 1.0.0-SNAPSHOT + + + + org.codehaus.mojo + versions-maven-plugin + 2.7 + + + + `); + + await hooks.beforeRun.promise({} as any); + + const call = exec.mock.calls[0][1]; + expect(call).toContain("help:effective-pom"); + expect(call).toContain("-Doutput=target/output.xml"); }); test("should replace the parent previousVersion with the newVersion", async () => { @@ -468,7 +504,11 @@ describe("maven", () => { await hooks.publish.promise({ bump: Auto.SEMVER.patch }); - expect(exec.mock.calls[0][1]).toContain("deploy"); + const call = exec.mock.calls[0][1]; + expect(call).toContain("help:effective-pom"); + expect(call).toContain("-Doutput=target/output.xml"); + + expect(exec.mock.calls[1][1]).toContain("deploy"); }); }); }); diff --git a/plugins/maven/src/index.ts b/plugins/maven/src/index.ts index f877e8be6..bcb69fa0c 100644 --- a/plugins/maven/src/index.ts +++ b/plugins/maven/src/index.ts @@ -111,8 +111,8 @@ export default class MavenPlugin implements IPlugin { } /** Detect whether the parent pom.xml has the versions-maven-plugin **/ - private static async detectVersionMavenPlugin(): Promise { - await maven.createEffectivePom(pluginOptions, Auto); + private static async detectVersionMavenPlugin(options: IMavenPluginOptions, auto: Auto): Promise { + await maven.createEffectivePom(options, auto); const pom = await getPom("target/output.xml"); const pomDom = new jsdom.JSDOM(pom.pomXml, { contentType: "text/xml" }) .window.document; @@ -183,7 +183,7 @@ export default class MavenPlugin implements IPlugin { this.snapshotRelease = true; } - this.versionsMavenPlugin = await MavenPlugin.detectVersionMavenPlugin(); + this.versionsMavenPlugin = await MavenPlugin.detectVersionMavenPlugin(this.options, auto); }); auto.hooks.validateConfig.tapPromise(this.name, async (name, options) => { diff --git a/plugins/maven/src/maven.ts b/plugins/maven/src/maven.ts index 165de86e2..d635ae449 100644 --- a/plugins/maven/src/maven.ts +++ b/plugins/maven/src/maven.ts @@ -59,8 +59,13 @@ export async function createEffectivePom( `Creating effective-pom file in target directory` ); - await executeMaven(options, [ - "help:effective-pom", - "-Doutput='target/output.xml'" - ]); + try { + await executeMaven(options, [ + "help:effective-pom", + "-Doutput=target/output.xml" + ]); + } catch (error) { + auto.logger.verbose.error(`Failed to create effective pom: ${error.message}`); + throw new Error(`Failed to create effective pom in target directory`); + } }