From a443d4055a4d9685531cc6d3b593e2a5d5187d33 Mon Sep 17 00:00:00 2001 From: Andrzej Jarmoniuk Date: Mon, 19 Sep 2022 18:24:26 +0200 Subject: [PATCH] Implementing #709: Remove snapshot idempotency --- .../org/codehaus/mojo/versions/SetMojo.java | 17 ++++---- .../codehaus/mojo/versions/SetMojoTest.java | 41 +++++++++++++++++++ .../codehaus/mojo/set/remove-snapshot/pom.xml | 23 +++++++++++ 3 files changed, 73 insertions(+), 8 deletions(-) create mode 100644 src/test/resources/org/codehaus/mojo/set/remove-snapshot/pom.xml diff --git a/src/main/java/org/codehaus/mojo/versions/SetMojo.java b/src/main/java/org/codehaus/mojo/versions/SetMojo.java index 206417570b..56205ecf8f 100644 --- a/src/main/java/org/codehaus/mojo/versions/SetMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/SetMojo.java @@ -65,6 +65,8 @@ import org.codehaus.plexus.components.interactivity.PrompterException; import org.codehaus.plexus.util.StringUtils; +import static org.codehaus.plexus.util.StringUtils.isEmpty; + /** * Sets the current project's version and based on that change propagates that change onto any child modules as * necessary. @@ -298,8 +300,13 @@ public void execute() throws MojoExecutionException, MojoFailureException getLog().info( "SNAPSHOT found. BEFORE " + version + " --> AFTER: " + newVersion ); } - if ( StringUtils.isEmpty( newVersion ) ) + if ( isEmpty( newVersion ) ) { + if ( removeSnapshot ) + { + getLog().info( "removeSnapshot enabled whilst the version is not a snapshot: nothing to do." ); + return; + } if ( settings.isInteractiveMode() ) { try @@ -319,12 +326,6 @@ public void execute() throws MojoExecutionException, MojoFailureException + "or run in interactive mode" ); } } - if ( StringUtils.isEmpty( newVersion ) ) - { - throw new MojoExecutionException( "You must specify the new version, either by using the newVersion " - + "property (that is -DnewVersion=... on the command line) " - + "or run in interactive mode" ); - } if ( !"onchange".equals( updateBuildOutputTimestampPolicy ) && !"always".equals( updateBuildOutputTimestampPolicy ) && !"never".equals( updateBuildOutputTimestampPolicy ) ) @@ -614,7 +615,7 @@ private void updateBuildOutputTimestamp( ModifiedPomXMLEventReader pom, Model mo { String buildOutputTimestamp = model.getProperties().getProperty( "project.build.outputTimestamp" ); - if ( buildOutputTimestamp == null || StringUtils.isEmpty( buildOutputTimestamp ) ) + if ( buildOutputTimestamp == null || isEmpty( buildOutputTimestamp ) ) { // no Reproducible Builds output timestamp defined return; diff --git a/src/test/java/org/codehaus/mojo/versions/SetMojoTest.java b/src/test/java/org/codehaus/mojo/versions/SetMojoTest.java index b8d5ff77ad..162679afd5 100644 --- a/src/test/java/org/codehaus/mojo/versions/SetMojoTest.java +++ b/src/test/java/org/codehaus/mojo/versions/SetMojoTest.java @@ -1,7 +1,13 @@ package org.codehaus.mojo.versions; import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.Objects; +import org.apache.maven.execution.MavenExecutionRequest; import org.apache.maven.model.Model; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; @@ -11,6 +17,7 @@ import org.junit.Rule; import org.junit.Test; +import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.is; @@ -106,4 +113,38 @@ public void testVersionlessDependency() throws Exception new File( "target/test-classes/org/codehaus/mojo/set/versionless-01" ), "set" ); myMojo.execute(); } + + @Test + public void testRemoveSnapshotIdempotency() + throws Exception + { + Path pomDir = Files.createTempDirectory( "set-" ); + try + { + Files.copy( Paths.get( "src/test/resources/org/codehaus/mojo/set/remove-snapshot/pom.xml" ), + Paths.get( pomDir.toString(), "pom.xml" ), REPLACE_EXISTING ); + + SetMojo firstRun = (SetMojo) mojoRule.lookupConfiguredMojo( pomDir.toFile(), "set" ); + firstRun.execute(); + assertThat( String.join( "", Files.readAllLines( Paths.get( pomDir.toString(), "pom.xml" ) ) ), + containsString( "1.0" ) ); + + // no exception should be thrown, the file should stay with version "1.0" + SetMojo secondRun = (SetMojo) mojoRule.lookupConfiguredMojo( pomDir.toFile(), "set" ); + MavenExecutionRequest request = + (MavenExecutionRequest) getVariableValueFromObject( secondRun.settings, "request" ); + setVariableValueToObject( request, "interactiveMode", false ); + secondRun.execute(); + assertThat( String.join( "", Files.readAllLines( Paths.get( pomDir.toString(), "pom.xml" ) ) ), + containsString( "1.0" ) ); + } + finally + { + if ( pomDir != null && pomDir.toFile().exists() ) + { + Arrays.stream( Objects.requireNonNull( pomDir.toFile().listFiles() ) ).forEach( File::delete ); + pomDir.toFile().delete(); + } + } + } } diff --git a/src/test/resources/org/codehaus/mojo/set/remove-snapshot/pom.xml b/src/test/resources/org/codehaus/mojo/set/remove-snapshot/pom.xml new file mode 100644 index 0000000000..3bfaf049a7 --- /dev/null +++ b/src/test/resources/org/codehaus/mojo/set/remove-snapshot/pom.xml @@ -0,0 +1,23 @@ + + 4.0.0 + default-group + default-artifact + 1.0-SNAPSHOT + + + + + org.codehaus.mojo + versions-maven-plugin + + set + + + true + false + + + + +