Skip to content

Commit 7303b57

Browse files
committed
[MNG-8477] Fix problems with file activation being wrongly cached
1 parent 023b014 commit 7303b57

File tree

7 files changed

+105
-84
lines changed

7 files changed

+105
-84
lines changed

impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelBuilder.java

Lines changed: 2 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
import org.apache.maven.api.di.Named;
5959
import org.apache.maven.api.di.Singleton;
6060
import org.apache.maven.api.model.Activation;
61-
import org.apache.maven.api.model.ActivationFile;
6261
import org.apache.maven.api.model.Dependency;
6362
import org.apache.maven.api.model.DependencyManagement;
6463
import org.apache.maven.api.model.Exclusion;
@@ -71,7 +70,6 @@
7170
import org.apache.maven.api.services.BuilderProblem;
7271
import org.apache.maven.api.services.BuilderProblem.Severity;
7372
import org.apache.maven.api.services.Interpolator;
74-
import org.apache.maven.api.services.InterpolatorException;
7573
import org.apache.maven.api.services.MavenException;
7674
import org.apache.maven.api.services.ModelBuilder;
7775
import org.apache.maven.api.services.ModelBuilderException;
@@ -111,7 +109,6 @@
111109
import org.apache.maven.api.spi.ModelParserException;
112110
import org.apache.maven.api.spi.ModelTransformer;
113111
import org.apache.maven.impl.util.PhasingExecutor;
114-
import org.apache.maven.model.v4.MavenTransformer;
115112
import org.slf4j.Logger;
116113
import org.slf4j.LoggerFactory;
117114

