diff --git a/src/it/it-revert-isssue-265/aggregate/pom.xml b/src/it/it-revert-isssue-265/aggregate/pom.xml new file mode 100644 index 0000000000..ef00bd7e19 --- /dev/null +++ b/src/it/it-revert-isssue-265/aggregate/pom.xml @@ -0,0 +1,21 @@ + + 4.0.0 + localhost + dummy-artifact + OLD + pom + + + ../module-a + + + + + module-b + + + ../module-b + + + + diff --git a/src/it/it-revert-isssue-265/invoker.properties b/src/it/it-revert-isssue-265/invoker.properties new file mode 100644 index 0000000000..ca423c70cc --- /dev/null +++ b/src/it/it-revert-isssue-265/invoker.properties @@ -0,0 +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}:revert -f aggregate/pom.xml \ No newline at end of file diff --git a/src/it/it-revert-isssue-265/module-a/pom.xml b/src/it/it-revert-isssue-265/module-a/pom.xml new file mode 100644 index 0000000000..c0c8c088ca --- /dev/null +++ b/src/it/it-revert-isssue-265/module-a/pom.xml @@ -0,0 +1,15 @@ + + 4.0.0 + localhost + dummy-artifact-a + 1.0.0 + pom + + + + localhost + dummy-artifact + OLD + + + diff --git a/src/it/it-revert-isssue-265/module-b/pom.xml b/src/it/it-revert-isssue-265/module-b/pom.xml new file mode 100644 index 0000000000..6ea7173432 --- /dev/null +++ b/src/it/it-revert-isssue-265/module-b/pom.xml @@ -0,0 +1,17 @@ + + 4.0.0 + localhost + dummy-artifact-b + 1.0.0 + pom + + + + + localhost + dummy-artifact + OLD + + + + diff --git a/src/it/it-revert-isssue-265/verify.groovy b/src/it/it-revert-isssue-265/verify.groovy new file mode 100644 index 0000000000..4fd909e1f8 --- /dev/null +++ b/src/it/it-revert-isssue-265/verify.groovy @@ -0,0 +1,8 @@ +assert new File( basedir, "aggregate/pom.xml" ).text =~ /OLD/ +assert !new File( basedir, "aggregate/pom.xml.versionsBackup" ).exists() + +assert new File( basedir, "module-a/pom.xml" ).text =~ /OLD/ +assert !new File( basedir, "module-a/pom.xml.versionsBackup" ).exists() + +assert new File( basedir, "module-b/pom.xml" ).text =~ /OLD/ +assert !new File( basedir, "module-b/pom.xml.versionsBackup" ).exists() diff --git a/src/main/java/org/codehaus/mojo/versions/RevertMojo.java b/src/main/java/org/codehaus/mojo/versions/RevertMojo.java index a85092f420..c2c294fcf2 100644 --- a/src/main/java/org/codehaus/mojo/versions/RevertMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/RevertMojo.java @@ -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. @@ -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. @@ -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 projectToProcess = !processFromLocalAggregationRoot + ? PomHelper.getLocalRoot( projectBuilder, this.project, localRepository, null, getLog() ) + : this.project; - if ( backupFile.exists() ) + getLog().info( "Local aggregation root: " + projectToProcess.getBasedir() ); + Set reactor = PomHelper.getAllChildModules( projectToProcess, 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 = projectToProcess.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 ); + } } - } + } ); } } diff --git a/src/test/java/org/codehaus/mojo/versions/RevertMojoTest.java b/src/test/java/org/codehaus/mojo/versions/RevertMojoTest.java new file mode 100644 index 0000000000..ceb31ea1e6 --- /dev/null +++ b/src/test/java/org/codehaus/mojo/versions/RevertMojoTest.java @@ -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 ) ); + } +} diff --git a/src/test/resources/org/codehaus/mojo/revert/issue-265/aggregate/pom.xml b/src/test/resources/org/codehaus/mojo/revert/issue-265/aggregate/pom.xml new file mode 100644 index 0000000000..2dbb4eb56a --- /dev/null +++ b/src/test/resources/org/codehaus/mojo/revert/issue-265/aggregate/pom.xml @@ -0,0 +1,52 @@ + + + + 4.0.0 + localhost + dummy-artifact + NEW + pom + + + ../module-a + + + + + module-b + + + ../module-b + + + + + + + + org.codehaus.mojo + versions-maven-plugin + + revert + + + + + diff --git a/src/test/resources/org/codehaus/mojo/revert/issue-265/aggregate/pom.xml.versionsBackup b/src/test/resources/org/codehaus/mojo/revert/issue-265/aggregate/pom.xml.versionsBackup new file mode 100644 index 0000000000..ef00bd7e19 --- /dev/null +++ b/src/test/resources/org/codehaus/mojo/revert/issue-265/aggregate/pom.xml.versionsBackup @@ -0,0 +1,21 @@ + + 4.0.0 + localhost + dummy-artifact + OLD + pom + + + ../module-a + + + + + module-b + + + ../module-b + + + + diff --git a/src/test/resources/org/codehaus/mojo/revert/issue-265/module-a/pom.xml b/src/test/resources/org/codehaus/mojo/revert/issue-265/module-a/pom.xml new file mode 100644 index 0000000000..a5782a4807 --- /dev/null +++ b/src/test/resources/org/codehaus/mojo/revert/issue-265/module-a/pom.xml @@ -0,0 +1,15 @@ + + 4.0.0 + localhost + dummy-artifact-a + 1.0.0 + pom + + + + localhost + dummy-artifact + NEW + + + diff --git a/src/test/resources/org/codehaus/mojo/revert/issue-265/module-a/pom.xml.versionsBackup b/src/test/resources/org/codehaus/mojo/revert/issue-265/module-a/pom.xml.versionsBackup new file mode 100644 index 0000000000..c0c8c088ca --- /dev/null +++ b/src/test/resources/org/codehaus/mojo/revert/issue-265/module-a/pom.xml.versionsBackup @@ -0,0 +1,15 @@ + + 4.0.0 + localhost + dummy-artifact-a + 1.0.0 + pom + + + + localhost + dummy-artifact + OLD + + + diff --git a/src/test/resources/org/codehaus/mojo/revert/issue-265/module-b/pom.xml b/src/test/resources/org/codehaus/mojo/revert/issue-265/module-b/pom.xml new file mode 100644 index 0000000000..9bf6bd5e83 --- /dev/null +++ b/src/test/resources/org/codehaus/mojo/revert/issue-265/module-b/pom.xml @@ -0,0 +1,17 @@ + + 4.0.0 + localhost + dummy-artifact-b + NEW + pom + + + + + localhost + dummy-artifact + VERSION + + + + diff --git a/src/test/resources/org/codehaus/mojo/revert/issue-265/module-b/pom.xml.versionsBackup b/src/test/resources/org/codehaus/mojo/revert/issue-265/module-b/pom.xml.versionsBackup new file mode 100644 index 0000000000..6ea7173432 --- /dev/null +++ b/src/test/resources/org/codehaus/mojo/revert/issue-265/module-b/pom.xml.versionsBackup @@ -0,0 +1,17 @@ + + 4.0.0 + localhost + dummy-artifact-b + 1.0.0 + pom + + + + + localhost + dummy-artifact + OLD + + + +