Skip to content

Commit cc8ce50

Browse files
committed
[MNG-8245][MNG-8246] Warn when calling before: or after: phases
1 parent 0b34ce5 commit cc8ce50

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed

impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/DefaultLifecycleTaskSegmentCalculator.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.stream.Collectors;
2828
import java.util.stream.Stream;
2929

30+
import org.apache.maven.api.Lifecycle;
3031
import org.apache.maven.execution.MavenSession;
3132
import org.apache.maven.lifecycle.LifecycleNotFoundException;
3233
import org.apache.maven.lifecycle.LifecyclePhaseNotFoundException;
@@ -39,6 +40,8 @@
3940
import org.apache.maven.plugin.prefix.NoPluginFoundForPrefixException;
4041
import org.apache.maven.plugin.version.PluginVersionResolutionException;
4142
import org.apache.maven.project.MavenProject;
43+
import org.slf4j.Logger;
44+
import org.slf4j.LoggerFactory;
4245

4346
import static java.util.Objects.requireNonNull;
4447

@@ -53,6 +56,8 @@
5356
@Named
5457
@Singleton
5558
public class DefaultLifecycleTaskSegmentCalculator implements LifecycleTaskSegmentCalculator {
59+
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultLifecycleTaskSegmentCalculator.class);
60+
5661
private final MojoDescriptorCreator mojoDescriptorCreator;
5762

