Skip to content

Commit 1a787b0

Browse files
authored
[MNG-8188] Profile properties are not interpolated (#1634)
Restore of uninterpolated things did "too much", we need to restore activations only. Maven4 is not affected, manually checked using reproducer. --- https://issues.apache.org/jira/browse/MNG-8188
1 parent ecd577b commit 1a787b0

File tree

3 files changed

+159
-3
lines changed

3 files changed

+159
-3
lines changed

maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.util.Optional;
3737
import java.util.Properties;
3838
import java.util.function.Consumer;
39+
import java.util.stream.IntStream;
3940

4041
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
4142
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
@@ -764,7 +765,7 @@ private List<Profile> getProfiles(Model model, boolean clone) {
764765

765766
private Model interpolateModel(Model model, ModelBuildingRequest request, ModelProblemCollector problems) {
766767
// save profile activations before interpolation, since they are evaluated with limited scope
767-
List<Profile> originalActivations = getProfiles(model, true);
768+
List<Profile> originalProfiles = getProfiles(model, true);
768769

769770
Model interpolatedModel =
770771
modelInterpolator.interpolateModel(model, model.getProjectDirectory(), request, problems);
@@ -791,8 +792,11 @@ private Model interpolateModel(Model model, ModelBuildingRequest request, ModelP
791792
}
792793
interpolatedModel.setPomFile(model.getPomFile());
793794

794-
// restore profiles with file activation to their value before full interpolation
795-
model.setProfiles(originalActivations);
795+
// restore profiles with any activation to their value before full interpolation
796+
List<Profile> interpolatedProfiles = model.getProfiles();
797+
IntStream.range(0, interpolatedProfiles.size()).forEach(i -> interpolatedProfiles
798+
.get(i)
799+
.setActivation(originalProfiles.get(i).getActivation()));
796800

797801
return interpolatedModel;
798802
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.maven.model.profile;
20+
21+
import java.io.File;
22+
23+
import org.apache.maven.model.Model;
24+
import org.apache.maven.model.Plugin;
25+
import org.apache.maven.model.building.DefaultModelBuilderFactory;
26+
import org.apache.maven.model.building.DefaultModelBuildingRequest;
27+
import org.apache.maven.model.building.FileModelSource;
28+
import org.apache.maven.model.building.ModelBuilder;
29+
import org.apache.maven.model.building.ModelBuildingRequest;
30+
import org.apache.maven.model.building.ModelBuildingResult;
31+
import org.junit.Test;
32+
33+
import static org.junit.Assert.assertEquals;
34+
import static org.junit.Assert.assertNotNull;
35+
36+
/**
37+
* Tests model builder profile interpolation.
38+
*/
39+
public class DefaultProfileInterpolationTest {
40+
private File getPom(String name) {
41+
return new File("src/test/resources/poms/profile/" + name);
42+
}
43+
44+
/**
45+
* MNG-8188: profile interpolation was "undone" by mistake. This UT executes reproducer and ensures that
46+
* profile interpolated values (sans activation) are fully interpolated.
47+
*/
48+
@Test
49+
public void profilePropertiesInterpolation() throws Exception {
50+
ModelBuilder builder = new DefaultModelBuilderFactory().newInstance();
51+
assertNotNull(builder);
52+
53+
DefaultModelBuildingRequest request = new DefaultModelBuildingRequest();
54+
request.setModelSource(new FileModelSource(getPom("mng8188.xml")));
55+
request.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MAVEN_3_1);
56+
57+
ModelBuildingResult result = builder.build(request);
58+
assertNotNull(result);
59+
Model effectiveModel = result.getEffectiveModel();
60+
assertNotNull(effectiveModel);
61+
62+
Plugin interpolatedPlugin = null;
63+
64+
// build/pluginManagement
65+
for (Plugin plugin : effectiveModel.getBuild().getPluginManagement().getPlugins()) {
66+
if ("spring-boot-maven-plugin".equals(plugin.getArtifactId())) {
67+
interpolatedPlugin = plugin;
68+
break;
69+
}
70+
}
71+
assertNotNull(interpolatedPlugin);
72+
assertEquals("3.3.1", interpolatedPlugin.getVersion());
73+
74+
// profiles/foo/build/pluginManagement
75+
interpolatedPlugin = null;
76+
for (Plugin plugin : effectiveModel
77+
.getProfiles()
78+
.get(0)
79+
.getBuild()
80+
.getPluginManagement()
81+
.getPlugins()) {
82+
if ("spring-boot-maven-plugin".equals(plugin.getArtifactId())) {
83+
interpolatedPlugin = plugin;
84+
break;
85+
}
86+
}
87+
assertNotNull(interpolatedPlugin);
88+
assertEquals("3.3.1", interpolatedPlugin.getVersion());
89+
}
90+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!--
4+
Licensed to the Apache Software Foundation (ASF) under one
5+
or more contributor license agreements. See the NOTICE file
6+
distributed with this work for additional information
7+
regarding copyright ownership. The ASF licenses this file
8+
to you under the Apache License, Version 2.0 (the
9+
"License"); you may not use this file except in compliance
10+
with the License. You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0
13+
14+
Unless required by applicable law or agreed to in writing,
15+
software distributed under the License is distributed on an
16+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
KIND, either express or implied. See the License for the
18+
specific language governing permissions and limitations
19+
under the License.
20+
-->
21+
22+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
23+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
24+
<modelVersion>4.0.0</modelVersion>
25+
26+
<groupId>profile</groupId>
27+
<artifactId>mng8188</artifactId>
28+
<version>1.0</version>
29+
30+
<properties>
31+
<version.spring-boot>3.3.1</version.spring-boot>
32+
</properties>
33+
34+
<build>
35+
<pluginManagement>
36+
<plugins>
37+
<plugin>
38+
<groupId>org.springframework.boot</groupId>
39+
<artifactId>spring-boot-maven-plugin</artifactId>
40+
<version>${version.spring-boot}</version>
41+
</plugin>
42+
</plugins>
43+
</pluginManagement>
44+
</build>
45+
46+
<profiles>
47+
<profile>
48+
<id>foo</id>
49+
<build>
50+
<pluginManagement>
51+
<plugins>
52+
<plugin>
53+
<groupId>org.springframework.boot</groupId>
54+
<artifactId>spring-boot-maven-plugin</artifactId>
55+
<version>${version.spring-boot}</version>
56+
</plugin>
57+
</plugins>
58+
</pluginManagement>
59+
</build>
60+
</profile>
61+
</profiles>
62+
</project>

0 commit comments

Comments
 (0)