Skip to content

Commit

Permalink
mojohaus#265: Processing all child modules instead of relying on defa…
Browse files Browse the repository at this point in the history
…ult maven processing
  • Loading branch information
jarmoniuk committed Sep 22, 2022
1 parent 3bdfa7d commit ea42af0
Show file tree
Hide file tree
Showing 15 changed files with 310 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/it/it-revert-isssue-265/aggregate/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>localhost</groupId>
<artifactId>dummy-artifact</artifactId>
<version>1.0</version>
<version>VERSION</version>
<packaging>pom</packaging>

<modules>
Expand Down
2 changes: 1 addition & 1 deletion src/it/it-revert-isssue-265/invoker.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
invoker.goals.1 = ${project.groupId}:${project.artifactId}:${project.version}:set -f aggregate/pom.xml
invoker.mavenOpts.1 = -DnewVersion=1.2.3 -DprocessAllModules

#invoker.goals.2=${project.groupId}:${project.artifactId}:${project.version}:plugin-updates-report
invoker.goals.2 = ${project.groupId}:${project.artifactId}:${project.version}:revert -f aggregate/pom.xml
4 changes: 2 additions & 2 deletions src/it/it-revert-isssue-265/module-a/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
<modelVersion>4.0.0</modelVersion>
<groupId>localhost</groupId>
<artifactId>dummy-artifact-a</artifactId>
<version>1.0</version>
<version>1.0.0</version>
<packaging>pom</packaging>

<dependencies>
<dependency>
<groupId>localhost</groupId>
<artifactId>dummy-artifact</artifactId>
<version>1.0.0</version>
<version>VERSION</version>
</dependency>
</dependencies>
</project>
4 changes: 2 additions & 2 deletions src/it/it-revert-isssue-265/module-b/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
<modelVersion>4.0.0</modelVersion>
<groupId>localhost</groupId>
<artifactId>dummy-artifact-b</artifactId>
<version>1.0</version>
<version>1.0.0</version>
<packaging>pom</packaging>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>localhost</groupId>
<artifactId>dummy-artifact</artifactId>
<version>1.0.0</version>
<version>VERSION</version>
</dependency>
</dependencies>
</dependencyManagement>
Expand Down
8 changes: 8 additions & 0 deletions src/it/it-revert-isssue-265/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
assert new File( basedir, "aggregate/pom.xml" ).text =~ /VERSION/
assert !new File( basedir, "aggregate/pom.xml.versionsBackup" ).exists()

assert new File( basedir, "module-a/pom.xml" ).text =~ /VERSION/
assert !new File( basedir, "module-a/pom.xml.versionsBackup" ).exists()

assert new File( basedir, "module-b/pom.xml" ).text =~ /VERSION/
assert !new File( basedir, "module-b/pom.xml.versionsBackup" ).exists()
82 changes: 64 additions & 18 deletions src/main/java/org/codehaus/mojo/versions/RevertMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,25 @@
* under the License.
*/

import java.io.File;
import javax.inject.Inject;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Set;

import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.FileUtils;
import org.apache.maven.project.MavenProjectBuilder;
import org.codehaus.mojo.versions.api.PomHelper;

import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;

/**
* Restores the pom from the initial backup.
Expand All @@ -37,8 +46,7 @@
* @since 1.0-alpha-3
*/
@Mojo( name = "revert", threadSafe = true )
public class RevertMojo
extends AbstractMojo
public class RevertMojo extends AbstractMojo
{
/**
* The Maven Project.
Expand All @@ -48,24 +56,62 @@ public class RevertMojo
@Parameter( defaultValue = "${project}", required = true, readonly = true )
private MavenProject project;

public void execute()
throws MojoExecutionException, MojoFailureException
/**
* Whether to start processing at the local aggregation root (which might be a parent module
* of that module where Maven is executed in, and the version change may affect parent and sibling modules).
* Setting to false makes sure only the module (and it's submodules) where Maven is executed for is affected.
*
* @since 2.13.0
*/
@Parameter( property = "processFromLocalAggregationRoot", defaultValue = "true" )
private boolean processFromLocalAggregationRoot;

protected MavenProjectBuilder projectBuilder;

@Parameter( defaultValue = "${localRepository}", readonly = true )
protected ArtifactRepository localRepository;

@Inject
protected RevertMojo( MavenProjectBuilder projectBuilder )
{
this.projectBuilder = projectBuilder;
}

public void execute() throws MojoExecutionException, MojoFailureException
{
File outFile = project.getFile();
File backupFile = new File( outFile.getParentFile(), outFile.getName() + ".versionsBackup" );
final MavenProject project = !processFromLocalAggregationRoot
? PomHelper.getLocalRoot( projectBuilder, this.project, localRepository, null, getLog() )
: this.project;

if ( backupFile.exists() )
getLog().info( "Local aggregation root: " + project.getBasedir() );
Set<String> reactor = PomHelper.getAllChildModules( project, getLog() );
reactor.add( "." );

reactor.forEach( entry ->
{
getLog().info( "Restoring " + outFile + " from " + backupFile );
try
{
FileUtils.copyFile( backupFile, outFile );
FileUtils.forceDelete( backupFile );
}
catch ( IOException e )
Path pomFile = project.getBasedir().toPath().resolve( entry ).resolve( "pom.xml" ).normalize();
getLog().debug( "Processing:" + pomFile );
Path backupFile = Paths.get( pomFile + ".versionsBackup" );
if ( Files.exists( backupFile ) )
{
throw new MojoExecutionException( e.getMessage(), e );
getLog().info( "Restoring " + pomFile + " from " + backupFile );
try
{
Files.copy( backupFile, pomFile, REPLACE_EXISTING );
try
{
Files.delete( backupFile );
}
catch ( IOException e )
{
getLog().warn( "Error deleting " + backupFile );
}
}
catch ( IOException e )
{
getLog().warn( "Error copying " + backupFile + " onto " + pomFile );
}
}
}
} );
}
}
1 change: 1 addition & 0 deletions src/main/java/org/codehaus/mojo/versions/SetMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ public void execute() throws MojoExecutionException, MojoFailureException
final SortedMap<String, Model> reactor =
new TreeMap<>( new ReactorDepthComparator( reactorModels ) );
reactor.putAll( reactorModels );
reactor.entrySet().forEach( entry -> getLog().debug( "*** entry:" + entry.getKey() ) );

