Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -1407,6 +1408,12 @@ Model doReadFileModel() throws ModelBuilderException {
model.getParent().getVersion()))
: null)
.build();
// Override model properties with user properties
Map<String, String> newProps = merge(model.getProperties(), session.getUserProperties());
if (newProps != null) {
model = model.withProperties(newProps);
}
model = model.withProfiles(merge(model.getProfiles(), session.getUserProperties()));
}

for (var transformer : transformers) {
Expand All @@ -1425,6 +1432,57 @@ Model doReadFileModel() throws ModelBuilderException {
return model;
}

/**
* Merges a list of model profiles with user-defined properties.
* For each property defined in both the model and user properties, the user property value
* takes precedence and overrides the model value.
*
* @param profiles list of profiles from the model
* @param userProperties map of user-defined properties that override model properties
* @return a new list containing profiles with overridden properties if changes were made,
* or the original list if no overrides were needed
*/
List<Profile> merge(List<Profile> profiles, Map<String, String> userProperties) {
List<Profile> result = null;
for (int i = 0; i < profiles.size(); i++) {
Profile profile = profiles.get(i);
Map<String, String> props = merge(profile.getProperties(), userProperties);
if (props != null) {
Profile merged = profile.withProperties(props);
if (result == null) {
result = new ArrayList<>(profiles);
}
result.set(i, merged);
}
}
return result != null ? result : profiles;
}

/**
* 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
* the model property value when they differ.
*
* @param properties properties defined in the model
* @param userProperties user-defined properties that override model properties
* @return a new map with model properties overridden by user properties if changes were needed,
* or null if no overrides were needed
*/
Map<String, String> merge(Map<String, String> properties, Map<String, String> userProperties) {
Map<String, String> result = null;
for (Map.Entry<String, String> entry : properties.entrySet()) {
String key = entry.getKey();
String value = userProperties.get(key);
if (value != null && !Objects.equals(value, entry.getValue())) {
if (result == null) {
result = new LinkedHashMap<>(properties);
}
result.put(entry.getKey(), value);
}
}
return result;
}

Model readRawModel() throws ModelBuilderException {
// ensure file model is available
readFileModel();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Stream;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* This is a test set for <a href="https://issues.apache.org/jira/browse/MNG-8477">MNG-8477</a>.
*/
class MavenITmng8523ModelPropertiesTest extends AbstractMavenIntegrationTestCase {

MavenITmng8523ModelPropertiesTest() {
super("[4.0.0-rc-3-SNAPSHOT,)");
}

/**
* Verify project is buildable.
*/
@Test
void testIt() throws Exception {
Path basedir =
extractResources("/mng-8523-model-properties").getAbsoluteFile().toPath();

Verifier verifier = newVerifier(basedir.toString());
verifier.addCliArguments("install", "-DmavenVersion=4.0.0-rc-2");
verifier.execute();
verifier.verifyErrorFreeLog();

Path consumerPomPath =
Paths.get(verifier.getArtifactPath("org.apache.maven.its.mng-8523", "jar", "1.0.0-SNAPSHOT", "pom"));
assertTrue(Files.exists(consumerPomPath), "consumer pom not found at " + consumerPomPath);

List<String> consumerPomLines;
try (Stream<String> lines = Files.lines(consumerPomPath)) {
consumerPomLines = lines.toList();
}
assertTrue(
consumerPomLines.stream().noneMatch(s -> s.contains("${mavenVersion}")),
"Consumer pom should not have any <parent> element");
assertTrue(
consumerPomLines.stream().anyMatch(s -> s.contains("4.0.0-rc-2")),
"Consumer pom should not have any <parent> element");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,12 @@ public TestSuiteOrdering() {
* the tests are to finishing. Newer tests are also more likely to fail, so this is
* a fail fast technique as well.
*/
suite.addTestSuite(MavenITmng8523ModelPropertiesTest.class);
suite.addTestSuite(MavenITmng8527ConsumerPomTest.class);
suite.addTestSuite(MavenITmng8525MavenDIPlugin.class);
suite.addTestSuite(MavenITmng8477MultithreadedFileActivationTest.class);
suite.addTestSuite(MavenITmng8469InterpolationPrecendenceTest.class);
suite.addTestSuite(MavenITmng8465RepositoryWithProjectDirTest.class);
suite.addTestSuite(MavenITmng8461SpySettingsEventTest.class);
suite.addTestSuite(MavenITmng8414ConsumerPomWithNewFeaturesTest.class);
suite.addTestSuite(MavenITmng8245BeforePhaseCliTest.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.maven.its.mng-8523</groupId>
<artifactId>jar</artifactId>
<version>1.0.0-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-api-core</artifactId>
<version>${mavenVersion}</version>
</dependency>
</dependencies>

</project>