19
19
* under the License.
20
20
*/
21
21
22
- import java .io .File ;
22
+ import javax .inject .Inject ;
23
+
23
24
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 ;
24
29
30
+ import org .apache .maven .artifact .repository .ArtifactRepository ;
25
31
import org .apache .maven .plugin .AbstractMojo ;
26
32
import org .apache .maven .plugin .MojoExecutionException ;
27
33
import org .apache .maven .plugin .MojoFailureException ;
28
34
import org .apache .maven .plugins .annotations .Mojo ;
29
35
import org .apache .maven .plugins .annotations .Parameter ;
30
36
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 ;
32
41
33
42
/**
34
43
* Restores the pom from the initial backup.
37
46
* @since 1.0-alpha-3
38
47
*/
39
48
@ Mojo ( name = "revert" , threadSafe = true )
40
- public class RevertMojo
41
- extends AbstractMojo
49
+ public class RevertMojo extends AbstractMojo
42
50
{
43
51
/**
44
52
* The Maven Project.
@@ -48,24 +56,62 @@ public class RevertMojo
48
56
@ Parameter ( defaultValue = "${project}" , required = true , readonly = true )
49
57
private MavenProject project ;
50
58
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
53
81
{
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 ;
56
85
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 ->
58
91
{
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 ) )
66
96
{
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
+ }
68
114
}
69
- }
115
+ } );
70
116
}
71
117
}
0 commit comments