This is an automated email from the ASF dual-hosted git repository.
gnodet pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven.git
The following commit(s) were added to refs/heads/master by this push:
new b0da053613 Remove Profile getSource / setSource (#12377)
b0da053613 is described below
commit b0da05361335c48013367681201117f22b293fb5
Author: Guillaume Nodet <[email protected]>
AuthorDate: Wed Nov 18 20:03:06 2026 +0100
Remove Profile getSource / setSource (#12377)
* [#2569] Remove Profile getSource / setSource
Remove getSource()/setSource() and SOURCE_POM/SOURCE_SETTINGS constants
from the Profile class starting at model version 4.2.0. The 4.0.0 and
4.1.0 model versions retain these methods for backward compatibility.
Replace usages in the impl layer (DefaultProfileSelector,
SettingsUtilsV4, ConditionProfileActivator, PropertyProfileActivator)
with InputLocation/InputSource checks. POM-originating profiles are now
identified by having a non-null, non-empty InputSource.getModelId().
Fix compat-layer regression: SettingsUtils.convertFromSettingsProfile()
now explicitly sets SOURCE_SETTINGS so that the compat
DefaultProfileSelector
still correctly identifies settings-originating profiles.
Add tests for POM vs settings profile detection in DefaultProfileSelector.
* Address review: make isFromPom() private static in DefaultProfileSelector
and PropertyProfileActivator
---
api/maven-api-model/src/main/mdo/maven.mdo | 21 +-
.../org/apache/maven/settings/SettingsUtils.java | 4 +-
.../org/apache/maven/impl/SettingsUtilsV4.java | 4 +-
.../maven/impl/model/DefaultProfileSelector.java | 7 +-
.../model/profile/ConditionProfileActivator.java | 11 +-
.../impl/model/profile/ProfileSourceDetector.java | 65 ++++
.../model/profile/PropertyProfileActivator.java | 11 +-
.../impl/model/DefaultProfileSelectorTest.java | 364 +++++++++++++++++----
8 files changed, 419 insertions(+), 68 deletions(-)
diff --git a/api/maven-api-model/src/main/mdo/maven.mdo
b/api/maven-api-model/src/main/mdo/maven.mdo
index 7c7dc7ae67..8cdb64bc51 100644
--- a/api/maven-api-model/src/main/mdo/maven.mdo
+++ b/api/maven-api-model/src/main/mdo/maven.mdo
@@ -3026,12 +3026,14 @@
public static final String SOURCE_SETTINGS = "settings.xml";
+ private String source = SOURCE_POM;
+
public void setSource(String source) {
- getDelegate().setSource(source);
+ this.source = source;
}
public String getSource() {
- return getDelegate().getSource();
+ return source;
}
/**
@@ -3044,7 +3046,7 @@
</code>
</codeSegment>
<codeSegment>
- <version>4.1.0+</version>
+ <version>4.1.0/4.1.99</version>
<code>
<![CDATA[
public static final String SOURCE_POM = "pom";
@@ -3068,6 +3070,19 @@
*/
public String toString() {
return "Profile {id: " + getId() + ", source: " + getSource() + "}";
+ }
+ ]]>
+ </code>
+ </codeSegment>
+ <codeSegment>
+ <version>4.2.0+</version>
+ <code>
+ <![CDATA[
+ /**
+ * @see java.lang.Object#toString()
+ */
+ public String toString() {
+ return "Profile {id: " + getId() + "}";
}
]]>
</code>
diff --git
a/impl/maven-core/src/main/java/org/apache/maven/settings/SettingsUtils.java
b/impl/maven-core/src/main/java/org/apache/maven/settings/SettingsUtils.java
index e2ef69ff72..3ccf79ab60 100644
--- a/impl/maven-core/src/main/java/org/apache/maven/settings/SettingsUtils.java
+++ b/impl/maven-core/src/main/java/org/apache/maven/settings/SettingsUtils.java
@@ -54,8 +54,10 @@ public static Profile
convertToSettingsProfile(org.apache.maven.model.Profile mo
* @return a profile
*/
public static org.apache.maven.model.Profile
convertFromSettingsProfile(Profile settingsProfile) {
- return new org.apache.maven.model.Profile(
+ org.apache.maven.model.Profile result = new
org.apache.maven.model.Profile(
SettingsUtilsV4.convertFromSettingsProfile(settingsProfile.getDelegate()));
+ result.setSource(org.apache.maven.model.Profile.SOURCE_SETTINGS);
+ return result;
}
/**
diff --git
a/impl/maven-impl/src/main/java/org/apache/maven/impl/SettingsUtilsV4.java
b/impl/maven-impl/src/main/java/org/apache/maven/impl/SettingsUtilsV4.java
index 7481a28c18..14dd4c5c2a 100644
--- a/impl/maven-impl/src/main/java/org/apache/maven/impl/SettingsUtilsV4.java
+++ b/impl/maven-impl/src/main/java/org/apache/maven/impl/SettingsUtilsV4.java
@@ -266,9 +266,7 @@ public static org.apache.maven.api.model.Profile
convertFromSettingsProfile(Prof
.collect(Collectors.toList()));
}
- org.apache.maven.api.model.Profile value = profile.build();
- value.setSource("settings.xml");
- return value;
+ return profile.build();
}
/**
diff --git
a/impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultProfileSelector.java
b/impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultProfileSelector.java
index bd8e1724c3..392e9736d3 100644
---
a/impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultProfileSelector.java
+++
b/impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultProfileSelector.java
@@ -33,6 +33,7 @@
import org.apache.maven.api.services.model.ProfileActivationContext;
import org.apache.maven.api.services.model.ProfileActivator;
import org.apache.maven.api.services.model.ProfileSelector;
+import org.apache.maven.impl.model.profile.ProfileSourceDetector;
/**
* Calculates the active profiles among a given collection of profiles.
@@ -79,7 +80,7 @@ public List<Profile> getActiveProfiles(
boolean active = isActive(profile, context, problems);
boolean activeByDefault = isActiveByDefault(profile);
if (activated || active || activeByDefault) {
- if (Profile.SOURCE_POM.equals(profile.getSource())) {
+ if (isFromPom(profile)) {
if (activated || active) {
activePomProfiles.add(profile);
} else {
@@ -140,4 +141,8 @@ private boolean isActiveByDefault(Profile profile) {
Activation activation = profile.getActivation();
return activation != null && activation.isActiveByDefault();
}
+
+ private static boolean isFromPom(Profile profile) {
+ return ProfileSourceDetector.isFromPom(profile);
+ }
}
diff --git
a/impl/maven-impl/src/main/java/org/apache/maven/impl/model/profile/ConditionProfileActivator.java
b/impl/maven-impl/src/main/java/org/apache/maven/impl/model/profile/ConditionProfileActivator.java
index 63f8faa795..c82078d767 100644
---
a/impl/maven-impl/src/main/java/org/apache/maven/impl/model/profile/ConditionProfileActivator.java
+++
b/impl/maven-impl/src/main/java/org/apache/maven/impl/model/profile/ConditionProfileActivator.java
@@ -190,7 +190,7 @@ static String doGetProperty(Profile profile,
ProfileActivationContext context, S
// Check system properties
v = context.getSystemProperty(name);
}
- if (v == null && profile != null &&
Profile.SOURCE_POM.equals(profile.getSource())) {
+ if (v == null && profile != null && isFromPom(profile)) {
v = context.getCascadedProfileProperty(name);
if (v == null) {
v = context.getModelProperty(name);
@@ -198,4 +198,13 @@ static String doGetProperty(Profile profile,
ProfileActivationContext context, S
}
return v;
}
+
+ /**
+ * Determines whether the given profile originates from a POM file.
+ *
+ * @see ProfileSourceDetector#isFromPom(Profile)
+ */
+ private static boolean isFromPom(Profile profile) {
+ return ProfileSourceDetector.isFromPom(profile);
+ }
}
diff --git
a/impl/maven-impl/src/main/java/org/apache/maven/impl/model/profile/ProfileSourceDetector.java
b/impl/maven-impl/src/main/java/org/apache/maven/impl/model/profile/ProfileSourceDetector.java
new file mode 100644
index 0000000000..5377f9f370
--- /dev/null
+++
b/impl/maven-impl/src/main/java/org/apache/maven/impl/model/profile/ProfileSourceDetector.java
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.impl.model.profile;
+
+import org.apache.maven.api.model.InputLocation;
+import org.apache.maven.api.model.InputSource;
+import org.apache.maven.api.model.Profile;
+
+/**
+ * Utility for detecting the origin of a {@link Profile}.
+ *
+ * <p>Since model version 4.2.0, {@link Profile} no longer carries a mutable
{@code source}
+ * field. Origin is instead inferred from the {@link InputSource} attached to
the profile's
+ * root {@link InputLocation}: a non-null, non-empty {@code modelId} means the
profile was
+ * read from a POM file; an empty or null {@code modelId} indicates a settings
profile
+ * (settings.xml converted via {@code SettingsUtilsV4.toLocation}).
+ *
+ * @since 4.2.0
+ */
+public final class ProfileSourceDetector {
+
+ private ProfileSourceDetector() {}
+
+ /**
+ * Returns {@code true} if the given profile originates from a POM file.
+ *
+ * <p>Detection is based on the profile's root {@link InputLocation}: if
the attached
+ * {@link InputSource} has a non-null, non-empty {@code modelId}, the
profile is from a
+ * POM. Settings profiles converted via {@code SettingsUtilsV4} receive an
+ * {@code InputSource} with an empty {@code modelId}.
+ *
+ * <p>Defaults to {@code true} when no location information is available,
preserving
+ * the pre-4.2.0 behaviour.
+ *
+ * @param profile the profile to inspect; must not be {@code null}
+ * @return {@code true} if the profile is from a POM, {@code false} if
from settings
+ */
+ public static boolean isFromPom(Profile profile) {
+ InputLocation location = profile.getLocation("");
+ if (location != null) {
+ InputSource source = location.getSource();
+ if (source != null) {
+ String modelId = source.getModelId();
+ return modelId != null && !modelId.isEmpty();
+ }
+ }
+ return true;
+ }
+}
diff --git
a/impl/maven-impl/src/main/java/org/apache/maven/impl/model/profile/PropertyProfileActivator.java
b/impl/maven-impl/src/main/java/org/apache/maven/impl/model/profile/PropertyProfileActivator.java
index 5934595741..11154b7d08 100644
---
a/impl/maven-impl/src/main/java/org/apache/maven/impl/model/profile/PropertyProfileActivator.java
+++
b/impl/maven-impl/src/main/java/org/apache/maven/impl/model/profile/PropertyProfileActivator.java
@@ -103,7 +103,7 @@ public boolean isActive(Profile profile,
ProfileActivationContext context, Model
// Only cascaded properties (from previously-activated profiles) are
checked — the
// POM's own <properties> must NOT self-activate profiles via
PropertyProfileActivator.
// ONLY for POM profiles - settings profiles should not use cascaded
properties.
- if (sysValue == null &&
Profile.SOURCE_POM.equals(profile.getSource())) {
+ if (sysValue == null && isFromPom(profile)) {
sysValue = context.getCascadedProfileProperty(name);
}
@@ -161,4 +161,13 @@ public boolean presentInConfig(Profile profile,
ProfileActivationContext context
return property != null;
}
+
+ /**
+ * Determines whether the given profile originates from a POM file.
+ *
+ * @see ProfileSourceDetector#isFromPom(Profile)
+ */
+ private static boolean isFromPom(Profile profile) {
+ return ProfileSourceDetector.isFromPom(profile);
+ }
}
diff --git
a/impl/maven-impl/src/test/java/org/apache/maven/impl/model/DefaultProfileSelectorTest.java
b/impl/maven-impl/src/test/java/org/apache/maven/impl/model/DefaultProfileSelectorTest.java
index 6e256eaa42..73aee975d7 100644
---
a/impl/maven-impl/src/test/java/org/apache/maven/impl/model/DefaultProfileSelectorTest.java
+++
b/impl/maven-impl/src/test/java/org/apache/maven/impl/model/DefaultProfileSelectorTest.java
@@ -26,8 +26,13 @@
import org.apache.maven.api.model.Activation;
import org.apache.maven.api.model.ActivationFile;
import org.apache.maven.api.model.ActivationProperty;
+import org.apache.maven.api.model.InputLocation;
+import org.apache.maven.api.model.InputSource;
import org.apache.maven.api.model.Model;
import org.apache.maven.api.model.Profile;
+import org.apache.maven.api.services.ModelProblemCollector;
+import org.apache.maven.api.services.model.ProfileActivationContext;
+import org.apache.maven.api.services.model.ProfileActivator;
import org.apache.maven.impl.DefaultModelVersionParser;
import org.apache.maven.impl.model.profile.FileProfileActivator;
import org.apache.maven.impl.model.profile.PropertyProfileActivator;
@@ -41,7 +46,8 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
- * Tests {@link DefaultProfileSelector} with focus on cascading activation
behavior.
+ * Tests {@link DefaultProfileSelector} with focus on cascading activation
behavior
+ * and POM vs settings profile distinction via {@link InputSource} model id
(issue #2569).
*/
public class DefaultProfileSelectorTest {
@@ -60,8 +66,8 @@ void setUp() {
@Test
void testNonCascadingActivation() {
// Create profiles with property-based activation
- Profile profile1 = createProfile("profile1", "prop1", "value1",
Profile.SOURCE_POM);
- Profile profile2 = createProfile("profile2", "prop2", "value2",
Profile.SOURCE_POM);
+ Profile profile1 = createPomProfile("profile1", "prop1", "value1");
+ Profile profile2 = createPomProfile("profile2", "prop2", "value2");
List<Profile> profiles = Arrays.asList(profile1, profile2);
@@ -82,9 +88,8 @@ void testNonCascadingActivation() {
@Test
void testCascadingActivation() {
// Create profiles where one activates another through properties
- Profile profile1 = createProfileWithProperties(
- "profile1", "prop1", "value1", Map.of("prop2", "value2"),
Profile.SOURCE_POM);
- Profile profile2 = createProfile("profile2", "prop2", "value2",
Profile.SOURCE_POM);
+ Profile profile1 = createPomProfileWithProperties("profile1", "prop1",
"value1", Map.of("prop2", "value2"));
+ Profile profile2 = createPomProfile("profile2", "prop2", "value2");
List<Profile> profiles = Arrays.asList(profile1, profile2);
@@ -106,9 +111,8 @@ void testCascadingActivation() {
@Test
void testCascadingVsNonCascadingDifference() {
// Create profiles where cascading would activate more profiles
- Profile profile1 = createProfileWithProperties(
- "profile1", "prop1", "value1", Map.of("prop2", "value2"),
Profile.SOURCE_POM);
- Profile profile2 = createProfile("profile2", "prop2", "value2",
Profile.SOURCE_POM);
+ Profile profile1 = createPomProfileWithProperties("profile1", "prop1",
"value1", Map.of("prop2", "value2"));
+ Profile profile2 = createPomProfile("profile2", "prop2", "value2");
List<Profile> profiles = Arrays.asList(profile1, profile2);
@@ -124,8 +128,8 @@ void testCascadingVsNonCascadingDifference() {
@Test
void testActiveByDefaultProfiles() {
- Profile defaultProfile =
createActiveByDefaultProfile("default-profile", Profile.SOURCE_POM);
- Profile conditionalProfile = createProfile("conditional", "prop1",
"value1", Profile.SOURCE_POM);
+ Profile defaultProfile =
createPomActiveByDefaultProfile("default-profile");
+ Profile conditionalProfile = createPomProfile("conditional", "prop1",
"value1");
List<Profile> profiles = Arrays.asList(defaultProfile,
conditionalProfile);
@@ -147,8 +151,8 @@ void testActiveByDefaultProfiles() {
@Test
void testMixedSourceProfiles() {
- Profile pomProfile = createProfile("pom-profile", "prop1", "value1",
Profile.SOURCE_POM);
- Profile settingsProfile = createProfile("settings-profile", "prop2",
"value2", Profile.SOURCE_SETTINGS);
+ Profile pomProfile = createPomProfile("pom-profile", "prop1",
"value1");
+ Profile settingsProfile = createSettingsProfile("settings-profile",
"prop2", "value2");
List<Profile> profiles = Arrays.asList(pomProfile, settingsProfile);
@@ -178,8 +182,8 @@ void testEmptyProfilesList() {
@Test
void testExplicitlyActivatedProfiles() {
- Profile profile1 = createProfile("profile1", "nonexistent", "value",
Profile.SOURCE_POM);
- Profile profile2 = createProfile("profile2", "prop2", "value2",
Profile.SOURCE_POM);
+ Profile profile1 = createPomProfile("profile1", "nonexistent",
"value");
+ Profile profile2 = createPomProfile("profile2", "prop2", "value2");
List<Profile> profiles = Arrays.asList(profile1, profile2);
@@ -201,11 +205,9 @@ void testExplicitlyActivatedProfiles() {
@Test
void testCascadingActivationChain() {
// Create a chain of profiles: profile1 -> profile2 -> profile3
- Profile profile1 = createProfileWithProperties(
- "profile1", "prop1", "value1", Map.of("prop2", "value2"),
Profile.SOURCE_POM);
- Profile profile2 = createProfileWithProperties(
- "profile2", "prop2", "value2", Map.of("prop3", "value3"),
Profile.SOURCE_POM);
- Profile profile3 = createProfile("profile3", "prop3", "value3",
Profile.SOURCE_POM);
+ Profile profile1 = createPomProfileWithProperties("profile1", "prop1",
"value1", Map.of("prop2", "value2"));
+ Profile profile2 = createPomProfileWithProperties("profile2", "prop2",
"value2", Map.of("prop3", "value3"));
+ Profile profile3 = createPomProfile("profile3", "prop3", "value3");
List<Profile> profiles = Arrays.asList(profile1, profile2, profile3);
@@ -229,12 +231,10 @@ void testCascadingActivationChain() {
@Test
void testCascadingStopCondition() {
// Test that cascading stops when no more profiles can be activated
- Profile profile1 = createProfileWithProperties(
- "profile1", "prop1", "value1", Map.of("prop2", "value2"),
Profile.SOURCE_POM);
- Profile profile2 = createProfileWithProperties(
- "profile2", "prop2", "value2", Map.of("prop3", "value3"),
Profile.SOURCE_POM);
+ Profile profile1 = createPomProfileWithProperties("profile1", "prop1",
"value1", Map.of("prop2", "value2"));
+ Profile profile2 = createPomProfileWithProperties("profile2", "prop2",
"value2", Map.of("prop3", "value3"));
// profile3 requires prop4 which is never set, so cascading should stop
- Profile profile3 = createProfile("profile3", "prop4", "value4",
Profile.SOURCE_POM);
+ Profile profile3 = createPomProfile("profile3", "prop4", "value4");
List<Profile> profiles = Arrays.asList(profile1, profile2, profile3);
@@ -258,10 +258,8 @@ void testCascadingStopCondition() {
@Test
void testCascadingWithCircularDependency() {
// Test that cascading handles circular dependencies gracefully
- Profile profile1 = createProfileWithProperties(
- "profile1", "prop1", "value1", Map.of("prop2", "value2"),
Profile.SOURCE_POM);
- Profile profile2 = createProfileWithProperties(
- "profile2", "prop2", "value2", Map.of("prop1", "value1"),
Profile.SOURCE_POM);
+ Profile profile1 = createPomProfileWithProperties("profile1", "prop1",
"value1", Map.of("prop2", "value2"));
+ Profile profile2 = createPomProfileWithProperties("profile2", "prop2",
"value2", Map.of("prop1", "value1"));
List<Profile> profiles = Arrays.asList(profile1, profile2);
@@ -284,9 +282,8 @@ void testCascadingWithCircularDependency() {
@Test
void testCascadingWithInactiveProfile() {
// Create profiles where one would activate another, but the second is
explicitly deactivated
- Profile profile1 = createProfileWithProperties(
- "profile1", "prop1", "value1", Map.of("prop2", "value2"),
Profile.SOURCE_POM);
- Profile profile2 = createProfile("profile2", "prop2", "value2",
Profile.SOURCE_POM);
+ Profile profile1 = createPomProfileWithProperties("profile1", "prop1",
"value1", Map.of("prop2", "value2"));
+ Profile profile2 = createPomProfile("profile2", "prop2", "value2");
List<Profile> profiles = Arrays.asList(profile1, profile2);
@@ -318,11 +315,11 @@ void testActiveByDefaultDoesNotCascade() {
.id("default-on")
.activation(Activation.newBuilder().activeByDefault(true).build())
.properties(Map.of("prop2", "value2"))
+ .location("", newPomLocation())
.build();
- defaultProfile.setSource(Profile.SOURCE_POM);
- Profile cascadeTarget = createProfile("cascade-target", "prop2",
"value2", Profile.SOURCE_POM);
- Profile explicitProfile = createProfile("explicit", "trigger", "yes",
Profile.SOURCE_POM);
+ Profile cascadeTarget = createPomProfile("cascade-target", "prop2",
"value2");
+ Profile explicitProfile = createPomProfile("explicit", "trigger",
"yes");
List<Profile> profiles = Arrays.asList(defaultProfile, cascadeTarget,
explicitProfile);
@@ -345,9 +342,9 @@ void testActiveByDefaultDoesNotCascade() {
void testExplicitlyActivatedProfileDoesNotCascade() {
// Reproduces the Windows CI failure: -Prun-its sets mavenHome as a
profile property,
// which must NOT cascade to activate maven-from-build (which
activates on mavenHome).
- Profile runIts = createProfileWithProperties(
- "run-its", "nonexistent", "value", Map.of("mavenHome",
"target/apache-maven"), Profile.SOURCE_POM);
- Profile mavenFromBuild = createProfile("maven-from-build",
"mavenHome", null, Profile.SOURCE_POM);
+ Profile runIts = createPomProfileWithProperties(
+ "run-its", "nonexistent", "value", Map.of("mavenHome",
"target/apache-maven"));
+ Profile mavenFromBuild = createPomProfile("maven-from-build",
"mavenHome", null);
List<Profile> profiles = Arrays.asList(runIts, mavenFromBuild);
@@ -373,9 +370,9 @@ void testExplicitlyActivatedProfileDoesNotCascade() {
@Test
void testCascadingWithRecordImmutability() {
// Test that profile records remain immutable during cascading
- Profile originalProfile1 = createProfileWithProperties(
- "profile1", "prop1", "value1", Map.of("prop2", "value2"),
Profile.SOURCE_POM);
- Profile originalProfile2 = createProfile("profile2", "prop2",
"value2", Profile.SOURCE_POM);
+ Profile originalProfile1 =
+ createPomProfileWithProperties("profile1", "prop1", "value1",
Map.of("prop2", "value2"));
+ Profile originalProfile2 = createPomProfile("profile2", "prop2",
"value2");
List<Profile> profiles = Arrays.asList(originalProfile1,
originalProfile2);
@@ -404,8 +401,7 @@ void testFileExistenceCascade() {
String existingDir = System.getProperty("java.io.tmpdir");
// Profile 1: activates on trigger=yes, sets my.path to an existing
directory
- Profile profile1 = createProfileWithProperties(
- "profile1", "trigger", "yes", Map.of("my.path", existingDir),
Profile.SOURCE_POM);
+ Profile profile1 = createPomProfileWithProperties("profile1",
"trigger", "yes", Map.of("my.path", existingDir));
// Profile 2: activates on file existence using cascaded property
Profile profile2 = Profile.newBuilder()
@@ -413,8 +409,8 @@ void testFileExistenceCascade() {
.activation(Activation.newBuilder()
.file(ActivationFile.newBuilder().exists("${my.path}").build())
.build())
+ .location("", newPomLocation())
.build();
- profile2.setSource(Profile.SOURCE_POM);
List<Profile> profiles = Arrays.asList(profile1, profile2);
@@ -433,10 +429,173 @@ void testFileExistenceCascade() {
assertTrue(problems.getErrors().isEmpty());
}
- // Helper methods for creating test profiles
+ // ---- isFromPom() / activeByDefault: POM profiles (issue #2569) ----
- private Profile createProfile(String id, String propName, String
propValue, String source) {
- Profile profile = Profile.newBuilder()
+ @Test
+ void testPomActiveByDefaultProfileIsActivatedWhenNoOtherPomProfileActive()
{
+ Profile defaultProfile = newPomProfile("default", true);
+
+ List<Profile> active = selector.getActiveProfiles(
+ Collections.singletonList(defaultProfile), newContext(), new
SimpleProblemCollector());
+
+ assertEquals(1, active.size());
+ assertEquals("default", active.get(0).getId());
+ }
+
+ @Test
+ void
testPomActiveByDefaultProfileIsDeactivatedWhenAnotherPomProfileIsExplicitlyActive()
{
+ Profile defaultProfile = newPomProfile("default", true);
+ Profile explicitProfile = newPomProfile("explicit", false);
+
+ List<Profile> active = selector.getActiveProfiles(
+ Arrays.asList(defaultProfile, explicitProfile),
+ newContext(Collections.singletonList("explicit")),
+ new SimpleProblemCollector());
+
+ assertEquals(1, active.size());
+ assertEquals("explicit", active.get(0).getId());
+ }
+
+ // ---- isFromPom() / activeByDefault: settings profiles (issue #2569) ----
+
+ @Test
+ void testSettingsActiveByDefaultProfileIsAlwaysActivated() {
+ Profile settingsDefault = newSettingsProfile("settings-default", true);
+
+ List<Profile> active = selector.getActiveProfiles(
+ Collections.singletonList(settingsDefault), newContext(), new
SimpleProblemCollector());
+
+ assertEquals(1, active.size());
+ assertEquals("settings-default", active.get(0).getId());
+ }
+
+ @Test
+ void
testSettingsActiveByDefaultProfileIsNotSuppressedByExplicitPomProfile() {
+ Profile settingsDefault = newSettingsProfile("settings-default", true);
+ Profile pomExplicit = newPomProfile("pom-explicit", false);
+
+ List<Profile> active = selector.getActiveProfiles(
+ Arrays.asList(settingsDefault, pomExplicit),
+ newContext(Collections.singletonList("pom-explicit")),
+ new SimpleProblemCollector());
+
+ assertEquals(2, active.size());
+ assertTrue(active.stream().anyMatch(p ->
"settings-default".equals(p.getId())));
+ assertTrue(active.stream().anyMatch(p ->
"pom-explicit".equals(p.getId())));
+ }
+
+ @Test
+ void testSettingsProfileWithNullModelIdTreatedAsNonPom() {
+ Profile settingsDefault =
newSettingsProfileNullModelId("settings-null", true);
+ Profile pomExplicit = newPomProfile("pom-explicit", false);
+
+ List<Profile> active = selector.getActiveProfiles(
+ Arrays.asList(settingsDefault, pomExplicit),
+ newContext(Collections.singletonList("pom-explicit")),
+ new SimpleProblemCollector());
+
+ assertEquals(2, active.size());
+ assertTrue(active.stream().anyMatch(p ->
"settings-null".equals(p.getId())));
+ assertTrue(active.stream().anyMatch(p ->
"pom-explicit".equals(p.getId())));
+ }
+
+ // ---- mixed POM + settings profiles (issue #2569) ----
+
+ @Test
+ void testMixedPomAndSettingsActiveByDefault() {
+ Profile pomDefault = newPomProfile("pom-default", true);
+ Profile settingsDefault = newSettingsProfile("settings-default", true);
+
+ List<Profile> active = selector.getActiveProfiles(
+ Arrays.asList(pomDefault, settingsDefault), newContext(), new
SimpleProblemCollector());
+
+ // Both should activate: POM default (no other POM profile active) +
settings always
+ assertEquals(2, active.size());
+ assertTrue(active.stream().anyMatch(p ->
"pom-default".equals(p.getId())));
+ assertTrue(active.stream().anyMatch(p ->
"settings-default".equals(p.getId())));
+ }
+
+ @Test
+ void testExplicitPomProfileSuppressesPomDefaultButNotSettingsDefault() {
+ Profile pomDefault = newPomProfile("pom-default", true);
+ Profile pomExplicit = newPomProfile("pom-explicit", false);
+ Profile settingsDefault = newSettingsProfile("settings-default", true);
+
+ List<Profile> active = selector.getActiveProfiles(
+ Arrays.asList(pomDefault, pomExplicit, settingsDefault),
+ newContext(Collections.singletonList("pom-explicit")),
+ new SimpleProblemCollector());
+
+ // pom-default suppressed (another POM profile explicitly active),
settings-default stays
+ assertEquals(2, active.size());
+ assertTrue(active.stream().anyMatch(p ->
"pom-explicit".equals(p.getId())));
+ assertTrue(active.stream().anyMatch(p ->
"settings-default".equals(p.getId())));
+ }
+
+ // ---- edge case: profile with no location info (issue #2569) ----
+
+ @Test
+ void testProfileWithNoLocationDefaultsToPom() {
+ Profile noLocation = newProfileNoLocation("no-location", true);
+ Profile pomExplicit = newPomProfile("pom-explicit", false);
+
+ List<Profile> active = selector.getActiveProfiles(
+ Arrays.asList(noLocation, pomExplicit),
+ newContext(Collections.singletonList("pom-explicit")),
+ new SimpleProblemCollector());
+
+ // no-location defaults to "from POM", so it should be suppressed
+ assertEquals(1, active.size());
+ assertEquals("pom-explicit", active.get(0).getId());
+ }
+
+ // ---- throwing activator ----
+
+ @Test
+ void testThrowingActivator() {
+ DefaultProfileSelector localSelector = new DefaultProfileSelector();
+ localSelector.addProfileActivator(new ProfileActivator() {
+ @Override
+ public boolean isActive(Profile profile, ProfileActivationContext
context, ModelProblemCollector problems) {
+ throw new RuntimeException("BOOM");
+ }
+
+ @Override
+ public boolean presentInConfig(
+ Profile profile, ProfileActivationContext context,
ModelProblemCollector problems) {
+ return true;
+ }
+ });
+
+ List<Profile> profiles =
Collections.singletonList(newPomProfile("one", false));
+ SimpleProblemCollector localProblems = new SimpleProblemCollector();
+ List<Profile> active = localSelector.getActiveProfiles(profiles,
newContext(), localProblems);
+
+ assertTrue(active.isEmpty());
+ assertEquals(1, localProblems.getErrors().size());
+ assertEquals(
+ "Failed to determine activation for profile one: BOOM",
+ localProblems.getErrors().get(0));
+ }
+
+ // ---- Helper methods ----
+
+ /** Creates an InputLocation simulating a POM file origin (non-empty
modelId). */
+ private InputLocation newPomLocation() {
+ return InputLocation.of(1, 1,
InputSource.of("org.example:myproject:1.0", "pom.xml"));
+ }
+
+ /** Creates an InputLocation simulating a settings.xml origin (empty
modelId). */
+ private InputLocation newSettingsLocation() {
+ return InputLocation.of(1, 1, InputSource.of("",
"~/.m2/settings.xml"));
+ }
+
+ /**
+ * Creates a POM profile with property-based activation and a POM
InputLocation
+ * so that {@link DefaultProfileSelector#isFromPom} returns {@code true}.
+ */
+ private Profile createPomProfile(String id, String propName, String
propValue) {
+ return Profile.newBuilder()
.id(id)
.activation(Activation.newBuilder()
.property(ActivationProperty.newBuilder()
@@ -444,14 +603,33 @@ private Profile createProfile(String id, String propName,
String propValue, Stri
.value(propValue)
.build())
.build())
+ .location("", newPomLocation())
.build();
- profile.setSource(source);
- return profile;
}
- private Profile createProfileWithProperties(
- String id, String propName, String propValue, Map<String, String>
profileProperties, String source) {
- Profile profile = Profile.newBuilder()
+ /**
+ * Creates a settings profile with property-based activation and a
settings InputLocation
+ * so that {@link DefaultProfileSelector#isFromPom} returns {@code false}.
+ */
+ private Profile createSettingsProfile(String id, String propName, String
propValue) {
+ return Profile.newBuilder()
+ .id(id)
+ .activation(Activation.newBuilder()
+ .property(ActivationProperty.newBuilder()
+ .name(propName)
+ .value(propValue)
+ .build())
+ .build())
+ .location("", newSettingsLocation())
+ .build();
+ }
+
+ /**
+ * Creates a POM profile with property-based activation, extra properties,
and a POM InputLocation.
+ */
+ private Profile createPomProfileWithProperties(
+ String id, String propName, String propValue, Map<String, String>
profileProperties) {
+ return Profile.newBuilder()
.id(id)
.activation(Activation.newBuilder()
.property(ActivationProperty.newBuilder()
@@ -460,17 +638,87 @@ private Profile createProfileWithProperties(
.build())
.build())
.properties(profileProperties)
+ .location("", newPomLocation())
.build();
- profile.setSource(source);
- return profile;
}
- private Profile createActiveByDefaultProfile(String id, String source) {
- Profile profile = Profile.newBuilder()
+ /**
+ * Creates a POM activeByDefault profile with a POM InputLocation.
+ */
+ private Profile createPomActiveByDefaultProfile(String id) {
+ return Profile.newBuilder()
.id(id)
.activation(Activation.newBuilder().activeByDefault(true).build())
+ .location("", newPomLocation())
+ .build();
+ }
+
+ // ---- Helpers for isFromPom() / #2569 tests ----
+
+ /**
+ * Creates a POM profile with an InputSource that has a non-empty modelId,
+ * simulating a profile read from a POM file.
+ */
+ private Profile newPomProfile(String id, boolean activeByDefault) {
+ return Profile.newBuilder()
+ .id(id)
+ .activation(
+
Activation.newBuilder().activeByDefault(activeByDefault).build())
+ .location("", newPomLocation())
+ .build();
+ }
+
+ /**
+ * Creates a settings profile with an InputSource that has an empty
modelId,
+ * simulating a profile read from settings.xml (via
SettingsUtilsV4.toLocation).
+ */
+ private Profile newSettingsProfile(String id, boolean activeByDefault) {
+ return Profile.newBuilder()
+ .id(id)
+ .activation(
+
Activation.newBuilder().activeByDefault(activeByDefault).build())
+ .location("", newSettingsLocation())
.build();
- profile.setSource(source);
- return profile;
+ }
+
+ /**
+ * Creates a settings profile with a null modelId in its InputSource,
+ * simulating a profile read from settings.xml before conversion.
+ */
+ private Profile newSettingsProfileNullModelId(String id, boolean
activeByDefault) {
+ InputSource source = InputSource.of(null, "~/.m2/settings.xml");
+ InputLocation location = InputLocation.of(1, 1, source);
+ return Profile.newBuilder()
+ .id(id)
+ .activation(
+
Activation.newBuilder().activeByDefault(activeByDefault).build())
+ .location("", location)
+ .build();
+ }
+
+ /**
+ * Creates a profile with no InputLocation at all.
+ */
+ private Profile newProfileNoLocation(String id, boolean activeByDefault) {
+ return Profile.newBuilder()
+ .id(id)
+ .activation(
+
Activation.newBuilder().activeByDefault(activeByDefault).build())
+ .build();
+ }
+
+ private DefaultProfileActivationContext newContext() {
+ return new DefaultProfileActivationContext(new DefaultRootLocator(),
new DefaultInterpolator());
+ }
+
+ private DefaultProfileActivationContext newContext(List<String>
activeProfileIds) {
+ return new DefaultProfileActivationContext(
+ new DefaultRootLocator(),
+ new DefaultInterpolator(),
+ activeProfileIds,
+ Collections.emptyList(),
+ Collections.emptyMap(),
+ Collections.emptyMap(),
+ null);
}
}