Skip to content

Commit e7aa406

Browse files
andrzejj0slawekjaranowski
authored andcommitted
#265: Processing all child modules instead of relying on default maven processing
1 parent 37e8947 commit e7aa406

File tree

13 files changed

+359
-18
lines changed

13 files changed

+359
-18
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<project>
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>localhost</groupId>
4+
<artifactId>dummy-artifact</artifactId>
5+
<version>OLD</version>
6+
<packaging>pom</packaging>
7+
8+
<modules>
9+
<module>../module-a</module>
10+
</modules>
11+
12+
<profiles>
13+
<profile>
14+
<id>module-b</id>
15+
16+
<modules>
17+
<module>../module-b</module>
18+
</modules>
19+
</profile>
20+
</profiles>
21+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
invoker.goals.1 = ${project.groupId}:${project.artifactId}:${project.version}:set -f aggregate/pom.xml
2+
invoker.mavenOpts.1 = -DnewVersion=1.2.3 -DprocessAllModules
3+
4+
invoker.goals.2 = ${project.groupId}:${project.artifactId}:${project.version}:revert -f aggregate/pom.xml
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<project>
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>localhost</groupId>
4+
<artifactId>dummy-artifact-a</artifactId>
5+
<version>1.0.0</version>
6+
<packaging>pom</packaging>
7+
8+
<dependencies>
9+
<dependency>
10+
<groupId>localhost</groupId>
11+
<artifactId>dummy-artifact</artifactId>
12+
<version>OLD</version>
13+
</dependency>
14+
</dependencies>
15+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<project>
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>localhost</groupId>
4+
<artifactId>dummy-artifact-b</artifactId>
5+
<version>1.0.0</version>
6+
<packaging>pom</packaging>
7+
8+
<dependencyManagement>
9+
<dependencies>
10+
<dependency>
11+
<groupId>localhost</groupId>
12+
<artifactId>dummy-artifact</artifactId>
13+
<version>OLD</version>
14+
</dependency>
15+
</dependencies>
16+
</dependencyManagement>
17+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
assert new File( basedir, "aggregate/pom.xml" ).text =~ /OLD/
2+
assert !new File( basedir, "aggregate/pom.xml.versionsBackup" ).exists()
3+
4+
assert new File( basedir, "module-a/pom.xml" ).text =~ /OLD/
5+
assert !new File( basedir, "module-a/pom.xml.versionsBackup" ).exists()
6+
7+
assert new File( basedir, "module-b/pom.xml" ).text =~ /OLD/
8+
assert !new File( basedir, "module-b/pom.xml.versionsBackup" ).exists()

src/main/java/org/codehaus/mojo/versions/RevertMojo.java

+64-18
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,25 @@
1919
* under the License.
2020
*/
2121

22-
import java.io.File;
22+
import javax.inject.Inject;
23+
2324
import java.io.IOException;
25+
import java.nio.file.Files;
26+
import java.nio.file.Path;
27+
import java.nio.file.Paths;
28+
import java.util.Set;
2429

30+
import org.apache.maven.artifact.repository.ArtifactRepository;
2531
import org.apache.maven.plugin.AbstractMojo;
2632
import org.apache.maven.plugin.MojoExecutionException;
2733
import org.apache.maven.plugin.MojoFailureException;
2834
import org.apache.maven.plugins.annotations.Mojo;
2935
import org.apache.maven.plugins.annotations.Parameter;
3036
import org.apache.maven.project.MavenProject;
31-
import org.codehaus.plexus.util.FileUtils;
37+
import org.apache.maven.project.MavenProjectBuilder;
38+
import org.codehaus.mojo.versions.api.PomHelper;
39+
40+
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
3241

