Skip to content

Commit 171412b

Browse files
committed
Warn when profile ID matches a lifecycle phase name
Emit a validation warning when a profile ID matches a default Maven lifecycle phase (clean, compile, test, package, deploy, etc.). Using lifecycle phase names as profile IDs can lead to accidental phase execution when the name is misused on the command line. Closes #10310
1 parent 459de76 commit 171412b

5 files changed

Lines changed: 94 additions & 2 deletions

File tree

impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelValidator.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,34 @@ public class DefaultModelValidator implements ModelValidator {
9999

100100
private static final String EMPTY = "";
101101

102+
private static final Set<String> LIFECYCLE_PHASES = Set.of(
103+
"clean",
104+
"validate",
105+
"initialize",
106+
"generate-sources",
107+
"process-sources",
108+
"generate-resources",
109+
"process-resources",
110+
"compile",
111+
"process-classes",
112+
"generate-test-sources",
113+
"process-test-sources",
114+
"generate-test-resources",
115+
"process-test-resources",
116+
"test-compile",
117+
"process-test-classes",
118+
"test",
119+
"prepare-package",
120+
"package",
121+
"pre-integration-test",
122+
"integration-test",
123+
"post-integration-test",
124+
"verify",
125+
"install",
126+
"deploy",
127+
"site",
128+
"site-deploy");
129+
102130
private record ActivationFrame(String location, Optional<? extends InputLocationTracker> parent) {}
103131

104132
private static class ActivationWalker extends MavenTransformer {
@@ -564,6 +592,20 @@ && equals(parent.getArtifactId(), model.getArtifactId())) {
564592

565593
validateProfileId(prefix, "id", problems, Severity.ERROR, Version.V40, profile.getId(), null, model);
566594

595+
if (profile.getId() != null && LIFECYCLE_PHASES.contains(profile.getId())) {
596+
addViolation(
597+
problems,
598+
Severity.WARNING,
599+
Version.BASE,
600+
"profiles.profile.id",
601+
null,
602+
"Profile has the same id '"
603+
+ profile.getId() + "' as a Maven lifecycle phase. "
604+
+ "This can accidentally trigger the lifecycle phase "
605+
+ "when the profile name is misused on the command line.",
606+
profile);
607+
}
608+
567609
if (!profileIds.add(profile.getId())) {
568610
addViolation(
569611
problems,

impl/maven-impl/src/test/java/org/apache/maven/impl/model/DefaultModelValidatorTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,18 @@ void testDuplicateProfileId() throws Exception {
481481
assertTrue(result.getErrors().get(0).contains("non-unique-id"));
482482
}
483483

484+
@Test
485+
void testLifecyclePhaseProfileId() throws Exception {
486+
SimpleProblemCollector result = validateFile("lifecycle-phase-profile-id.xml");
487+
488+
assertViolations(result, 0, 0, 2);
489+
490+
assertTrue(result.getWarnings().get(0).contains("integration-test"));
491+
assertTrue(result.getWarnings().get(0).contains("lifecycle phase"));
492+
assertTrue(result.getWarnings().get(1).contains("deploy"));
493+
assertTrue(result.getWarnings().get(1).contains("lifecycle phase"));
494+
}
495+
484496
@Test
485497
void testBadPluginVersion() throws Exception {
486498
SimpleProblemCollector result = validate("bad-plugin-version.xml");

impl/maven-impl/src/test/resources/poms/validation/duplicate-plugin-execution.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ under the License.
6161

6262
<profiles>
6363
<profile>
64-
<id>test</id>
64+
<id>test-profile</id>
6565
<build>
6666
<pluginManagement>
6767
<plugins>

impl/maven-impl/src/test/resources/poms/validation/duplicate-plugin.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ under the License.
5151

5252
<profiles>
5353
<profile>
54-
<id>test</id>
54+
<id>test-profile</id>
5555
<build>
5656
<pluginManagement>
5757
<plugins>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
<artifactId>aid</artifactId>
23+
<groupId>gid</groupId>
24+
<version>0.1</version>
25+
<packaging>pom</packaging>
26+
27+
<profiles>
28+
<profile>
29+
<id>integration-test</id>
30+
</profile>
31+
<profile>
32+
<id>deploy</id>
33+
</profile>
34+
<profile>
35+
<id>my-custom-profile</id>
36+
</profile>
37+
</profiles>
38+
</project>

0 commit comments

Comments
 (0)