@@ -1174,11 +1171,8 @@ private Model readEffectiveModel() throws ModelBuilderException {
11741171
// profile activation
11751172
profileActivationContext.setModel(model);
11761173

1177-
List<Profile> interpolatedProfiles =
1178-
interpolateActivations(model.getProfiles(), profileActivationContext, this);
1179-
11801174
// profile injection
1181-
List<Profile> activePomProfiles = getActiveProfiles(interpolatedProfiles, profileActivationContext);
1175+
List<Profile> activePomProfiles = getActiveProfiles(model.getProfiles(), profileActivationContext);
11821176
model = profileInjector.injectProfiles(model, activePomProfiles, request, this);
11831177
model = profileInjector.injectProfiles(model, activeExternalProfiles, request, this);
11841178

@@ -1505,11 +1499,8 @@ protected void mergeModel_Subprojects(
15051499
.assembleModelInheritance(raw, parentData, request, this);
15061500

15071501
// activate profiles
1508-
List<Profile> parentInterpolatedProfiles =
1509-
interpolateActivations(parent.getProfiles(), profileActivationContext, this);
1502+
List<Profile> parentActivePomProfiles = getActiveProfiles(parent.getProfiles(), profileActivationContext);
15101503
// profile injection
1511-
List<Profile> parentActivePomProfiles =
1512-
getActiveProfiles(parentInterpolatedProfiles, profileActivationContext);
15131504
Model injectedParentModel = profileInjector
15141505
.injectProfiles(parent, parentActivePomProfiles, request, this)
15151506
.withProfiles(List.of());
@@ -1741,79 +1732,6 @@ boolean isBuildRequestWithActivation() {
17411732
return request.getRequestType() != ModelBuilderRequest.RequestType.BUILD_CONSUMER;
17421733
}
17431734

1744-
private List<Profile> interpolateActivations(
1745-
List<Profile> profiles, DefaultProfileActivationContext context, ModelProblemCollector problems) {
1746-
if (profiles.stream()
1747-
.map(org.apache.maven.api.model.Profile::getActivation)
1748-
.noneMatch(Objects::nonNull)) {
1749-
return profiles;
1750-
}
1751-
Interpolator interpolator = request.getSession().getService(Interpolator.class);
1752-
1753-
class ProfileInterpolator extends MavenTransformer implements UnaryOperator<Profile> {
1754-
ProfileInterpolator() {
1755-
super(s -> {
1756-
try {
1757-
return interpolator.interpolate(
1758-
s, Interpolator.chain(context::getUserProperty, context::getSystemProperty));
1759-
} catch (InterpolatorException e) {
1760-
problems.add(Severity.ERROR, Version.BASE, e.getMessage(), e);
1761-
}
1762-
return s;
1763-
});
1764-
}
1765-
1766-
@Override
1767-
public Profile apply(Profile p) {
1768-
return Profile.newBuilder(p)
1769-
.activation(transformActivation(p.getActivation()))
1770-
.build();
1771-
}
1772-
1773-
@Override
1774-
protected Activation.Builder transformActivation_Condition(
1775-
Supplier<? extends Activation.Builder> creator, Activation.Builder builder, Activation target) {
1776-
// do not interpolate the condition activation
1777-
return builder;
1778-
}
1779-
1780-
@Override
1781-
protected ActivationFile.Builder transformActivationFile_Missing(
1782-
Supplier<? extends ActivationFile.Builder> creator,
1783-
ActivationFile.Builder builder,
1784-
ActivationFile target) {
1785-
String path = target.getMissing();
1786-
String xformed = transformPath(path, target, "missing");
1787-
return xformed != path ? (builder != null ? builder : creator.get()).missing(xformed) : builder;
1788-
}
1789-
1790-
@Override
1791-
protected ActivationFile.Builder transformActivationFile_Exists(
1792-
Supplier<? extends ActivationFile.Builder> creator,
1793-
ActivationFile.Builder builder,
1794-
ActivationFile target) {
1795-
final String path = target.getExists();
1796-
final String xformed = transformPath(path, target, "exists");
1797-
return xformed != path ? (builder != null ? builder : creator.get()).exists(xformed) : builder;
1798-
}
1799-
1800-
private String transformPath(String path, ActivationFile target, String locationKey) {
1801-
try {
1802-
return context.interpolatePath(path);
1803-
} catch (InterpolatorException e) {
1804-
problems.add(
1805-
Severity.ERROR,
1806-
Version.BASE,
1807-
"Failed to interpolate file location " + path + ": " + e.getMessage(),
1808-
target.getLocation(locationKey),
1809-
e);
1810-
}
1811-
return path;
1812-
}
1813-
}
1814-
return profiles.stream().map(new ProfileInterpolator()).toList();
1815-
}
1816-
18171735
record ModelResolverResult(ModelSource source, String resolvedVersion) {}
18181736

18191737
class CachingModelResolver implements ModelResolver {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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.it;
20+
21+
import org.junit.jupiter.api.Test;
22+
23+
import java.nio.file.Path;
24+
25+
/**
26+
* This is a test set for <a href="https://issues.apache.org/jira/browse/MNG-8477">MNG-8477</a>.
27+
*/
28+
class MavenITmng8477MultithreadedFileActivationTest extends AbstractMavenIntegrationTestCase {
29+
30+
MavenITmng8477MultithreadedFileActivationTest() {
31+
super("[4.0.0-rc-3-SNAPSHOT,)");
32+
}
33+
34+
/**
35+
* Verify project is buildable.
36+
*/
37+
@Test
38+
void testIt() throws Exception {
39+
Path basedir = extractResources("/mng-8477").getAbsoluteFile().toPath();
40+
41+
Verifier verifier = newVerifier(basedir.toString());
42+
verifier.addCliArguments("help:active-profiles", "-Dmaven.modelBuilder.parallelism=1");
43+
verifier.execute();
44+
verifier.verifyErrorFreeLog();
45+
46+
verifier.verifyTextInLog("- xxx (source: test:m2:jar:1)");
47+
}
48+
}

its/core-it-suite/src/test/java/org/apache/maven/it/TestSuiteOrdering.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ public TestSuiteOrdering() {
100100
* the tests are to finishing. Newer tests are also more likely to fail, so this is
101101
* a fail fast technique as well.
102102
*/
103+
suite.addTestSuite(MavenITmng8477MultithreadedFileActivationTest.class);
103104
suite.addTestSuite(MavenITmng8469InterpolationPrecendenceTest.class);
104105
suite.addTestSuite(MavenITmng8461SpySettingsEventTest.class);
105106
suite.addTestSuite(MavenITmng8414ConsumerPomWithNewFeaturesTest.class);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>test</groupId>
7+
<artifactId>m1</artifactId>
8+
<parent>
9+
<groupId>test</groupId>
10+
<artifactId>project</artifactId>
11+
<version>1</version>
12+
</parent></project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>test</groupId>
7+
<artifactId>m2</artifactId>
8+
<parent>
9+
<groupId>test</groupId>
10+
<artifactId>project</artifactId>
11+
<version>1</version>
12+
</parent></project>

its/core-it-suite/src/test/resources/mng-8477/m2/src/io.properties

Whitespace-only changes.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>test</groupId>
7+
<artifactId>project</artifactId>
8+
<version>1</version>
9+
<packaging>pom</packaging>
10+
<profiles>
11+
<profile>
12+
<id>xxx</id>
13+
<dependencies>
14+
<dependency>
15+
<groupId>junit</groupId>
16+
<artifactId>junit</artifactId>
17+
<version>4.0</version>
18+
</dependency>
19+
</dependencies>
20+
<activation>
21+
<file>
22+
<exists>src/io.properties</exists>
23+
</file>
24+
</activation>
25+
</profile>
26+
</profiles>
27+
<modules>
28+
<module>m1</module>
29+
<module>m2</module>
30+
</modules></project>

0 commit comments

Comments
 (0)