From 8c13de22cae85419588adbcd4f532d0fa2b0dec2 Mon Sep 17 00:00:00 2001 From: Andrzej Jarmoniuk Date: Wed, 26 Oct 2022 18:38:07 +0200 Subject: [PATCH] Resolves #794: Do not change versions where oldVersion does not match the POM version --- .../org/codehaus/mojo/versions/SetMojo.java | 52 +++++------ .../codehaus/mojo/versions/SetMojoTest.java | 91 +++++++++++++------ .../org/codehaus/mojo/set/issue-794/pom.xml | 7 ++ 3 files changed, 90 insertions(+), 60 deletions(-) create mode 100644 versions-maven-plugin/src/test/resources/org/codehaus/mojo/set/issue-794/pom.xml diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/SetMojo.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/SetMojo.java index 0305a2968..fef10ea97 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/SetMojo.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/SetMojo.java @@ -33,6 +33,7 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Set; import java.util.SortedMap; import java.util.TimeZone; @@ -336,15 +337,9 @@ public void execute() throws MojoExecutionException, MojoFailureException try { - final MavenProject project; - if ( processFromLocalAggregationRoot ) - { - project = PomHelper.getLocalRoot( projectBuilder, getProject(), localRepository, null, getLog() ); - } - else - { - project = getProject(); - } + final MavenProject project = processFromLocalAggregationRoot + ? PomHelper.getLocalRoot( projectBuilder, getProject(), localRepository, null, getLog() ) + : getProject(); getLog().info( "Local aggregation root: " + project.getBasedir() ); Map reactorModels = PomHelper.getReactorModels( project, getLog() ); @@ -363,7 +358,7 @@ public void execute() throws MojoExecutionException, MojoFailureException Pattern.compile( RegexUtils.convertWildcardsToRegex( fixNullOrEmpty( artifactId, "*" ), true ) ); Pattern oldVersionIdRegex = Pattern.compile( RegexUtils.convertWildcardsToRegex( fixNullOrEmpty( oldVersion, "*" ), true ) ); - boolean found = false; + for ( Model m : reactor.values() ) { final String mGroupId = PomHelper.getGroupId( m ); @@ -375,16 +370,20 @@ public void execute() throws MojoExecutionException, MojoFailureException && oldVersionIdRegex.matcher( mVersion ).matches() && !newVersion.equals( mVersion ) ) { - found = true; - // if the change is not one we have swept up already applyChange( project, reactor, files, mGroupId, m.getArtifactId(), StringUtils.isBlank( oldVersion ) || "*".equals( oldVersion ) ? "" : mVersion ); } } - if ( !found && RegexUtils.getWildcardScore( groupId ) == 0 && RegexUtils.getWildcardScore( artifactId ) == 0 - && RegexUtils.getWildcardScore( oldVersion ) == 0 ) + + if ( "always".equals( updateBuildOutputTimestampPolicy ) ) { - applyChange( project, reactor, files, groupId, artifactId, oldVersion ); + reactor.values().parallelStream() + .map( m -> PomHelper.getModelEntry( reactor, PomHelper.getGroupId( m ), + PomHelper.getArtifactId( m ) ) ) + .filter( Objects::nonNull ) + .map( Map.Entry::getKey ) + .map( f -> getModuleProjectFile( project, f ) ) + .forEach( files::add ); } // now process all the updates @@ -453,7 +452,7 @@ private void applyChange( MavenProject project, SortedMap reactor if ( current != null ) { current.getValue().setVersion( newVersion ); - addFile( files, project, current.getKey() ); + files.add( getModuleProjectFile( project, current.getKey() ) ); } for ( Map.Entry sourceEntry : reactor.entrySet() ) @@ -483,7 +482,7 @@ private void applyChange( MavenProject project, SortedMap reactor continue; } - addFile( files, project, sourcePath ); + files.add( getModuleProjectFile( project, sourcePath ) ); getLog().debug( "Looking for modules which use " + ArtifactUtils.versionlessKey( sourceGroupId, sourceArtifactId ) @@ -540,29 +539,22 @@ private void applyChange( MavenProject project, SortedMap reactor } } - private void addFile( Set files, MavenProject project, String relativePath ) + private static File getModuleProjectFile( MavenProject project, String relativePath ) { final File moduleDir = new File( project.getBasedir(), relativePath ); final File projectBaseDir = project.getBasedir(); - final File moduleProjectFile; - if ( projectBaseDir.equals( moduleDir ) ) { - moduleProjectFile = project.getFile(); + return project.getFile(); } else if ( moduleDir.isDirectory() ) { - moduleProjectFile = new File( moduleDir, "pom.xml" ); - } - else - { - // i don't think this should ever happen... but just in case - // the module references the file-name - moduleProjectFile = moduleDir; + return new File( moduleDir, "pom.xml" ); } - - files.add( moduleProjectFile ); + // i don't think this should ever happen... but just in case + // the module references the file-name + return moduleDir; } /** diff --git a/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/SetMojoTest.java b/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/SetMojoTest.java index 162679afd..bce9eaa04 100644 --- a/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/SetMojoTest.java +++ b/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/SetMojoTest.java @@ -1,11 +1,10 @@ package org.codehaus.mojo.versions; import java.io.File; +import java.io.IOException; 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; @@ -14,6 +13,9 @@ import org.apache.maven.plugin.testing.AbstractMojoTestCase; import org.apache.maven.plugin.testing.MojoRule; import org.apache.maven.project.MavenProject; +import org.codehaus.mojo.versions.utils.TestUtils; +import org.junit.After; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -21,12 +23,28 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.not; public class SetMojoTest extends AbstractMojoTestCase { @Rule public MojoRule mojoRule = new MojoRule( this ); + private Path tempDir; + + @Before + public void setUp() throws Exception + { + super.setUp(); + tempDir = TestUtils.createTempDir( "set" ); + } + + @After + public void tearDown() throws IOException + { + TestUtils.tearDownTempDir( tempDir ); + } + @Test public void testGetIncrementedVersion() throws MojoExecutionException { @@ -118,33 +136,46 @@ public void testVersionlessDependency() throws Exception 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(); - } - } + Files.copy( Paths.get( "src/test/resources/org/codehaus/mojo/set/remove-snapshot/pom.xml" ), + Paths.get( tempDir.toString(), "pom.xml" ), REPLACE_EXISTING ); + + SetMojo firstRun = (SetMojo) mojoRule.lookupConfiguredMojo( tempDir.toFile(), "set" ); + firstRun.execute(); + assertThat( String.join( "", Files.readAllLines( tempDir.resolve( "pom.xml" ) ) ), + containsString( "1.0" ) ); + + // no exception should be thrown, the file should stay with version "1.0" + SetMojo secondRun = (SetMojo) mojoRule.lookupConfiguredMojo( tempDir.toFile(), "set" ); + MavenExecutionRequest request = + (MavenExecutionRequest) getVariableValueFromObject( secondRun.settings, "request" ); + setVariableValueToObject( request, "interactiveMode", false ); + secondRun.execute(); + assertThat( String.join( "", Files.readAllLines( tempDir.resolve( "pom.xml" ) ) ), + containsString( "1.0" ) ); + } + + @Test + public void testSetOldVersionMismatch() throws Exception + { + TestUtils.copyDir( Paths.get( "src/test/resources/org/codehaus/mojo/set/issue-794" ), tempDir ); + SetMojo mojo = (SetMojo) mojoRule.lookupConfiguredMojo( tempDir.toFile(), "set" ); + setVariableValueToObject( mojo, "oldVersion", "foo" ); + setVariableValueToObject( mojo, "newVersion", "bar" ); + mojo.execute(); + assertThat( String.join( "", Files.readAllLines( tempDir.resolve( "pom.xml" ) ) ), + not( containsString( "bar" ) ) ); + } + + @Test + public void testSetOldVersionMismatchProcessAllModules() throws Exception + { + TestUtils.copyDir( Paths.get( "src/test/resources/org/codehaus/mojo/set/issue-794" ), tempDir ); + SetMojo mojo = (SetMojo) mojoRule.lookupConfiguredMojo( tempDir.toFile(), "set" ); + setVariableValueToObject( mojo, "oldVersion", "foo" ); + setVariableValueToObject( mojo, "newVersion", "bar" ); + setVariableValueToObject( mojo, "processAllModules", true ); + mojo.execute(); + assertThat( String.join( "", Files.readAllLines( tempDir.resolve( "pom.xml" ) ) ), + not( containsString( "bar" ) ) ); } } diff --git a/versions-maven-plugin/src/test/resources/org/codehaus/mojo/set/issue-794/pom.xml b/versions-maven-plugin/src/test/resources/org/codehaus/mojo/set/issue-794/pom.xml new file mode 100644 index 000000000..77f1540e8 --- /dev/null +++ b/versions-maven-plugin/src/test/resources/org/codehaus/mojo/set/issue-794/pom.xml @@ -0,0 +1,7 @@ + + 4.0.0 + default-group + default-artifact + 1.0.0 + pom + \ No newline at end of file