From 5c1a23d701cb946294899a4d315eab447585649a Mon Sep 17 00:00:00 2001 From: Marquis Wong Date: Wed, 16 Nov 2022 10:06:53 -0600 Subject: [PATCH] Add version prefixes option to released-version goal Allow running the released-version goal with optional parameter versionPrefix, which filters the matched released versions to versions that begin with that prefix. For example, if the released versions are 1.3.1, 1.3.2, 1.4.1, 1.4.2, and you run `mvn build-helper:released-version` normally, it will find version 1.4.2 as the latest released version. This commit makes it so if you give it the parameter versionPrefix=1.3, then it will find 1.3.2 instead. You can also pass in the versionPrefix via a property: mvn build-helper:released-version -DreleasedVersion.versionPrefix=1.3 --- .../invoker.properties | 3 + .../released-version-version-prefix/pom.xml | 68 +++++++++++++++++++ .../settings-maven-central.xml | 27 ++++++++ .../verify.groovy | 12 ++++ .../mojo/buildhelper/ReleasedVersionMojo.java | 10 ++- 5 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 src/it/released-version-version-prefix/invoker.properties create mode 100644 src/it/released-version-version-prefix/pom.xml create mode 100644 src/it/released-version-version-prefix/settings-maven-central.xml create mode 100644 src/it/released-version-version-prefix/verify.groovy diff --git a/src/it/released-version-version-prefix/invoker.properties b/src/it/released-version-version-prefix/invoker.properties new file mode 100644 index 00000000..a48f6073 --- /dev/null +++ b/src/it/released-version-version-prefix/invoker.properties @@ -0,0 +1,3 @@ +invoker.goals = test +invoker.buildResult = success +invoker.settingsFile = src/it/released-version-existing-asset/settings-maven-central.xml diff --git a/src/it/released-version-version-prefix/pom.xml b/src/it/released-version-version-prefix/pom.xml new file mode 100644 index 00000000..2f26102a --- /dev/null +++ b/src/it/released-version-version-prefix/pom.xml @@ -0,0 +1,68 @@ + + 4.0.0 + + + org.apache.continuum + continuum + 1.0-SNAPSHOT + build-helper-maven-plugin-released-version-it-mojo-parent + + + package + + + @project.groupId@ + @project.artifactId@ + @project.version@ + + + released-version + + released-version + + + 1.3 + myReleasedVersion + + + + + + maven-antrun-plugin + + + mk-target-dir + compile + + run + + + + + + + + + + echo-released-version + test + + run + + + + myReleasedVersion.version=${myReleasedVersion.version} + myReleasedVersion.majorVersion=${myReleasedVersion.majorVersion} + myReleasedVersion.minorVersion=${myReleasedVersion.minorVersion} + myReleasedVersion.incrementalVersion=${myReleasedVersion.incrementalVersion} + + + + + + + + + diff --git a/src/it/released-version-version-prefix/settings-maven-central.xml b/src/it/released-version-version-prefix/settings-maven-central.xml new file mode 100644 index 00000000..e112181a --- /dev/null +++ b/src/it/released-version-version-prefix/settings-maven-central.xml @@ -0,0 +1,27 @@ + + + + + default + + + central + https://repo1.maven.org/maven2/ + default + + + + + central + https://repo1.maven.org/maven2/ + default + + + + + + default + + diff --git a/src/it/released-version-version-prefix/verify.groovy b/src/it/released-version-version-prefix/verify.groovy new file mode 100644 index 00000000..e6c9d235 --- /dev/null +++ b/src/it/released-version-version-prefix/verify.groovy @@ -0,0 +1,12 @@ +File file = new File( basedir, "build.log" ); +assert file.exists(); + +String text = file.getText("utf-8"); + +// assert latest release of org.apache.continuum:continuum - it's in apache attic, there will be no new releases any more +assert text.contains("myReleasedVersion.version=1.3.8") +assert text.contains("myReleasedVersion.majorVersion=1") +assert text.contains("myReleasedVersion.minorVersion=3") +assert text.contains("myReleasedVersion.incrementalVersion=8") + +return true; diff --git a/src/main/java/org/codehaus/mojo/buildhelper/ReleasedVersionMojo.java b/src/main/java/org/codehaus/mojo/buildhelper/ReleasedVersionMojo.java index 77d3c7fa..ca6cf53e 100644 --- a/src/main/java/org/codehaus/mojo/buildhelper/ReleasedVersionMojo.java +++ b/src/main/java/org/codehaus/mojo/buildhelper/ReleasedVersionMojo.java @@ -73,6 +73,13 @@ public class ReleasedVersionMojo @Parameter( defaultValue = "${project.remoteArtifactRepositories}", readonly = true ) private List remoteArtifactRepositories; + /** + * Returned version must before with the version prefix. Allows searching for maximum minor version given a major + * version, or maximum increment version given a minor version. + */ + @Parameter( property = "releasedVersion.versionPrefix" ) + private String versionPrefix = ""; + /** * Prefix string to use for the set of version properties. */ @@ -111,7 +118,8 @@ public void execute() remoteArtifactRepositories ); for ( ArtifactVersion version : versions ) { - if ( !ArtifactUtils.isSnapshot( version.toString() ) + if ( version.toString().startsWith(versionPrefix) + && !ArtifactUtils.isSnapshot( version.toString() ) && ( releasedVersion == null || version.compareTo( releasedVersion ) > 0 ) ) { releasedVersion = version;