Skip to content

Commit 62d876e

Browse files
committed
mojohaus#1310: Handling a similar case in SetMojo, SetScmTagMojo, UpdateChildModulesMojo
1 parent 3952768 commit 62d876e

20 files changed

Lines changed: 483 additions & 58 deletions

File tree

versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/SetMojo.java

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,12 @@ public class SetMojo extends AbstractVersionsUpdaterMojo {
213213
protected Integer nextSnapshotIndexToIncrement;
214214

215215
/**
216-
* Whether to start processing at the local aggregation root (which might be a parent module
216+
* <p>Whether to start processing at the local aggregation root (which might be a parent module
217217
* of that module where Maven is executed in, and the version change may affect parent and sibling modules).
218-
* Setting to false makes sure only the module (and it's submodules) where Maven is executed for is affected.
218+
* Setting to false makes sure only the module (and its submodules) where Maven is executed for is affected.</p>
219+
*
220+
* <p>Since 2.20.2: If the plugin is executed with a project list (Maven CLI option {@code -pl}),
221+
* the plugin is always executed on every project on the list as local aggregation root.</p>
219222
*
220223
* @since 2.9
221224
*/
@@ -301,10 +304,19 @@ private synchronized void addChange(String groupId, String artifactId, String ol
301304
* Called when this mojo is executed.
302305
*
303306
* @throws org.apache.maven.plugin.MojoExecutionException when things go wrong.
304-
* @throws org.apache.maven.plugin.MojoFailureException when things go wrong.
305307
*/
306-
public void execute() throws MojoExecutionException, MojoFailureException {
307-
if (getProject().getOriginalModel().getVersion() == null) {
308+
public void execute() throws MojoExecutionException {
309+
for (MavenProject currentProject : session.getProjects()) {
310+
if (getLog().isDebugEnabled() && session.getProjects().size() > 1) {
311+
getLog().debug("Processing " + project.getGroupId() + ":" + project.getArtifactId() + ":"
312+
+ project.getVersion() + "...");
313+
}
314+
execute(currentProject);
315+
}
316+
}
317+
318+
private void execute(MavenProject currentProject) throws MojoExecutionException {
319+
if (currentProject.getOriginalModel().getVersion() == null) {
308320
throw new MojoExecutionException("Project version is inherited from parent.");
309321
}
310322

@@ -335,7 +347,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
335347
try {
336348
newVersion = prompter.prompt(
337349
"Enter the new version to set",
338-
getProject().getOriginalModel().getVersion());
350+
currentProject.getOriginalModel().getVersion());
339351
} catch (PrompterException e) {
340352
throw new MojoExecutionException(e.getMessage(), e);
341353
}
@@ -354,9 +366,9 @@ public void execute() throws MojoExecutionException, MojoFailureException {
354366
}
355367

356368
try {
357-
final MavenProject project = processFromLocalAggregationRoot
369+
final MavenProject project = session.getProjects().size() == 1 && processFromLocalAggregationRoot
358370
? PomHelper.getLocalRoot(projectBuilder, session, getLog())
359-
: getProject();
371+
: currentProject;
360372

361373
getLog().info("Local aggregation root: " + project.getBasedir());
362374
Map<File, Model> reactorModels = PomHelper.getChildModels(project, getLog());
@@ -370,7 +382,11 @@ public void execute() throws MojoExecutionException, MojoFailureException {
370382
// needs to be changed as well
371383
// setting them to the main project coordinates in case they are not set by the user,
372384
// so that the main project can be selected
373-
Model rootModel = reactorModels.get(session.getCurrentProject().getFile());
385+
Model rootModel = reactorModels.get(currentProject.getFile());
386+
387+
String groupId = this.groupId;
388+
String artifactId = this.artifactId;
389+
String oldVersion = this.oldVersion;
374390
if (groupId == null) {
375391
groupId = PomHelper.getGroupId(rootModel);
376392
}
@@ -429,11 +445,10 @@ public void execute() throws MojoExecutionException, MojoFailureException {
429445
process(file);
430446
}
431447

432-
} catch (IOException e) {
448+
} catch (IOException | MojoFailureException e) {
433449
throw new MojoExecutionException(e.getMessage(), e);
434450
}
435451
}
436-
437452
/**
438453
* Returns the incremented version, with the nextSnapshotIndexToIncrement indicating the 1-based index,
439454
* from the left, or the most major version component, of the version string.

versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/SetScmTagMojo.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import javax.inject.Inject;
44
import javax.xml.stream.XMLStreamException;
55

6+
import java.io.File;
67
import java.io.IOException;
78
import java.util.ArrayList;
89
import java.util.List;
@@ -13,6 +14,7 @@
1314
import org.apache.maven.plugin.MojoFailureException;
1415
import org.apache.maven.plugins.annotations.Mojo;
1516
import org.apache.maven.plugins.annotations.Parameter;
17+
import org.apache.maven.project.MavenProject;
1618
import org.apache.maven.wagon.Wagon;
1719
import org.codehaus.mojo.versions.api.PomHelper;
1820
import org.codehaus.mojo.versions.api.recording.ChangeRecorder;
@@ -104,12 +106,23 @@ protected boolean getAllowSnapshots() {
104106
*/
105107
@Override
106108
public void execute() throws MojoExecutionException, MojoFailureException {
109+
validateInput();
110+
for (MavenProject currentProject : session.getProjects()) {
111+
if (getLog().isDebugEnabled() && session.getProjects().size() > 1) {
112+
getLog().debug("Processing " + project.getGroupId() + ":" + project.getArtifactId() + ":"
113+
+ project.getVersion() + "...");
114+
}
115+
File outFile = currentProject.getFile();
116+
process(outFile);
117+
}
118+
}
119+
120+
@Override
121+
protected void validateInput() throws MojoExecutionException {
107122
if (isAllBlank(newTag, connection, developerConnection, url)) {
108-
throw new MojoFailureException(
123+
throw new MojoExecutionException(
109124
"One of: \"newTag\", \"connection\", \"developerConnection\", \"url\" should be provided.");
110125
}
111-
112-
super.execute();
113126
}
114127

115128
@Override

versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UpdateChildModulesMojo.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.apache.maven.plugin.MojoFailureException;
3636
import org.apache.maven.plugins.annotations.Mojo;
3737
import org.apache.maven.plugins.annotations.Parameter;
38+
import org.apache.maven.project.MavenProject;
3839
import org.apache.maven.wagon.Wagon;
3940
import org.codehaus.mojo.versions.api.PomHelper;
4041
import org.codehaus.mojo.versions.api.recording.ChangeRecorder;
@@ -105,11 +106,21 @@ protected boolean getAllowSnapshots() {
105106
* @throws MojoFailureException when things go wrong.
106107
*/
107108
public void execute() throws MojoExecutionException, MojoFailureException {
109+
validateInput();
110+
for (MavenProject currentProject : session.getProjects()) {
111+
if (getLog().isDebugEnabled() && session.getProjects().size() > 1) {
112+
getLog().debug("Processing " + project.getGroupId() + ":" + project.getArtifactId() + ":"
113+
+ project.getVersion() + "...");
114+
}
115+
execute(currentProject);
116+
}
117+
}
108118

119+
private void execute(MavenProject currentProject) throws MojoExecutionException {
109120
boolean didSomething = false;
110121

111122
try {
112-
final Map<File, Model> reactor = PomHelper.getChildModels(getProject(), getLog());
123+
final Map<File, Model> reactor = PomHelper.getChildModels(currentProject, getLog());
113124
List<File> order = new ArrayList<>(reactor.keySet());
114125
order.sort((o1, o2) -> {
115126
Model m1 = reactor.get(o1);
@@ -180,7 +191,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
180191
}
181192
}
182193

183-
} catch (IOException e) {
194+
} catch (IOException | MojoFailureException e) {
184195
throw new MojoExecutionException(e.getMessage(), e);
185196
}
186197
if (!didSomething) {
@@ -196,6 +207,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
196207
* @throws MojoFailureException when things go wrong.
197208
* @throws XMLStreamException when things go wrong.
198209
*/
210+
@Override
199211
protected synchronized void update(MutableXMLStreamReader pom)
200212
throws MojoExecutionException, MojoFailureException, XMLStreamException {
201213
getLog().debug("Updating parent to " + sourceVersion);

versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/SetMojoTest.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
import java.nio.file.Files;
66
import java.nio.file.Path;
77
import java.nio.file.Paths;
8+
import java.util.Collections;
89
import java.util.function.Consumer;
910
import java.util.stream.Collectors;
1011
import java.util.stream.Stream;
1112

1213
import org.apache.commons.lang3.tuple.Triple;
1314
import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
15+
import org.apache.maven.execution.MavenSession;
1416
import org.apache.maven.model.Model;
1517
import org.apache.maven.plugin.MojoExecutionException;
1618
import org.apache.maven.plugin.MojoFailureException;
@@ -29,13 +31,16 @@
2931
import org.mockito.Mock;
3032

3133
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
34+
import static org.codehaus.mojo.versions.utils.MockUtils.mockAetherRepositorySystem;
3235
import static org.codehaus.mojo.versions.utils.MockUtils.mockArtifactHandlerManager;
3336
import static org.hamcrest.MatcherAssert.assertThat;
3437
import static org.hamcrest.Matchers.containsString;
3538
import static org.hamcrest.Matchers.is;
3639
import static org.hamcrest.Matchers.matchesPattern;
3740
import static org.hamcrest.Matchers.matchesRegex;
3841
import static org.hamcrest.Matchers.not;
42+
import static org.mockito.Mockito.doReturn;
43+
import static org.mockito.Mockito.mock;
3944
import static org.mockito.MockitoAnnotations.openMocks;
4045

4146
public class SetMojoTest extends AbstractMojoTestCase {
@@ -121,7 +126,8 @@ public void testNextSnapshotIndexWithoutNextSnapshot() throws MojoFailureExcepti
121126
project.setParent(new MavenProject());
122127
project.setOriginalModel(new Model());
123128
project.getOriginalModel().setVersion("1.2.3-SNAPSHOT");
124-
129+
session = mock(MavenSession.class);
130+
doReturn(Collections.singletonList(project)).when(session).getProjects();
125131
nextSnapshotIndexToIncrement = 4;
126132
}
127133
}.execute();
@@ -277,4 +283,29 @@ public void testNextSnapshotIndexToIncrement() throws MojoExecutionException {
277283
}
278284
};
279285
}
286+
287+
/**
288+
* Tests against a case where {@link SetMojo} is executed on a module project list
289+
* (that is, if a -pl parameter is used providing a list of modules to process).
290+
*
291+
* @throws Exception thrown if something goes not according to plan
292+
*/
293+
@Test
294+
public void testModuleList() throws Exception {
295+
TestUtils.copyDir(Paths.get("src/test/resources/org/codehaus/mojo/set/module-list"), tempDir);
296+
297+
MavenSession session = TestUtils.createMavenSession(
298+
mojoRule.getContainer(), mojoRule::readMavenProject, tempDir, "mod1", "mod2");
299+
SetMojo mojo = mojoRule.lookupConfiguredMojo(session, newMojoExecution("set"));
300+
setVariableValueToObject(mojo, "newVersion", "2.0.0");
301+
setVariableValueToObject(mojo, "repositorySystem", mockAetherRepositorySystem());
302+
setVariableValueToObject(mojo, "log", log);
303+
304+
mojo.execute();
305+
306+
String mod1 = String.join("", Files.readAllLines(tempDir.resolve("mod1/pom.xml")));
307+
String mod2 = String.join("", Files.readAllLines(tempDir.resolve("mod2/pom.xml")));
308+
assertThat(mod1, containsString("<version>2.0.0</version>"));
309+
assertThat(mod2, containsString("<version>2.0.0</version>"));
310+
}
280311
}

versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/SetScmTagMojoTest.java

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,27 @@
1919
* under the License.
2020
*/
2121

22+
import java.io.IOException;
2223
import java.nio.file.Files;
2324
import java.nio.file.Path;
2425
import java.nio.file.Paths;
26+
import java.util.Arrays;
2527

28+
import org.apache.maven.execution.MavenSession;
29+
import org.apache.maven.plugin.logging.Log;
2630
import org.apache.maven.plugin.testing.AbstractMojoTestCase;
2731
import org.apache.maven.plugin.testing.MojoRule;
32+
import org.codehaus.mojo.versions.utils.TestUtils;
33+
import org.junit.After;
34+
import org.junit.Before;
2835
import org.junit.Rule;
2936
import org.junit.Test;
37+
import org.mockito.Mock;
3038

3139
import static org.hamcrest.MatcherAssert.assertThat;
3240
import static org.hamcrest.Matchers.allOf;
3341
import static org.hamcrest.Matchers.matchesPattern;
42+
import static org.mockito.MockitoAnnotations.openMocks;
3443

3544
/**
3645
* Basic tests for {@linkplain SetPropertyMojoTest}.
@@ -41,12 +50,29 @@ public class SetScmTagMojoTest extends AbstractMojoTestCase {
4150
@Rule
4251
public MojoRule mojoRule = new MojoRule(this);
4352

53+
private Path tempDir;
54+
55+
@Mock
56+
protected Log log;
57+
58+
@Before
59+
public void setUp() throws Exception {
60+
super.setUp();
61+
openMocks(this);
62+
tempDir = TestUtils.createTempDir("set");
63+
}
64+
65+
@After
66+
public void tearDown() throws IOException {
67+
TestUtils.tearDownTempDir(tempDir);
68+
}
69+
4470
@Test
4571
public void testNewScmValues() throws Exception {
46-
Path pomFile = Paths.get("target/test-classes/org/codehaus/mojo/set-scm-tag/pom.xml");
47-
mojoRule.lookupConfiguredMojo(pomFile.toFile().getParentFile(), "set-scm-tag")
48-
.execute();
49-
String output = String.join("", Files.readAllLines(pomFile)).replaceAll("\\s*", "");
72+
TestUtils.copyDir(Paths.get("src/test/resources/org/codehaus/mojo/set-scm-tag/new-scm-values"), tempDir);
73+
mojoRule.lookupConfiguredMojo(tempDir.toFile(), "set-scm-tag").execute();
74+
String output =
75+
String.join("", Files.readAllLines(tempDir.resolve("pom.xml"))).replaceAll("\\s*", "");
5076
assertThat(
5177
output,
5278
allOf(
@@ -56,4 +82,36 @@ public void testNewScmValues() throws Exception {
5682
matchesPattern(".*<scm>.*<developerConnection>\\s*"
5783
+ "developerConnection\\s*</developerConnection>.*</scm>.*")));
5884
}
85+
86+
/**
87+
* Tests against a case where {@link SetScmTagMojo} is executed on a module project list
88+
* (that is, if a -pl parameter is used providing a list of modules to process).
89+
*
90+
* @throws Exception thrown if something goes not according to plan
91+
*/
92+
@Test
93+
public void testModuleList() throws Exception {
94+
TestUtils.copyDir(Paths.get("src/test/resources/org/codehaus/mojo/set-scm-tag/module-list"), tempDir);
95+
96+
MavenSession session = TestUtils.createMavenSession(
97+
mojoRule.getContainer(), mojoRule::readMavenProject, tempDir, "mod1", "mod2");
98+
SetScmTagMojo mojo = mojoRule.lookupConfiguredMojo(session, newMojoExecution("set-scm-tag"));
99+
setVariableValueToObject(mojo, "newTag", "newTag");
100+
setVariableValueToObject(mojo, "connection", "connection");
101+
setVariableValueToObject(mojo, "developerConnection", "developerConnection");
102+
setVariableValueToObject(mojo, "url", "url");
103+
mojo.execute();
104+
for (String project : Arrays.asList("mod1", "mod2")) {
105+
Path pomFile = tempDir.resolve(project).resolve("pom.xml");
106+
String output = String.join("", Files.readAllLines(pomFile)).replaceAll("\\s*", "");
107+
assertThat(
108+
output,
109+
allOf(
110+
matchesPattern(".*<scm>.*<tag>\\s*newTag\\s*</tag>.*</scm>.*"),
111+
matchesPattern(".*<scm>.*<url>\\s*url\\s*</url>.*</scm>.*"),
112+
matchesPattern(".*<scm>.*<connection>\\s*connection\\s*</connection>.*</scm>.*"),
113+
matchesPattern(".*<scm>.*<developerConnection>\\s*"
114+
+ "developerConnection\\s*</developerConnection>.*</scm>.*")));
115+
}
116+
}
59117
}

0 commit comments

Comments
 (0)