Skip to content

Commit

Permalink
work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
jarmoniuk committed Oct 26, 2022
1 parent e07fa08 commit 9ea8fb0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 40 deletions.
44 changes: 21 additions & 23 deletions src/main/java/org/codehaus/mojo/versions/SetMojo.java
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 @@ -357,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 @@ -369,17 +370,21 @@ 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 )
// {
// applyChange( project, reactor, files, groupId, artifactId, oldVersion );
// }

if ( "always".equals( updateBuildOutputTimestampPolicy ) )
{
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
for ( File file : files )
Expand Down Expand Up @@ -447,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 @@ -477,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 @@ -534,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" );
return 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;
}

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
18 changes: 17 additions & 1 deletion src/test/java/org/codehaus/mojo/versions/SetMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,23 @@ public void testRemoveSnapshotIdempotency()
public void testSetOldVersionMismatch() throws Exception
{
TestUtils.copyDir( Paths.get( "src/test/resources/org/codehaus/mojo/set/issue-794" ), tempDir );
mojoRule.lookupConfiguredMojo( tempDir.toFile(), "set" ).execute();
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>" ) ) );
}
Expand Down
16 changes: 0 additions & 16 deletions src/test/resources/org/codehaus/mojo/set/issue-794/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,4 @@
<artifactId>default-artifact</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<goals>
<goal>set</goal>
</goals>
<configuration>
<oldVersion>foo</oldVersion>
<newVersion>bar</newVersion>
</configuration>
</plugin>
</plugins>
</build>
</project>

0 comments on commit 9ea8fb0

Please sign in to comment.