Skip to content

Commit

Permalink
#265: Processing all child modules instead of relying on default mave…
Browse files Browse the repository at this point in the history
…n processing
  • Loading branch information
andrzejj0 committed Sep 22, 2022
1 parent 3e98398 commit 473bff4
Show file tree
Hide file tree
Showing 13 changed files with 359 additions and 18 deletions.
21 changes: 21 additions & 0 deletions src/it/it-revert-isssue-265/aggregate/pom.xml
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>
4 changes: 4 additions & 0 deletions src/it/it-revert-isssue-265/invoker.properties
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions src/it/it-revert-isssue-265/module-a/pom.xml
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>
17 changes: 17 additions & 0 deletions src/it/it-revert-isssue-265/module-b/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>localhost</groupId>
<artifactId>dummy-artifact-b</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>localhost</groupId>
<artifactId>dummy-artifact</artifactId>
<version>OLD</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
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 =~ /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()
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 );
}
}
}
} );
}
}
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>
Loading

0 comments on commit 473bff4

Please sign in to comment.