3342
/**
3443
* Restores the pom from the initial backup.
@@ -37,8 +46,7 @@
3746
* @since 1.0-alpha-3
3847
*/
3948
@Mojo( name = "revert", threadSafe = true )
40-
public class RevertMojo
41-
extends AbstractMojo
49+
public class RevertMojo extends AbstractMojo
4250
{
4351
/**
4452
* The Maven Project.
@@ -48,24 +56,62 @@ public class RevertMojo
4856
@Parameter( defaultValue = "${project}", required = true, readonly = true )
4957
private MavenProject project;
5058

51-
public void execute()
52-
throws MojoExecutionException, MojoFailureException
59+
/**
60+
* Whether to start processing at the local aggregation root (which might be a parent module
61+
* of that module where Maven is executed in, and the version change may affect parent and sibling modules).
62+
* Setting to false makes sure only the module (and it's submodules) where Maven is executed for is affected.
63+
*
64+
* @since 2.13.0
65+
*/
66+
@Parameter( property = "processFromLocalAggregationRoot", defaultValue = "true" )
67+
private boolean processFromLocalAggregationRoot;
68+
69+
protected MavenProjectBuilder projectBuilder;
70+
71+
@Parameter( defaultValue = "${localRepository}", readonly = true )
72+
protected ArtifactRepository localRepository;
73+
74+
@Inject
75+
protected RevertMojo( MavenProjectBuilder projectBuilder )
76+
{
77+
this.projectBuilder = projectBuilder;
78+
}
79+
80+
public void execute() throws MojoExecutionException, MojoFailureException
5381
{
54-
File outFile = project.getFile();
55-
File backupFile = new File( outFile.getParentFile(), outFile.getName() + ".versionsBackup" );
82+
final MavenProject projectToProcess = !processFromLocalAggregationRoot
83+
? PomHelper.getLocalRoot( projectBuilder, this.project, localRepository, null, getLog() )
84+
: this.project;
5685

57-
if ( backupFile.exists() )
86+
getLog().info( "Local aggregation root: " + projectToProcess.getBasedir() );
87+
Set<String> reactor = PomHelper.getAllChildModules( projectToProcess, getLog() );
88+
reactor.add( "." );
89+
90+
reactor.forEach( entry ->
5891
{
59-
getLog().info( "Restoring " + outFile + " from " + backupFile );
60-
try
61-
{
62-
FileUtils.copyFile( backupFile, outFile );
63-
FileUtils.forceDelete( backupFile );
64-
}
65-
catch ( IOException e )
92+
Path pomFile = projectToProcess.getBasedir().toPath().resolve( entry ).resolve( "pom.xml" ).normalize();
93+
getLog().debug( "Processing:" + pomFile );
94+
Path backupFile = Paths.get( pomFile + ".versionsBackup" );
95+
if ( Files.exists( backupFile ) )
6696
{
67-
throw new MojoExecutionException( e.getMessage(), e );
97+
getLog().info( "Restoring " + pomFile + " from " + backupFile );
98+
try
99+
{
100+
Files.copy( backupFile, pomFile, REPLACE_EXISTING );
101+
try
102+
{
103+
Files.delete( backupFile );
104+
}
105+
catch ( IOException e )
106+
{
107+
getLog().warn( "Error deleting " + backupFile );
108+
}
109+
}
110+
catch ( IOException e )
111+
{
112+
getLog().warn( "Error copying " + backupFile + " onto " + pomFile );
113+
}
68114
}
69-
}
115+
} );
70116
}
71117
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package org.codehaus.mojo.versions;
2+
3+
/*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
import java.io.File;
23+
import java.nio.file.Files;
24+
import java.nio.file.Path;
25+
import java.util.Arrays;
26+
import java.util.Objects;
27+
28+
import org.apache.maven.plugin.testing.AbstractMojoTestCase;
29+
import org.apache.maven.plugin.testing.MojoRule;
30+
import org.junit.After;
31+
import org.junit.Before;
32+
import org.junit.Rule;
33+
34+
import static java.lang.String.join;
35+
import static org.apache.commons.io.FileUtils.copyDirectory;
36+
import static org.hamcrest.MatcherAssert.assertThat;
37+
import static org.hamcrest.Matchers.containsString;
38+
import static org.hamcrest.core.Is.is;
39+
40+
/**
41+
* Unit tests for {@link RevertMojo}
42+
*
43+
* @author Andrzej Jarmoniuk
44+
*/
45+
public class RevertMojoTest extends AbstractMojoTestCase
46+
{
47+
@Rule
48+
public MojoRule mojoRule = new MojoRule( this );
49+
private Path pomDir;
50+
51+
@Before
52+
public void setUp() throws Exception
53+
{
54+
super.setUp();
55+
pomDir = Files.createTempDirectory( "revert-" );
56+
}
57+
58+
@After
59+
public void tearDown() throws Exception
60+
{
61+
try
62+
{
63+
if ( pomDir != null && pomDir.toFile().exists() )
64+
{
65+
Arrays.stream( Objects.requireNonNull( pomDir.toFile().listFiles() ) ).forEach( File::delete );
66+
pomDir.toFile().delete();
67+
}
68+
}
69+
finally
70+
{
71+
super.tearDown();
72+
}
73+
}
74+
75+
public void testRevert() throws Exception
76+
{
77+
copyDirectory( new File( getBasedir(),
78+
"target/test-classes/org/codehaus/mojo/revert/issue-265" ), pomDir.toFile() );
79+
RevertMojo myMojo = (RevertMojo) mojoRule.lookupConfiguredMojo(
80+
new File( pomDir.toString(), "aggregate" ), "revert" );
81+
myMojo.execute();
82+
83+
assertThat( join( "\n", Files.readAllLines( pomDir.resolve( "aggregate/pom.xml" ) ) ),
84+
containsString( "OLD" ) );
85+
assertThat( Files.exists( pomDir.resolve( "aggregate/pom.xml.versionsBackup" ) ), is( false ) );
86+
assertThat( join( "\n", Files.readAllLines( pomDir.resolve( "module-a/pom.xml" ) ) ),
87+
containsString( "OLD" ) );
88+
assertThat( Files.exists( pomDir.resolve( "module-a/pom.xml.versionsBackup" ) ), is( false ) );
89+
assertThat( join( "\n", Files.readAllLines( pomDir.resolve( "module-b/pom.xml" ) ) ),
90+
containsString( "OLD" ) );
91+
assertThat( Files.exists( pomDir.resolve( "module-b/pom.xml.versionsBackup" ) ), is( false ) );
92+
}
93+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<!--
2+
~ Licensed to the Apache Software Foundation (ASF) under one
3+
~ or more contributor license agreements. See the NOTICE file
4+
~ distributed with this work for additional information
5+
~ regarding copyright ownership. The ASF licenses this file
6+
~ to you under the Apache License, Version 2.0 (the
7+
~ "License"); you may not use this file except in compliance
8+
~ with the License. You may obtain a copy of the License at
9+
~
10+
~ http://www.apache.org/licenses/LICENSE-2.0
11+
~
12+
~ Unless required by applicable law or agreed to in writing,
13+
~ software distributed under the License is distributed on an
14+
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
~ KIND, either express or implied. See the License for the
16+
~ specific language governing permissions and limitations
17+
~ under the License.
18+
-->
19+
20+
<project>
21+
<modelVersion>4.0.0</modelVersion>
22+
<groupId>localhost</groupId>
23+
<artifactId>dummy-artifact</artifactId>
24+
<version>NEW</version>
25+
<packaging>pom</packaging>
26+
27+
<modules>
28+
<module>../module-a</module>
29+
</modules>
30+
31+
<profiles>
32+
<profile>
33+
<id>module-b</id>
34+
35+
<modules>
36+
<module>../module-b</module>
37+
</modules>
38+
</profile>
39+
</profiles>
40+
41+
<build>
42+
<plugins>
43+
<plugin>
44+
<groupId>org.codehaus.mojo</groupId>
45+
<artifactId>versions-maven-plugin</artifactId>
46+
<goals>
47+
<goal>revert</goal>
48+
</goals>
49+
</plugin>
50+
</plugins>
51+
</build>
52+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<project>
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>localhost</groupId>
4+
<artifactId>dummy-artifact</artifactId>
5+
<version>OLD</version>
6+
<packaging>pom</packaging>
7+
8+
<modules>
9+
<module>../module-a</module>
10+
</modules>
11+
12+
<profiles>
13+
<profile>
14+
<id>module-b</id>
15+
16+
<modules>
17+
<module>../module-b</module>
18+
</modules>
19+
</profile>
20+
</profiles>
21+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<project>
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>localhost</groupId>
4+
<artifactId>dummy-artifact-a</artifactId>
5+
<version>1.0.0</version>
6+
<packaging>pom</packaging>
7+
8+
<dependencies>
9+
<dependency>
10+
<groupId>localhost</groupId>
11+
<artifactId>dummy-artifact</artifactId>
12+
<version>NEW</version>
13+
</dependency>
14+
</dependencies>
15+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<project>
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>localhost</groupId>
4+
<artifactId>dummy-artifact-a</artifactId>
5+
<version>1.0.0</version>
6+
<packaging>pom</packaging>
7+
8+
<dependencies>
9+
<dependency>
10+
<groupId>localhost</groupId>
11+
<artifactId>dummy-artifact</artifactId>
12+
<version>OLD</version>
13+
</dependency>
14+
</dependencies>
15+
</project>

0 commit comments

Comments
 (0)