This is an automated email from the ASF dual-hosted git repository.
cstamas pushed a commit to branch maven-3.9.x
in repository https://gitbox.apache.org/repos/asf/maven.git
The following commit(s) were added to refs/heads/maven-3.9.x by this push:
new 1a787b0357 [MNG-8188] Profile properties are not interpolated (#1634)
1a787b0357 is described below
commit 1a787b035738ea83eb7f6eeca4cc0090314e0e3a
Author: Tamas Cservenak <[email protected]>
AuthorDate: Mon Aug 12 14:44:59 2024 +0200
[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
---
.../maven/model/building/DefaultModelBuilder.java | 10 ++-
.../profile/DefaultProfileInterpolationTest.java | 90 ++++++++++++++++++++++
.../src/test/resources/poms/profile/mng8188.xml | 62 +++++++++++++++
3 files changed, 159 insertions(+), 3 deletions(-)
diff --git
a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java
b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java
index 931e8c5bd3..9d3a23f5a6 100644
---
a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java
+++
b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java
@@ -36,6 +36,7 @@ import java.util.Objects;
import java.util.Optional;
import java.util.Properties;
import java.util.function.Consumer;
+import java.util.stream.IntStream;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import
org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
@@ -764,7 +765,7 @@ public class DefaultModelBuilder implements ModelBuilder {
private Model interpolateModel(Model model, ModelBuildingRequest request,
ModelProblemCollector problems) {
// save profile activations before interpolation, since they are
evaluated with limited scope
- List<Profile> originalActivations = getProfiles(model, true);
+ List<Profile> originalProfiles = getProfiles(model, true);
Model interpolatedModel =
modelInterpolator.interpolateModel(model,
model.getProjectDirectory(), request, problems);
@@ -791,8 +792,11 @@ public class DefaultModelBuilder implements ModelBuilder {
}
interpolatedModel.setPomFile(model.getPomFile());
- // restore profiles with file activation to their value before full
interpolation
- model.setProfiles(originalActivations);
+ // restore profiles with any activation to their value before full
interpolation
+ List<Profile> interpolatedProfiles = model.getProfiles();
+ IntStream.range(0, interpolatedProfiles.size()).forEach(i ->
interpolatedProfiles
+ .get(i)
+ .setActivation(originalProfiles.get(i).getActivation()));
return interpolatedModel;
}
diff --git
a/maven-model-builder/src/test/java/org/apache/maven/model/profile/DefaultProfileInterpolationTest.java
b/maven-model-builder/src/test/java/org/apache/maven/model/profile/DefaultProfileInterpolationTest.java
new file mode 100644
index 0000000000..0d227ab8e2
--- /dev/null
+++
b/maven-model-builder/src/test/java/org/apache/maven/model/profile/DefaultProfileInterpolationTest.java
@@ -0,0 +1,90 @@
+/*
+ * 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.model.profile;
+
+import java.io.File;
+
+import org.apache.maven.model.Model;
+import org.apache.maven.model.Plugin;
+import org.apache.maven.model.building.DefaultModelBuilderFactory;
+import org.apache.maven.model.building.DefaultModelBuildingRequest;
+import org.apache.maven.model.building.FileModelSource;
+import org.apache.maven.model.building.ModelBuilder;
+import org.apache.maven.model.building.ModelBuildingRequest;
+import org.apache.maven.model.building.ModelBuildingResult;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+/**
+ * Tests model builder profile interpolation.
+ */
+public class DefaultProfileInterpolationTest {
+ private File getPom(String name) {
+ return new File("src/test/resources/poms/profile/" + name);
+ }
+
+ /**
+ * MNG-8188: profile interpolation was "undone" by mistake. This UT
executes reproducer and ensures that
+ * profile interpolated values (sans activation) are fully interpolated.
+ */
+ @Test
+ public void profilePropertiesInterpolation() throws Exception {
+ ModelBuilder builder = new DefaultModelBuilderFactory().newInstance();
+ assertNotNull(builder);
+
+ DefaultModelBuildingRequest request = new
DefaultModelBuildingRequest();
+ request.setModelSource(new FileModelSource(getPom("mng8188.xml")));
+
request.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MAVEN_3_1);
+
+ ModelBuildingResult result = builder.build(request);
+ assertNotNull(result);
+ Model effectiveModel = result.getEffectiveModel();
+ assertNotNull(effectiveModel);
+
+ Plugin interpolatedPlugin = null;
+
+ // build/pluginManagement
+ for (Plugin plugin :
effectiveModel.getBuild().getPluginManagement().getPlugins()) {
+ if ("spring-boot-maven-plugin".equals(plugin.getArtifactId())) {
+ interpolatedPlugin = plugin;
+ break;
+ }
+ }
+ assertNotNull(interpolatedPlugin);
+ assertEquals("3.3.1", interpolatedPlugin.getVersion());
+
+ // profiles/foo/build/pluginManagement
+ interpolatedPlugin = null;
+ for (Plugin plugin : effectiveModel
+ .getProfiles()
+ .get(0)
+ .getBuild()
+ .getPluginManagement()
+ .getPlugins()) {
+ if ("spring-boot-maven-plugin".equals(plugin.getArtifactId())) {
+ interpolatedPlugin = plugin;
+ break;
+ }
+ }
+ assertNotNull(interpolatedPlugin);
+ assertEquals("3.3.1", interpolatedPlugin.getVersion());
+ }
+}
diff --git a/maven-model-builder/src/test/resources/poms/profile/mng8188.xml
b/maven-model-builder/src/test/resources/poms/profile/mng8188.xml
new file mode 100644
index 0000000000..ec85d44a48
--- /dev/null
+++ b/maven-model-builder/src/test/resources/poms/profile/mng8188.xml
@@ -0,0 +1,62 @@
+<?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>profile</groupId>
+ <artifactId>mng8188</artifactId>
+ <version>1.0</version>
+
+ <properties>
+ <version.spring-boot>3.3.1</version.spring-boot>
+ </properties>
+
+ <build>
+ <pluginManagement>
+ <plugins>
+ <plugin>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-maven-plugin</artifactId>
+ <version>${version.spring-boot}</version>
+ </plugin>
+ </plugins>
+ </pluginManagement>
+ </build>
+
+ <profiles>
+ <profile>
+ <id>foo</id>
+ <build>
+ <pluginManagement>
+ <plugins>
+ <plugin>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-maven-plugin</artifactId>
+ <version>${version.spring-boot}</version>
+ </plugin>
+ </plugins>
+ </pluginManagement>
+ </build>
+ </profile>
+ </profiles>
+</project>
\ No newline at end of file