// set of files to update
final Set<File> files = new LinkedHashSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1394,7 +1394,7 @@ public static void removeMissingChildModules( Log logger, File basedir, Collecti
{
String modulePath = i.next();
File moduleFile = new File( basedir, modulePath );

logger.debug( "*** moduleFile: " + moduleFile.getAbsolutePath() );
if ( moduleFile.isDirectory() && new File( moduleFile, "pom.xml" ).isFile() )
{
// it's a directory that exists
Expand Down
93 changes: 93 additions & 0 deletions src/test/java/org/codehaus/mojo/versions/RevertMojoTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package org.codehaus.mojo.versions;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Objects;

import org.apache.maven.plugin.testing.AbstractMojoTestCase;
import org.apache.maven.plugin.testing.MojoRule;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;

import static java.lang.String.join;
import static org.apache.commons.io.FileUtils.copyDirectory;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.core.Is.is;

/**
* Unit tests for {@link RevertMojo}
*
* @author Andrzej Jarmoniuk
*/
public class RevertMojoTest extends AbstractMojoTestCase
{
@Rule
public MojoRule mojoRule = new MojoRule( this );
private Path pomDir;

@Before
public void setUp() throws Exception
{
super.setUp();
pomDir = Files.createTempDirectory( "revert-" );
}

@After
public void tearDown() throws Exception
{
try
{
if ( pomDir != null && pomDir.toFile().exists() )
{
Arrays.stream( Objects.requireNonNull( pomDir.toFile().listFiles() ) ).forEach( File::delete );
pomDir.toFile().delete();
}
}
finally
{
super.tearDown();
}
}

public void testRevert() throws Exception
{
copyDirectory( new File( getBasedir(),
"target/test-classes/org/codehaus/mojo/revert/issue-265" ), pomDir.toFile() );
RevertMojo myMojo = (RevertMojo) mojoRule.lookupConfiguredMojo(
new File( pomDir.toString(), "aggregate" ), "revert" );
myMojo.execute();

assertThat( join( "\n", Files.readAllLines( pomDir.resolve( "aggregate/pom.xml" ) ) ),
containsString( "OLD" ) );
assertThat( Files.exists( pomDir.resolve( "aggregate/pom.xml.versionsBackup" ) ), is( false ) );
assertThat( join( "\n", Files.readAllLines( pomDir.resolve( "module-a/pom.xml" ) ) ),
containsString( "OLD" ) );
assertThat( Files.exists( pomDir.resolve( "module-a/pom.xml.versionsBackup" ) ), is( false ) );
assertThat( join( "\n", Files.readAllLines( pomDir.resolve( "module-b/pom.xml" ) ) ),
containsString( "OLD" ) );
assertThat( Files.exists( pomDir.resolve( "module-b/pom.xml.versionsBackup" ) ), is( false ) );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<!--
~ Licensed to the Apache Software Foundation (ASF) under one
~ or more contributor license agreements. See the NOTICE file
~ distributed with this work for additional information
~ regarding copyright ownership. The ASF licenses this file
~ to you under the Apache License, Version 2.0 (the
~ "License"); you may not use this file except in compliance
~ with the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~ KIND, either express or implied. See the License for the
~ specific language governing permissions and limitations
~ under the License.
-->

<project>
<modelVersion>4.0.0</modelVersion>
<groupId>localhost</groupId>
<artifactId>dummy-artifact</artifactId>
<version>NEW</version>
<packaging>pom</packaging>

<modules>
<module>../module-a</module>
</modules>

<profiles>
<profile>
<id>module-b</id>

<modules>
<module>../module-b</module>
</modules>
</profile>
</profiles>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<goals>
<goal>revert</goal>
</goals>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>localhost</groupId>
<artifactId>dummy-artifact</artifactId>
<version>OLD</version>
<packaging>pom</packaging>

<modules>
<module>../module-a</module>
</modules>

<profiles>
<profile>
<id>module-b</id>

<modules>
<module>../module-b</module>
</modules>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>localhost</groupId>
<artifactId>dummy-artifact-a</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>

<dependencies>
<dependency>
<groupId>localhost</groupId>
<artifactId>dummy-artifact</artifactId>
<version>NEW</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>localhost</groupId>
<artifactId>dummy-artifact-a</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>

<dependencies>
<dependency>
<groupId>localhost</groupId>
<artifactId>dummy-artifact</artifactId>
<version>OLD</version>
</dependency>
</dependencies>
</project>

0 comments on commit ea42af0

Please sign in to comment.