Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix detection of Maven versions plugin from parent pom.xml #2492

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 50 additions & 10 deletions plugins/maven/__tests__/maven.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -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 () => {
Expand Down Expand Up @@ -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(`<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<version>1.0.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.7</version>
</plugin>
</plugins>
</build>
</project>`);

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 () => {
Expand Down Expand Up @@ -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");
});
});
});
7 changes: 4 additions & 3 deletions plugins/maven/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,9 @@ export default class MavenPlugin implements IPlugin {
}

/** Detect whether the parent pom.xml has the versions-maven-plugin **/
private static async detectVersionMavenPlugin(): Promise<boolean> {
const pom = await getPom();
private static async detectVersionMavenPlugin(options: IMavenPluginOptions, auto: Auto): Promise<boolean> {
await maven.createEffectivePom(options, auto);
const pom = await getPom("target/output.xml");
const pomDom = new jsdom.JSDOM(pom.pomXml, { contentType: "text/xml" })
.window.document;
const versionsMavenPluginNode = pomDom.evaluate(
Expand Down Expand Up @@ -182,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) => {
Expand Down
20 changes: 20 additions & 0 deletions plugins/maven/src/maven.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,23 @@ 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`
);

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`);
}
}