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

Implementing #709: removeSnapshot idempotency #712

Merged
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
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>