This is an automated email from the ASF dual-hosted git repository.
gnodet pushed a commit to branch maven-4.0.x
in repository https://gitbox.apache.org/repos/asf/maven.git
The following commit(s) were added to refs/heads/maven-4.0.x by this push:
new a7b99f91e1 [Backport maven-4.0.x] Fix #13761: keep active-profile
properties out of the consumer POM (#13921)
a7b99f91e1 is described below
commit a7b99f91e12caae64e632f41d3348d9466219274
Author: Guillaume Nodet <[email protected]>
AuthorDate: Mon Nov 9 20:41:45 2026 +0100
[Backport maven-4.0.x] Fix #13761: keep active-profile properties out of
the consumer POM (#13921)
* Fix #13761: keep active-profile properties out of the consumer POM
(#13768)
* Fix #13761: keep active-profile properties out of the consumer POM
Since MNG-8709 the consumer POM build receives the properties of every
profile that was active when the project was built as request user
properties, so that coordinates those profiles supply interpolate and
validate. The model builder then applied the same map as the user-property
override, writing those values over the main <properties> block and over
the same keys in every profile, active or not. A consumer saw the
producer's environment: a file-activated profile's values in the main
block, an opt-in profile rewritten to the opposite of its purpose, and an
OS profile for another platform carrying the producer's classifier.
For a BUILD_CONSUMER request the override now uses the session's user
properties only, which restores the rc-5 behaviour: declared values stay
as declared, and a -D flag still overrides them. Every other build request
keeps overriding with the request's user properties.
Unit tests cover the consumer model directly; a core IT installs a project
and reads back the consumer POM.
* Drop the dead null guard in overridingUserProperties()
BaseRequest rejects a null session in its constructor, so the guard could
never fire, and its fallback would have reinstated the defect by returning
the request properties that carry the active-profile values.
* Fix IT for maven-4.0.x API: use File/String instead of Path
---------
Co-authored-by: Keith E Campbell <[email protected]>
---
.../impl/DefaultConsumerPomBuilder.java | 4 +-
.../impl/ConsumerPomBuilderTest.java | 65 +++++++++++++++
.../resources/consumer/profile-properties/pom.xml | 57 +++++++++++++
.../maven/impl/model/DefaultModelBuilder.java | 27 +++++-
...nITgh13761ConsumerPomProfilePropertiesTest.java | 95 ++++++++++++++++++++++
.../pom.xml | 76 +++++++++++++++++
6 files changed, 321 insertions(+), 3 deletions(-)
diff --git
a/impl/maven-core/src/main/java/org/apache/maven/internal/transformation/impl/DefaultConsumerPomBuilder.java
b/impl/maven-core/src/main/java/org/apache/maven/internal/transformation/impl/DefaultConsumerPomBuilder.java
index 72a1208ad8..338e8601f2 100644
---
a/impl/maven-core/src/main/java/org/apache/maven/internal/transformation/impl/DefaultConsumerPomBuilder.java
+++
b/impl/maven-core/src/main/java/org/apache/maven/internal/transformation/impl/DefaultConsumerPomBuilder.java
@@ -592,7 +592,9 @@ private ModelBuilderResult
buildModel(RepositorySystemSession session, MavenProj
request.systemProperties(iSession.getSystemProperties());
Map<String, String> userProperties = new LinkedHashMap<>();
// BUILD_CONSUMER does not reactivate project profiles, so expose
properties from profiles
- // that were already active when the project model was built.
+ // that were already active when the project model was built. They
serve interpolation and
+ // coordinate validation only: the model builder does not write them
into the consumer POM
+ // (see DefaultModelBuilder#overridingUserProperties, GH-13761).
if (project != null && project.getActiveProfiles() != null) {
for (org.apache.maven.model.Profile profile :
project.getActiveProfiles()) {
userProperties.putAll(profile.getDelegate().getProperties());
diff --git
a/impl/maven-core/src/test/java/org/apache/maven/internal/transformation/impl/ConsumerPomBuilderTest.java
b/impl/maven-core/src/test/java/org/apache/maven/internal/transformation/impl/ConsumerPomBuilderTest.java
index 5baae0a862..c40a8da48b 100644
---
a/impl/maven-core/src/test/java/org/apache/maven/internal/transformation/impl/ConsumerPomBuilderTest.java
+++
b/impl/maven-core/src/test/java/org/apache/maven/internal/transformation/impl/ConsumerPomBuilderTest.java
@@ -1497,4 +1497,69 @@ void
testHasNonModelPropertiesReturnsFalseForEnvAndSettingsProperties() {
DefaultConsumerPomBuilder.hasNonModelProperties("${env.HOME}-${ext.qualifier}",
Map.of()),
"Should return true when at least one property is not built-in
or in the model");
}
+
+ /**
+ * The properties of a profile that was active when the project was built
reach the consumer
+ * model build as request user properties, so that the coordinates they
supply can be validated
+ * (MNG-8709). They must not be written into the consumer POM: a consumer
re-evaluates the
+ * profile activation itself, so the main block and every profile keep
their declared values
+ * (GH-13761).
+ */
+ @Test
+ void testActiveProfilePropertiesAreNotPersistedInConsumerPom() throws
Exception {
+ setRootDirectory("profile-properties");
+ Path file =
Paths.get("src/test/resources/consumer/profile-properties/pom.xml");
+
+ MavenProject project = getEffectiveModel(file);
+ activate(project, "active-here");
+
+ Model model = builder.build(session, project,
Sources.buildSource(file));
+
+ assertEquals(Map.of("flag", "false", "classifier", "none"),
model.getProperties());
+ assertEquals(
+ Map.of("flag", "true", "classifier", "here"),
+ profile(model, "active-here").getProperties());
+ assertEquals(Map.of("flag", "false"), profile(model,
"opt-in").getProperties());
+ assertEquals(
+ Map.of("classifier", "elsewhere"), profile(model,
"elsewhere").getProperties());
+ }
+
+ /**
+ * A property the user passed on the command line still overrides the
declared value in the
+ * consumer POM, as it did before GH-13761, and still takes precedence
over the value of an
+ * active profile.
+ */
+ @Test
+ void testUserPropertyStillOverridesDeclaredValueInConsumerPom() throws
Exception {
+ MavenExecutionRequest request = setRootDirectory("profile-properties");
+ request.getUserProperties().setProperty("classifier", "cli");
+ Path file =
Paths.get("src/test/resources/consumer/profile-properties/pom.xml");
+
+ MavenProject project = getEffectiveModel(file);
+ activate(project, "active-here");
+
+ Model model = builder.build(session, project,
Sources.buildSource(file));
+
+ assertEquals(Map.of("flag", "false", "classifier", "cli"),
model.getProperties());
+ assertEquals(Map.of("flag", "false"), profile(model,
"opt-in").getProperties());
+ }
+
+ /**
+ * Records the given profile as active on the project, as the project
builder does for a
+ * profile whose activation held when the project was built.
+ */
+ private static void activate(MavenProject project, String profileId) {
+ List<org.apache.maven.model.Profile> active =
project.getModel().getProfiles().stream()
+ .filter(profile -> profileId.equals(profile.getId()))
+ .toList();
+ assertEquals(1, active.size(), "profile " + profileId + " should be
declared once");
+ project.setActiveProfiles(active);
+ }
+
+ private static Profile profile(Model model, String id) {
+ return model.getProfiles().stream()
+ .filter(profile -> id.equals(profile.getId()))
+ .findFirst()
+ .orElseThrow(() -> new AssertionError("profile " + id + " is
missing from the consumer POM"));
+ }
}
diff --git
a/impl/maven-core/src/test/resources/consumer/profile-properties/pom.xml
b/impl/maven-core/src/test/resources/consumer/profile-properties/pom.xml
new file mode 100644
index 0000000000..01c97c6e6b
--- /dev/null
+++ b/impl/maven-core/src/test/resources/consumer/profile-properties/pom.xml
@@ -0,0 +1,57 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>org.apache.maven.its.gh13761</groupId>
+ <artifactId>profile-properties</artifactId>
+ <version>1.0</version>
+ <packaging>pom</packaging>
+
+ <!-- The declared values. A consumer must see exactly these, in the main
block and in each
+ profile, whatever was active on the machine that built the project
(GH-13761). -->
+ <properties>
+ <flag>false</flag>
+ <classifier>none</classifier>
+ </properties>
+
+ <profiles>
+ <!-- Active wherever the project is built: no-such-dir never exists. -->
+ <profile>
+ <id>active-here</id>
+ <activation>
+ <file>
+ <missing>no-such-dir</missing>
+ </file>
+ </activation>
+ <properties>
+ <flag>true</flag>
+ <classifier>here</classifier>
+ </properties>
+ </profile>
+ <!-- An opt-in switch a consumer may activate to set the flag the other
way. -->
+ <profile>
+ <id>opt-in</id>
+ <activation>
+ <property>
+ <name>want.flag.off</name>
+ </property>
+ </activation>
+ <properties>
+ <flag>false</flag>
+ </properties>
+ </profile>
+ <!-- A profile for a platform the producer is not on. -->
+ <profile>
+ <id>elsewhere</id>
+ <activation>
+ <os>
+ <family>no-such-os</family>
+ </os>
+ </activation>
+ <properties>
+ <classifier>elsewhere</classifier>
+ </properties>
+ </profile>
+ </profiles>
+</project>
diff --git
a/impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelBuilder.java
b/impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelBuilder.java
index 34279e2dff..e59160807f 100644
---
a/impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelBuilder.java
+++
b/impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelBuilder.java
@@ -2004,8 +2004,9 @@ Model doReadFileModel(Set<Path> activeModelReads) throws
ModelBuilderException {
.distributionManagement(interpolateRepository(model.getDistributionManagement(),
callback))
.build();
// Override model properties with user properties (use
request properties
- // to ensure consistency with model interpolation)
- Map<String, String> userProps =
request.getUserProperties();
+ // to ensure consistency with model interpolation, except
for a consumer
+ // build, see overridingUserProperties())
+ Map<String, String> userProps = overridingUserProperties();
Map<String, String> newProps =
merge(model.getProperties(), userProps);
if (newProps != null) {
model = model.withProperties(newProps);
@@ -2129,6 +2130,28 @@ List<Profile> merge(List<Profile> profiles, Map<String,
String> userProperties)
return result != null ? result : profiles;
}
+ /**
+ * Returns the user properties that override the properties declared
in the model, in the
+ * main block and in every profile.
+ * <p>
+ * A {@link ModelBuilderRequest.RequestType#BUILD_CONSUMER} request
carries, besides the
+ * session's user properties, the properties of the profiles that were
active when the
+ * project was built (see {@code DefaultConsumerPomBuilder}). They let
the coordinates those
+ * profiles supply interpolate and validate (MNG-8709), but they
describe the producer's
+ * environment: the consumer re-evaluates the profile activation
itself, so only the
+ * properties the user actually passed may be written into the
consumer POM (GH-13761).
+ * Every other build request overrides with the request's user
properties, which the
+ * caller may have tailored on purpose.
+ *
+ * @return the user properties to apply over the declared model
properties
+ */
+ Map<String, String> overridingUserProperties() {
+ if (request.getRequestType() ==
ModelBuilderRequest.RequestType.BUILD_CONSUMER) {
+ return request.getSession().getUserProperties();
+ }
+ return request.getUserProperties();
+ }
+
/**
* Merges model properties with user properties, giving precedence to
user properties.
* For any property key present in both maps, the user property value
will override
diff --git
a/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITgh13761ConsumerPomProfilePropertiesTest.java
b/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITgh13761ConsumerPomProfilePropertiesTest.java
new file mode 100644
index 0000000000..2bd6deff86
--- /dev/null
+++
b/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITgh13761ConsumerPomProfilePropertiesTest.java
@@ -0,0 +1,95 @@
+/*
+ * 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.it;
+
+import java.io.File;
+import java.io.Reader;
+import java.nio.file.Files;
+import java.util.Map;
+
+import org.apache.maven.api.model.Model;
+import org.apache.maven.api.model.Profile;
+import org.apache.maven.model.v4.MavenStaxReader;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * This is a test set for <a
href="https://github.com/apache/maven/issues/13761">GH-13761</a>.
+ * The consumer POM carries the property values as declared, in the main block
and in each
+ * profile, whatever profiles were active on the machine that built the
project. The properties
+ * of those profiles only serve interpolation and coordinate validation
(MNG-8709).
+ */
+class MavenITgh13761ConsumerPomProfilePropertiesTest extends
AbstractMavenIntegrationTestCase {
+
+ private static final String GROUP_ID = "org.apache.maven.its.gh13761";
+
+ MavenITgh13761ConsumerPomProfilePropertiesTest() {
+ super("[4.0.0,)");
+ }
+
+ @Test
+ void testActiveProfilePropertiesAreNotPersistedInConsumerPom() throws
Exception {
+ File basedir =
extractResources("gh-13761-consumer-pom-profile-properties");
+
+ Verifier verifier = newVerifier(basedir.getAbsolutePath());
+ verifier.addCliArgument("install");
+ verifier.execute();
+ verifier.verifyErrorFreeLog();
+
+ Model consumerPom = read(new File(verifier.getArtifactPath(GROUP_ID,
"profile-properties", "1.0", "pom")));
+
+ assertEquals(Map.of("flag", "false", "classifier", "none"),
consumerPom.getProperties());
+ assertEquals(Map.of("flag", "true", "classifier", "here"),
profile(consumerPom, "active-here").getProperties());
+ assertEquals(Map.of("flag", "false"), profile(consumerPom,
"opt-in").getProperties());
+ assertEquals(Map.of("classifier", "elsewhere"), profile(consumerPom,
"elsewhere").getProperties());
+ }
+
+ /**
+ * A property passed on the command line still overrides the declared
value, as before.
+ */
+ @Test
+ void testUserPropertyStillOverridesDeclaredValueInConsumerPom() throws
Exception {
+ File basedir =
extractResources("gh-13761-consumer-pom-profile-properties");
+
+ Verifier verifier = newVerifier(basedir.getAbsolutePath());
+ verifier.addCliArgument("-Dclassifier=cli");
+ verifier.addCliArgument("install");
+ verifier.execute();
+ verifier.verifyErrorFreeLog();
+
+ Model consumerPom = read(new File(verifier.getArtifactPath(GROUP_ID,
"profile-properties", "1.0", "pom")));
+
+ assertEquals(Map.of("flag", "false", "classifier", "cli"),
consumerPom.getProperties());
+ assertEquals(Map.of("flag", "false"), profile(consumerPom,
"opt-in").getProperties());
+ }
+
+ private static Model read(File pomFile) throws Exception {
+ try (Reader reader = Files.newBufferedReader(pomFile.toPath())) {
+ return new MavenStaxReader().read(reader);
+ }
+ }
+
+ private static Profile profile(Model model, String id) {
+ return model.getProfiles().stream()
+ .filter(profile -> id.equals(profile.getId()))
+ .findFirst()
+ .orElseThrow(() -> new AssertionError("profile " + id + " is
missing from the consumer POM"));
+ }
+}
diff --git
a/its/core-it-suite/src/test/resources/gh-13761-consumer-pom-profile-properties/pom.xml
b/its/core-it-suite/src/test/resources/gh-13761-consumer-pom-profile-properties/pom.xml
new file mode 100644
index 0000000000..884f7df100
--- /dev/null
+++
b/its/core-it-suite/src/test/resources/gh-13761-consumer-pom-profile-properties/pom.xml
@@ -0,0 +1,76 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>org.apache.maven.its.gh13761</groupId>
+ <artifactId>profile-properties</artifactId>
+ <version>1.0</version>
+ <packaging>pom</packaging>
+
+ <!-- The declared values. A consumer must see exactly these, in the main
block and in each
+ profile, whatever was active on the machine that built the project
(GH-13761). -->
+ <properties>
+ <flag>false</flag>
+ <classifier>none</classifier>
+ </properties>
+
+ <profiles>
+ <!-- Active wherever the project is built: no-such-dir never exists. -->
+ <profile>
+ <id>active-here</id>
+ <activation>
+ <file>
+ <missing>no-such-dir</missing>
+ </file>
+ </activation>
+ <properties>
+ <flag>true</flag>
+ <classifier>here</classifier>
+ </properties>
+ </profile>
+ <!-- An opt-in switch a consumer may activate to set the flag the other
way. -->
+ <profile>
+ <id>opt-in</id>
+ <activation>
+ <property>
+ <name>want.flag.off</name>
+ </property>
+ </activation>
+ <properties>
+ <flag>false</flag>
+ </properties>
+ </profile>
+ <!-- A profile for a platform the producer is not on. -->
+ <profile>
+ <id>elsewhere</id>
+ <activation>
+ <os>
+ <family>no-such-os</family>
+ </os>
+ </activation>
+ <properties>
+ <classifier>elsewhere</classifier>
+ </properties>
+ </profile>
+ </profiles>
+</project>