Skip to content

Commit

Permalink
Resolves #794: Do not change versions where oldVersion does not match…
Browse files Browse the repository at this point in the history
… the POM version
  • Loading branch information
andrzejj0 authored and slawekjaranowski committed Nov 3, 2022
1 parent 780cf6e commit 8c13de2
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, Model> reactorModels = PomHelper.getReactorModels( project, getLog() );
Expand All @@ -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 );
Expand All @@ -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
Expand Down Expand Up @@ -453,7 +452,7 @@ private void applyChange( MavenProject project, SortedMap<String, Model> reactor
if ( current != null )
{
current.getValue().setVersion( newVersion );
addFile( files, project, current.getKey() );
files.add( getModuleProjectFile( project, current.getKey() ) );
}

for ( Map.Entry<String, Model> sourceEntry : reactor.entrySet() )
Expand Down Expand Up @@ -483,7 +482,7 @@ private void applyChange( MavenProject project, SortedMap<String, Model> reactor
continue;
}

addFile( files, project, sourcePath );
files.add( getModuleProjectFile( project, sourcePath ) );

getLog().debug(
"Looking for modules which use " + ArtifactUtils.versionlessKey( sourceGroupId, sourceArtifactId )
Expand Down Expand Up @@ -540,29 +539,22 @@ private void applyChange( MavenProject project, SortedMap<String, Model> reactor
}
}

private void addFile( Set<File> 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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,19 +13,38 @@
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;

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;
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
{
Expand Down Expand Up @@ -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( "<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();
}
}
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( "<version>1.0</version>" ) );

// 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( "<version>1.0</version>" ) );
}

@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( "<version>bar</version>" ) ) );
}

@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( "<version>bar</version>" ) ) );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>default-group</groupId>
<artifactId>default-artifact</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
</project>

0 comments on commit 8c13de2

Please sign in to comment.