Skip to content

Commit 852c95f

Browse files
gnodetclaude
andcommitted
[#2569] Remove Profile getSource/setSource remnants from Maven 3
Replace the mutable getSource()/setSource() mechanism and SOURCE_POM/SOURCE_SETTINGS constants on Profile with checks on InputLocation/InputSource. POM-originating profiles are now identified by having a non-null InputSource.getModelId(), while settings/external profiles have a null model id. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent a28f4e3 commit 852c95f

7 files changed

Lines changed: 73 additions & 54 deletions

File tree

api/maven-api-model/src/main/mdo/maven.mdo

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2988,55 +2988,14 @@
29882988
</fields>
29892989
<codeSegments>
29902990
<codeSegment>
2991-
<version>4.0.0/4.0.99</version>
2992-
<code>
2993-
<![CDATA[
2994-
public static final String SOURCE_POM = "pom";
2995-
2996-
public static final String SOURCE_SETTINGS = "settings.xml";
2997-
2998-
public void setSource(String source) {
2999-
getDelegate().setSource(source);
3000-
}
3001-
3002-
public String getSource() {
3003-
return getDelegate().getSource();
3004-
}
3005-
3006-
/**
3007-
* @see java.lang.Object#toString()
3008-
*/
3009-
public String toString() {
3010-
return "Profile {id: " + getId() + ", source: " + getSource() + "}";
3011-
}
3012-
]]>
3013-
</code>
3014-
</codeSegment>
3015-
<codeSegment>
3016-
<version>4.1.0+</version>
2991+
<version>4.0.0+</version>
30172992
<code>
30182993
<![CDATA[
3019-
public static final String SOURCE_POM = "pom";
3020-
3021-
public static final String SOURCE_SETTINGS = "settings.xml";
3022-
3023-
// We don't want this to be parseable...it's sort of 'hidden'
3024-
// default source for this profile is in the pom itself.
3025-
private String source = SOURCE_POM;
3026-
3027-
public void setSource(String source) {
3028-
this.source = source;
3029-
}
3030-
3031-
public String getSource() {
3032-
return source;
3033-
}
3034-
30352994
/**
30362995
* @see java.lang.Object#toString()
30372996
*/
30382997
public String toString() {
3039-
return "Profile {id: " + getId() + ", source: " + getSource() + "}";
2998+
return "Profile {id: " + getId() + "}";
30402999
}
30413000
]]>
30423001
</code>

compat/maven-compat/src/main/java/org/apache/maven/profiles/DefaultProfileManager.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import java.util.Properties;
2828

2929
import org.apache.maven.model.Activation;
30+
import org.apache.maven.model.InputLocation;
31+
import org.apache.maven.model.InputSource;
3032
import org.apache.maven.model.Profile;
3133
import org.apache.maven.model.building.ModelProblem;
3234
import org.apache.maven.model.profile.DefaultProfileActivationContext;
@@ -102,8 +104,8 @@ public void addProfile(Profile profile) {
102104

103105
Profile existing = profilesById.get(profileId);
104106
if (existing != null) {
105-
logger.warn("Overriding profile: '" + profileId + "' (source: " + existing.getSource()
106-
+ ") with new instance from source: " + profile.getSource());
107+
logger.warn("Overriding profile: '" + profileId + "' (source: " + getProfileSource(existing)
108+
+ ") with new instance from source: " + getProfileSource(profile));
107109
}
108110

109111
profilesById.put(profile.getId(), profile);
@@ -215,4 +217,22 @@ public List<String> getExplicitlyDeactivatedIds() {
215217
public List getIdsActivatedByDefault() {
216218
return defaultIds;
217219
}
220+
221+
private static String getProfileSource(Profile profile) {
222+
InputLocation location = profile.getLocation("");
223+
if (location != null) {
224+
InputSource source = location.getSource();
225+
if (source != null) {
226+
String loc = source.getLocation();
227+
if (loc != null) {
228+
return loc;
229+
}
230+
String modelId = source.getModelId();
231+
if (modelId != null) {
232+
return modelId;
233+
}
234+
}
235+
}
236+
return "unknown";
237+
}
218238
}

compat/maven-compat/src/main/java/org/apache/maven/profiles/ProfilesConversionUtils.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ public static Profile convertFromProfileXmlProfile(org.apache.maven.profiles.Pro
3838

3939
profile.setId(profileXmlProfile.getId());
4040

41-
profile.setSource("profiles.xml");
42-
4341
org.apache.maven.profiles.Activation profileActivation = profileXmlProfile.getActivation();
4442

4543
if (profileActivation != null) {

compat/maven-model-builder/src/main/java/org/apache/maven/model/profile/DefaultProfileSelector.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import java.util.List;
2929

3030
import org.apache.maven.model.Activation;
31+
import org.apache.maven.model.InputLocation;
32+
import org.apache.maven.model.InputSource;
3133
import org.apache.maven.model.Profile;
3234
import org.apache.maven.model.building.ModelProblem.Severity;
3335
import org.apache.maven.model.building.ModelProblem.Version;
@@ -70,11 +72,11 @@ public List<Profile> getActiveProfiles(
7072
if (activatedIds.contains(profile.getId()) || isActive(profile, context, problems)) {
7173
activeProfiles.add(profile);
7274

73-
if (Profile.SOURCE_POM.equals(profile.getSource())) {
75+
if (isFromPom(profile)) {
7476
activatedPomProfileNotByDefault = true;
7577
}
7678
} else if (isActiveByDefault(profile)) {
77-
if (Profile.SOURCE_POM.equals(profile.getSource())) {
79+
if (isFromPom(profile)) {
7880
activePomProfilesByDefault.add(profile);
7981
} else {
8082
activeProfiles.add(profile);
@@ -118,4 +120,22 @@ private boolean isActiveByDefault(Profile profile) {
118120
Activation activation = profile.getActivation();
119121
return activation != null && activation.isActiveByDefault();
120122
}
123+
124+
/**
125+
* Determines whether the given profile originates from a POM file
126+
* by checking if the profile's {@link InputSource} has a non-null model id.
127+
* Profiles from settings.xml or other external sources will not have a model id.
128+
* Defaults to {@code true} if no location information is available,
129+
* matching the previous default behavior.
130+
*/
131+
private boolean isFromPom(Profile profile) {
132+
InputLocation location = profile.getLocation("");
133+
if (location != null) {
134+
InputSource source = location.getSource();
135+
if (source != null) {
136+
return source.getModelId() != null;
137+
}
138+
}
139+
return true;
140+
}
121141
}

compat/maven-model/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ under the License.
139139
<exclude>org.apache.maven.model.io.xpp3.MavenXpp3WriterEx#writeXpp3DomToSerializer(org.codehaus.plexus.util.xml.Xpp3Dom,org.codehaus.plexus.util.xml.pull.XmlSerializer):METHOD_REMOVED</exclude>
140140
<exclude>org.apache.maven.model.io.xpp3.MavenXpp3WriterEx#stringFormatter</exclude>
141141
<exclude>org.apache.maven.model.merge.ModelMerger</exclude>
142+
<exclude>org.apache.maven.model.Profile#getSource():METHOD_REMOVED</exclude>
143+
<exclude>org.apache.maven.model.Profile#setSource(java.lang.String):METHOD_REMOVED</exclude>
144+
<exclude>org.apache.maven.model.Profile#SOURCE_POM</exclude>
145+
<exclude>org.apache.maven.model.Profile#SOURCE_SETTINGS</exclude>
142146
</excludes>
143147
</parameter>
144148
</configuration>

impl/maven-impl/src/main/java/org/apache/maven/impl/SettingsUtilsV4.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,7 @@ public static org.apache.maven.api.model.Profile convertFromSettingsProfile(Prof
267267
.collect(Collectors.toList()));
268268
}
269269

270-
org.apache.maven.api.model.Profile value = profile.build();
271-
value.setSource("settings.xml");
272-
return value;
270+
return profile.build();
273271
}
274272

275273
/**

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import org.apache.maven.api.di.Named;
2727
import org.apache.maven.api.di.Singleton;
2828
import org.apache.maven.api.model.Activation;
29+
import org.apache.maven.api.model.InputLocation;
30+
import org.apache.maven.api.model.InputSource;
2931
import org.apache.maven.api.model.Profile;
3032
import org.apache.maven.api.services.BuilderProblem.Severity;
3133
import org.apache.maven.api.services.ModelProblem.Version;
@@ -71,11 +73,11 @@ public List<Profile> getActiveProfiles(
7173
if (!context.isProfileInactive(profile.getId())) {
7274
if (context.isProfileActive(profile.getId()) || isActive(profile, context, problems)) {
7375
activeProfiles.add(profile);
74-
if (Profile.SOURCE_POM.equals(profile.getSource())) {
76+
if (isFromPom(profile)) {
7577
activatedPomProfileNotByDefault = true;
7678
}
7779
} else if (isActiveByDefault(profile)) {
78-
if (Profile.SOURCE_POM.equals(profile.getSource())) {
80+
if (isFromPom(profile)) {
7981
activePomProfilesByDefault.add(profile);
8082
} else {
8183
activeProfiles.add(profile);
@@ -118,4 +120,22 @@ private boolean isActiveByDefault(Profile profile) {
118120
Activation activation = profile.getActivation();
119121
return activation != null && activation.isActiveByDefault();
120122
}
123+
124+
/**
125+
* Determines whether the given profile originates from a POM file
126+
* by checking if the profile's {@link InputSource} has a non-null model id.
127+
* Profiles from settings.xml or other external sources will not have a model id.
128+
* Defaults to {@code true} if no location information is available,
129+
* matching the previous default behavior.
130+
*/
131+
private boolean isFromPom(Profile profile) {
132+
InputLocation location = profile.getLocation("");
133+
if (location != null) {
134+
InputSource source = location.getSource();
135+
if (source != null) {
136+
return source.getModelId() != null;
137+
}
138+
}
139+
return true;
140+
}
121141
}

0 commit comments

Comments
 (0)