diff --git a/impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelValidator.java b/impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelValidator.java index 72917cd7c635..7a90c606e19a 100644 --- a/impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelValidator.java +++ b/impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelValidator.java @@ -99,6 +99,34 @@ public class DefaultModelValidator implements ModelValidator { private static final String EMPTY = ""; + private static final Set LIFECYCLE_PHASES = Set.of( + "clean", + "validate", + "initialize", + "generate-sources", + "process-sources", + "generate-resources", + "process-resources", + "compile", + "process-classes", + "generate-test-sources", + "process-test-sources", + "generate-test-resources", + "process-test-resources", + "test-compile", + "process-test-classes", + "test", + "prepare-package", + "package", + "pre-integration-test", + "integration-test", + "post-integration-test", + "verify", + "install", + "deploy", + "site", + "site-deploy"); + private record ActivationFrame(String location, Optional parent) {} private static class ActivationWalker extends MavenTransformer { @@ -564,6 +592,20 @@ && equals(parent.getArtifactId(), model.getArtifactId())) { validateProfileId(prefix, "id", problems, Severity.ERROR, Version.V40, profile.getId(), null, model); + if (profile.getId() != null && LIFECYCLE_PHASES.contains(profile.getId())) { + addViolation( + problems, + Severity.WARNING, + Version.BASE, + "profiles.profile.id", + null, + "Profile has the same id '" + + profile.getId() + "' as a Maven lifecycle phase. " + + "This can accidentally trigger the lifecycle phase " + + "when the profile name is misused on the command line.", + profile); + } + if (!profileIds.add(profile.getId())) { addViolation( problems, diff --git a/impl/maven-impl/src/test/java/org/apache/maven/impl/model/DefaultModelValidatorTest.java b/impl/maven-impl/src/test/java/org/apache/maven/impl/model/DefaultModelValidatorTest.java index df38b262f720..8c9595dfdf40 100644 --- a/impl/maven-impl/src/test/java/org/apache/maven/impl/model/DefaultModelValidatorTest.java +++ b/impl/maven-impl/src/test/java/org/apache/maven/impl/model/DefaultModelValidatorTest.java @@ -481,6 +481,18 @@ void testDuplicateProfileId() throws Exception { assertTrue(result.getErrors().get(0).contains("non-unique-id")); } + @Test + void testLifecyclePhaseProfileId() throws Exception { + SimpleProblemCollector result = validateFile("lifecycle-phase-profile-id.xml"); + + assertViolations(result, 0, 0, 2); + + assertTrue(result.getWarnings().get(0).contains("integration-test")); + assertTrue(result.getWarnings().get(0).contains("lifecycle phase")); + assertTrue(result.getWarnings().get(1).contains("deploy")); + assertTrue(result.getWarnings().get(1).contains("lifecycle phase")); + } + @Test void testBadPluginVersion() throws Exception { SimpleProblemCollector result = validate("bad-plugin-version.xml"); diff --git a/impl/maven-impl/src/test/resources/poms/validation/duplicate-plugin-execution.xml b/impl/maven-impl/src/test/resources/poms/validation/duplicate-plugin-execution.xml index 0d4256de25ae..891af72bb94a 100644 --- a/impl/maven-impl/src/test/resources/poms/validation/duplicate-plugin-execution.xml +++ b/impl/maven-impl/src/test/resources/poms/validation/duplicate-plugin-execution.xml @@ -61,7 +61,7 @@ under the License. - test + test-profile diff --git a/impl/maven-impl/src/test/resources/poms/validation/duplicate-plugin.xml b/impl/maven-impl/src/test/resources/poms/validation/duplicate-plugin.xml index 06a6a65776b2..6aa72fb741e8 100644 --- a/impl/maven-impl/src/test/resources/poms/validation/duplicate-plugin.xml +++ b/impl/maven-impl/src/test/resources/poms/validation/duplicate-plugin.xml @@ -51,7 +51,7 @@ under the License. - test + test-profile diff --git a/impl/maven-impl/src/test/resources/poms/validation/lifecycle-phase-profile-id.xml b/impl/maven-impl/src/test/resources/poms/validation/lifecycle-phase-profile-id.xml new file mode 100644 index 000000000000..9c2eb55ec8e6 --- /dev/null +++ b/impl/maven-impl/src/test/resources/poms/validation/lifecycle-phase-profile-id.xml @@ -0,0 +1,38 @@ + + + + 4.0.0 + aid + gid + 0.1 + pom + + + + integration-test + + + deploy + + + my-custom-profile + + +