5863
private final LifecyclePluginResolver lifecyclePluginResolver;
@@ -95,6 +100,11 @@ public List<TaskSegment> calculateTaskSegments(MavenSession session, List<String
95100
TaskSegment currentSegment = null;
96101

97102
for (String task : tasks) {
103+
if (isBeforeOrAfterPhase(task)) {
104+
String prevTask = task;
105+
task = PhaseId.of(task).phase();
106+
LOGGER.warn("Illegal call to phase '{}'. The main phase '{}' will be used instead.", prevTask, task);
107+
}
98108
if (isGoalSpecification(task)) {
99109
// "pluginPrefix[:version]:goal" or "groupId:artifactId[:version]:goal"
100110

@@ -139,6 +149,10 @@ public boolean requiresProject(MavenSession session) {
139149
return false;
140150
}
141151

152+
private boolean isBeforeOrAfterPhase(String task) {
153+
return task.startsWith(Lifecycle.BEFORE) || task.startsWith(Lifecycle.AFTER);
154+
}
155+
142156
private boolean isGoalSpecification(String task) {
143157
return task.indexOf(':') >= 0;
144158
}

impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/concurrent/ConcurrentLifecycleStarter.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.stream.Collectors;
2828
import java.util.stream.Stream;
2929

30+
import org.apache.maven.api.Lifecycle;
3031
import org.apache.maven.execution.ExecutionEvent;
3132
import org.apache.maven.execution.MavenSession;
3233
import org.apache.maven.lifecycle.DefaultLifecycles;
@@ -38,6 +39,7 @@
3839
import org.apache.maven.lifecycle.internal.LifecycleStarter;
3940
import org.apache.maven.lifecycle.internal.LifecycleTask;
4041
import org.apache.maven.lifecycle.internal.MojoDescriptorCreator;
42+
import org.apache.maven.lifecycle.internal.PhaseId;
4143
import org.apache.maven.lifecycle.internal.ReactorBuildStatus;
4244
import org.apache.maven.lifecycle.internal.ReactorContext;
4345
import org.apache.maven.lifecycle.internal.TaskSegment;
@@ -138,6 +140,11 @@ public List<TaskSegment> calculateTaskSegments(MavenSession session, List<String
138140
TaskSegment currentSegment = null;
139141

140142
for (String task : tasks) {
143+
if (isBeforeOrAfterPhase(task)) {
144+
String prevTask = task;
145+
task = PhaseId.of(task).phase();
146+
logger.warn("Illegal call to phase '{}'. The main phase '{}' will be used instead.", prevTask, task);
147+
}
141148
if (isGoalSpecification(task)) {
142149
// "pluginPrefix[:version]:goal" or "groupId:artifactId[:version]:goal"
143150

@@ -185,6 +192,10 @@ private boolean requiresProject(MavenSession session) {
185192
return false;
186193
}
187194

195+
private boolean isBeforeOrAfterPhase(String task) {
196+
return task.startsWith(Lifecycle.BEFORE) || task.startsWith(Lifecycle.AFTER);
197+
}
198+
188199
private boolean isGoalSpecification(String task) {
189200
return task.indexOf(':') >= 0;
190201
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
package org.apache.maven.it;
20+
21+
import java.io.File;
22+
23+
import org.junit.jupiter.api.Test;
24+
25+
/**
26+
* This is a test set for <a href="https://issues.apache.org/jira/browse/MNG-8245">MNG-8245</a>
27+
* and <a href="https://issues.apache.org/jira/browse/MNG-8246">MNG-8246</a>.
28+
*/
29+
class MavenITmng8245BeforePhaseCliTest extends AbstractMavenIntegrationTestCase {
30+
31+
MavenITmng8245BeforePhaseCliTest() {
32+
super("[4.0.0-rc-2,)");
33+
}
34+
35+
/**
36+
* Verify phase before:clean spits a warning and calls clean
37+
*/
38+
@Test
39+
void testPhaseBeforeCleanAllWihConcurrentBuilder() throws Exception {
40+
File testDir = extractResources("/mng-8245-phase-all");
41+
42+
Verifier verifier = newVerifier(testDir.getAbsolutePath());
43+
verifier.setLogFileName("build-concurrent.txt");
44+
verifier.addCliArguments("-b", "concurrent", "before:clean");
45+
verifier.execute();
46+
47+
verifier.verifyTextInLog("Illegal call to phase 'before:clean'. The main phase 'clean' will be used instead.");
48+
verifier.verifyTextInLog("Hallo 'before:all' phase.");
49+
verifier.verifyTextInLog("Hallo 'after:all' phase.");
50+
}
51+
52+
/**
53+
* Verify phase before:clean spits a warning and calls clean
54+
*/
55+
@Test
56+
void testPhaseBeforeCleanAllWithLegacyBuilder() throws Exception {
57+
File testDir = extractResources("/mng-8244-phase-all");
58+
59+
Verifier verifier = newVerifier(testDir.getAbsolutePath());
60+
verifier.setLogFileName("build-legacy.txt");
61+
verifier.addCliArguments("before:clean");
62+
verifier.execute();
63+
64+
verifier.verifyTextInLog("Illegal call to phase 'before:clean'. The main phase 'clean' will be used instead.");
65+
verifier.verifyTextInLog("Hallo 'before:all' phase.");
66+
verifier.verifyTextInLog("Hallo 'after:all' phase.");
67+
}
68+
69+
/**
70+
* Verify phase after:clean spits a warning and calls clean
71+
*/
72+
@Test
73+
void testPhaseAFterCleanAllWihConcurrentBuilder() throws Exception {
74+
File testDir = extractResources("/mng-8245-phase-all");
75+
76+
Verifier verifier = newVerifier(testDir.getAbsolutePath());
77+
verifier.setLogFileName("build-concurrent.txt");
78+
verifier.addCliArguments("-b", "concurrent", "after:clean");
79+
verifier.execute();
80+
81+
verifier.verifyTextInLog("Illegal call to phase 'after:clean'. The main phase 'clean' will be used instead.");
82+
verifier.verifyTextInLog("Hallo 'before:clean' phase.");
83+
verifier.verifyTextInLog("Hallo 'after:clean' phase.");
84+
}
85+
86+
/**
87+
* Verify phase after:clean spits a warning and calls clean
88+
*/
89+
@Test
90+
void testPhaseAfterCleanAllWithLegacyBuilder() throws Exception {
91+
File testDir = extractResources("/mng-8244-phase-all");
92+
93+
Verifier verifier = newVerifier(testDir.getAbsolutePath());
94+
verifier.setLogFileName("build-legacy.txt");
95+
verifier.addCliArguments("after:clean");
96+
verifier.execute();
97+
98+
verifier.verifyTextInLog("Illegal call to phase 'after:clean'. The main phase 'clean' will be used instead.");
99+
verifier.verifyTextInLog("Hallo 'before:clean' phase.");
100+
verifier.verifyTextInLog("Hallo 'after:clean' phase.");
101+
}
102+
}

its/core-it-suite/src/test/java/org/apache/maven/it/TestSuiteOrdering.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ public TestSuiteOrdering() {
100100
* the tests are to finishing. Newer tests are also more likely to fail, so this is
101101
* a fail fast technique as well.
102102
*/
103+
suite.addTestSuite(MavenITmng8245BeforePhaseCliTest.class);
103104
suite.addTestSuite(MavenITmng8244PhaseAllTest.class);
104105
suite.addTestSuite(MavenITmng8421MavenEncryptionTest.class);
105106
suite.addTestSuite(MavenITmng8400CanonicalMavenHomeTest.class);

0 commit comments

Comments
 (0)