gnodet commented on code in PR #13406:
URL: https://github.com/apache/maven/pull/13406#discussion_r4241162531
##########
impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultProfileInjector.java:
##########
@@ -103,6 +104,50 @@ public void mergeBuildBase(BuildBase.Builder builder,
BuildBase target, BuildBas
mergeBuildBase(builder, target, source, true,
Collections.emptyMap());
}
+ @Override
+ protected void mergeModelBase_Properties(
+ ModelBase.Builder builder,
+ ModelBase target,
+ ModelBase source,
+ boolean sourceDominant,
+ Map<Object, Object> context) {
+ Map<String, String> merged = new LinkedHashMap<>();
+ if (sourceDominant) {
+ merged.putAll(target.getProperties());
+ merged.putAll(source.getProperties());
+ } else {
+ merged.putAll(source.getProperties());
+ merged.putAll(target.getProperties());
+ }
+
+ if (source instanceof Profile profile) {
+ String profileId = profile.getId();
+ if (profileId != null && !profileId.isEmpty()) {
+ String prefixSingular = "profile." + profileId + ".";
+ String prefixPlural = "profiles." + profileId + ".";
Review Comment:
⚠️ **Design question: dual prefix (`profile.` + `profiles.`)** — This
doubles the number of synthetic keys injected into the model for every profile
property. If a profile has 10 properties, you get 20 synthetic entries. The
JIRA ([MNG-6885](https://issues.apache.org/jira/browse/MNG-6885)) describes
`profile.<id>.<key>` only. Has `profiles.<id>.<key>` been discussed on the dev
list? If both are intentional, add a comment explaining the rationale —
otherwise consider dropping the plural form to halve the overhead.
##########
impl/maven-impl/src/test/java/org/apache/maven/impl/model/ProfileScopePropertiesTest.java:
##########
@@ -0,0 +1,137 @@
+/*
+ * 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;
+
+import java.nio.file.Paths;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Map;
+
+import org.apache.maven.api.Session;
+import org.apache.maven.api.model.InputLocation;
+import org.apache.maven.api.model.Model;
+import org.apache.maven.api.model.Profile;
+import org.apache.maven.api.services.Lookup;
+import org.apache.maven.api.services.ModelBuilderRequest;
+import org.apache.maven.api.services.model.ModelInterpolator;
+import org.apache.maven.api.services.model.ProfileInjector;
+import org.apache.maven.impl.model.profile.SimpleProblemCollector;
+import org.apache.maven.impl.standalone.ApiRunner;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+class ProfileScopePropertiesTest {
+
+ private Session session;
+ private ProfileInjector profileInjector;
+ private ModelInterpolator interpolator;
+
+ @BeforeEach
+ void setUp() {
+ session = ApiRunner.createSession();
+ profileInjector =
session.getService(Lookup.class).lookup(ProfileInjector.class);
+ interpolator =
session.getService(Lookup.class).lookup(ModelInterpolator.class);
+ }
+
+ @Test
+ void testProfileScopePropertiesInjection() {
+ InputLocation loc1 = new InputLocation(10, 5);
+ InputLocation loc2 = new InputLocation(20, 5);
+
+ Profile profile1 = Profile.newBuilder()
+ .id("scala-2.11")
+ .properties(Map.of("scala.version", "2.11.12",
"compiler.plugin", "scalamacros"))
+ .location("scala.version", loc1)
+ .build();
+
+ Profile profile2 = Profile.newBuilder()
+ .id("scala-2.12")
+ .properties(Map.of("scala.version", "2.12.18", "extra.arg",
"-Xfatal-warnings"))
+ .location("scala.version", loc2)
+ .build();
+
+ Model model = Model.newBuilder()
+ .properties(Map.of("common.prop", "common"))
+ .build();
+
+ SimpleProblemCollector problems = new SimpleProblemCollector();
+ Model result = profileInjector.injectProfiles(
+ model, Arrays.asList(profile1, profile2),
ModelBuilderRequest.builder().session(session).build(), problems);
+
+ // Later active profile overwrites unscoped property
+ assertEquals("2.12.18", result.getProperties().get("scala.version"));
+ // Profile-scoped properties are preserved without collision
+ assertEquals("2.11.12",
result.getProperties().get("profile.scala-2.11.scala.version"));
+ assertEquals("2.11.12",
result.getProperties().get("profiles.scala-2.11.scala.version"));
+ assertEquals("2.12.18",
result.getProperties().get("profile.scala-2.12.scala.version"));
+ assertEquals("2.12.18",
result.getProperties().get("profiles.scala-2.12.scala.version"));
+
+ assertEquals("scalamacros",
result.getProperties().get("profile.scala-2.11.compiler.plugin"));
+ assertEquals("-Xfatal-warnings",
result.getProperties().get("profile.scala-2.12.extra.arg"));
+
+ // Verify InputLocation tracking on profile-scoped properties
+ assertEquals(loc1,
result.getLocation("profile.scala-2.11.scala.version"));
+ assertEquals(loc1,
result.getLocation("profiles.scala-2.11.scala.version"));
+ assertEquals(loc2,
result.getLocation("profile.scala-2.12.scala.version"));
+ assertEquals(loc2,
result.getLocation("profiles.scala-2.12.scala.version"));
+ }
+
+ @Test
+ void testProfileScopePropertiesInterpolation() {
+ Profile activeProfile = Profile.newBuilder()
+ .id("dev")
+ .properties(Map.of("env.name", "development"))
+ .build();
+
+ Profile inactiveProfile = Profile.newBuilder()
+ .id("prod")
+ .properties(Map.of("env.name", "production"))
+ .build();
+
+ Model rawModel = Model.newBuilder()
+ .profiles(Arrays.asList(activeProfile, inactiveProfile))
+ .description("${profile.dev.env.name}")
+ .url("${profiles.dev.env.name}")
+ .build();
+
+ SimpleProblemCollector problems = new SimpleProblemCollector();
+ // Only active profiles are injected
+ Model injectedModel = profileInjector.injectProfiles(
+ rawModel,
+ Collections.singletonList(activeProfile),
+ ModelBuilderRequest.builder().session(session).build(),
+ problems);
+
+ Model out = interpolator.interpolateModel(
+ injectedModel,
+ Paths.get("."),
+
ModelBuilderRequest.builder().session(session).requestType(ModelBuilderRequest.RequestType.BUILD_PROJECT).build(),
+ problems);
+
+ // Active profile-scoped properties resolve cleanly
+ assertEquals("development", out.getDescription());
+ assertEquals("development", out.getUrl());
+ // Inactive profile properties are never injected into model properties
+ assertNull(injectedModel.getProperties().get("profile.prod.env.name"));
+
assertNull(injectedModel.getProperties().get("profiles.prod.env.name"));
+ }
+}
Review Comment:
⚠️ **Missing edge-case tests:**
1. Profile ID containing dots (e.g., `"my.profile"`) — documents the known
dot-collision risk
2. Property key starting with `profile.` — verifies it is correctly excluded
from prefixing (the guard at line 131 of the impl)
3. Profile with no ID (empty string) — verifies the null/empty guard at line
125
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]