Skip to content

Commit

Permalink
Implementing #709: Remove snapshot idempotency
Browse files Browse the repository at this point in the history
  • Loading branch information
jarmoniuk authored and slawekjaranowski committed Sep 21, 2022
1 parent f0dc715 commit 3e98398
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/main/java/org/codehaus/mojo/versions/SetMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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 ) )
Expand Down Expand Up @@ -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;
Expand Down
41 changes: 41 additions & 0 deletions src/test/java/org/codehaus/mojo/versions/SetMojoTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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( "<version>1.0</version>" ) );

// 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( "<version>1.0</version>" ) );
}
finally
{
if ( pomDir != null && pomDir.toFile().exists() )
{
Arrays.stream( Objects.requireNonNull( pomDir.toFile().listFiles() ) ).forEach( File::delete );
pomDir.toFile().delete();
}
}
}
}
23 changes: 23 additions & 0 deletions src/test/resources/org/codehaus/mojo/set/remove-snapshot/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>default-group</groupId>
<artifactId>default-artifact</artifactId>
<version>1.0-SNAPSHOT</version>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<goals>
<goal>set</goal>
</goals>
<configuration>
<removeSnapshot>true</removeSnapshot>
<generateBackupPoms>false</generateBackupPoms>
</configuration>
</plugin>
</plugins>
</build>
</project>

0 comments on commit 3e98398

Please sign in to comment.