Skip to content

Commit 46707e0

Browse files
authored
[MNG-8340] Resolve parent according to the exact model location (#1857)
1 parent 903cf59 commit 46707e0

File tree

15 files changed

+251
-166
lines changed

15 files changed

+251
-166
lines changed

api/maven-api-core/src/main/java/org/apache/maven/api/services/ModelBuilderRequest.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,26 @@ public interface ModelBuilderRequest {
4949
*/
5050
enum RequestType {
5151
/**
52-
* The request is for building a model from a POM file in a project on the filesystem.
52+
* The request is for building an initial model from a POM file in a project on the filesystem.
5353
*/
54-
BUILD_POM,
54+
BUILD_PROJECT,
5555
/**
56-
* The request is for building the consumer POM.
56+
* The request is for rebuilding the effective POM in a project on the filesystem.
5757
*/
58-
CONSUMER_POM,
58+
BUILD_EFFECTIVE,
59+
/**
60+
* The request is used specifically to parse the POM used as a basis for creating the consumer POM.
61+
* This POM will not ungergo any profile activation.
62+
*/
63+
BUILD_CONSUMER,
5964
/**
6065
* The request is for building a model from a parent POM file from a downloaded artifact.
6166
*/
62-
PARENT_POM,
67+
CONSUMER_PARENT,
6368
/**
6469
* The request is for building a model from a dependency POM file from a downloaded artifact.
6570
*/
66-
DEPENDENCY
71+
CONSUMER_DEPENDENCY
6772
}
6873

6974
/**

impl/maven-core/src/main/java/org/apache/maven/internal/transformation/impl/DefaultConsumerPomBuilder.java

Lines changed: 2 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,9 @@
2121
import javax.inject.Inject;
2222
import javax.inject.Named;
2323

24-
import java.io.File;
25-
import java.io.IOException;
26-
import java.io.InputStream;
27-
import java.nio.file.Files;
2824
import java.nio.file.Path;
2925
import java.util.ArrayList;
3026
import java.util.List;
31-
import java.util.Objects;
3227
import java.util.stream.Collectors;
3328

3429
import org.apache.maven.api.SessionData;
@@ -44,7 +39,6 @@
4439
import org.apache.maven.api.services.ModelBuilderRequest;
4540
import org.apache.maven.api.services.ModelBuilderResult;
4641
import org.apache.maven.api.services.ModelSource;
47-
import org.apache.maven.api.services.Source;
4842
import org.apache.maven.api.services.model.LifecycleBindingsInjector;
4943
import org.apache.maven.internal.impl.InternalSession;
5044
import org.apache.maven.model.v4.MavenModelVersion;
@@ -95,10 +89,9 @@ private ModelBuilderResult buildModel(RepositorySystemSession session, MavenProj
9589
throws ModelBuilderException {
9690
InternalSession iSession = InternalSession.from(session);
9791
ModelBuilderRequest.ModelBuilderRequestBuilder request = ModelBuilderRequest.builder();
98-
request.requestType(ModelBuilderRequest.RequestType.CONSUMER_POM);
92+
request.requestType(ModelBuilderRequest.RequestType.BUILD_CONSUMER);
9993
request.session(iSession);
100-
// in order to resolve parents, we need to fake being at the correct location
101-
request.source(new PomConsumerModelSource(project.getModel().getPomPath(), src));
94+
request.source(ModelSource.fromPath(src));
10295
request.locationTracking(false);
10396
request.systemProperties(session.getSystemProperties());
10497
request.userProperties(session.getUserProperties());
@@ -208,63 +201,4 @@ private static List<Repository> pruneRepositories(List<Repository> repositories)
208201
.filter(r -> !org.apache.maven.api.Repository.CENTRAL_ID.equals(r.getId()))
209202
.collect(Collectors.toList());
210203
}
211-
212-
static class PomConsumerModelSource implements ModelSource {
213-
final Path path;
214-
final Path src;
215-
216-
PomConsumerModelSource(Path path, Path src) {
217-
this.path = path;
218-
this.src = src;
219-
}
220-
221-
@Override
222-
public Path getPath() {
223-
return path;
224-
}
225-
226-
@Override
227-
public InputStream openStream() throws IOException {
228-
return Files.newInputStream(src);
229-
}
230-
231-
@Override
232-
public String getLocation() {
233-
return src.toString();
234-
}
235-
236-
@Override
237-
public Source resolve(String relative) {
238-
return ModelSource.fromPath(path.resolve(relative));
239-
}
240-
241-
@Override
242-
public ModelSource resolve(ModelLocator locator, String relative) {
243-
String norm = relative.replace('\\', File.separatorChar).replace('/', File.separatorChar);
244-
Path path = getPath().getParent().resolve(norm);
245-
Path relatedPom = locator.locateExistingPom(path);
246-
if (relatedPom != null) {
247-
return ModelSource.fromPath(relatedPom);
248-
}
249-
return null;
250-
}
251-
252-
@Override
253-
public boolean equals(Object o) {
254-
return this == o
255-
|| o.getClass() == getClass()
256-
&& Objects.equals(path, ((PomConsumerModelSource) o).path)
257-
&& Objects.equals(src, ((PomConsumerModelSource) o).src);
258-
}
259-
260-
@Override
261-
public int hashCode() {
262-
return Objects.hash(path, src);
263-
}
264-
265-
@Override
266-
public String toString() {
267-
return "PomConsumerModelSource[" + "path=" + path + ']';
268-
}
269-
}
270204
}

impl/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,8 @@ ProjectBuildingResult build(Path pomFile, ModelSource modelSource) throws Projec
351351
ModelBuilderRequest.RequestType type = pomFile != null
352352
&& this.request.isProcessPlugins()
353353
&& this.request.getValidationLevel() == ModelBuildingRequest.VALIDATION_LEVEL_STRICT
354-
? ModelBuilderRequest.RequestType.BUILD_POM
355-
: ModelBuilderRequest.RequestType.PARENT_POM;
354+
? ModelBuilderRequest.RequestType.BUILD_EFFECTIVE
355+
: ModelBuilderRequest.RequestType.CONSUMER_PARENT;
356356
MavenProject theProject = project;
357357
ModelBuilderRequest request = builder.source(modelSource)
358358
.requestType(type)
@@ -489,7 +489,7 @@ private List<ProjectBuildingResult> build(File pomFile, boolean recursive) {
489489
};
490490
ModelBuilderRequest modelBuildingRequest = getModelBuildingRequest()
491491
.source(ModelSource.fromPath(pomFile.toPath()))
492-
.requestType(ModelBuilderRequest.RequestType.BUILD_POM)
492+
.requestType(ModelBuilderRequest.RequestType.BUILD_PROJECT)
493493
.locationTracking(true)
494494
.recursive(recursive)
495495
.lifecycleBindingsInjector(injector)
@@ -780,7 +780,7 @@ private ModelBuilderRequest.ModelBuilderRequestBuilder getModelBuildingRequest()
780780

781781
InternalSession internalSession = InternalSession.from(session);
782782
modelBuildingRequest.session(internalSession);
783-
modelBuildingRequest.requestType(ModelBuilderRequest.RequestType.BUILD_POM);
783+
modelBuildingRequest.requestType(ModelBuilderRequest.RequestType.BUILD_PROJECT);
784784
modelBuildingRequest.profiles(
785785
request.getProfiles() != null
786786
? request.getProfiles().stream()
@@ -913,7 +913,7 @@ private Model injectLifecycleBindings(
913913
}
914914
project.setPluginArtifactRepositories(pluginRepositories);
915915

916-
if (request.getRequestType() == ModelBuilderRequest.RequestType.BUILD_POM) {
916+
if (request.getRequestType() == ModelBuilderRequest.RequestType.BUILD_PROJECT) {
917917
try {
918918
ProjectRealmCache.CacheRecord record =
919919
projectBuildingHelper.createProjectRealm(project, model3, projectBuildingRequest);

impl/maven-core/src/test/java/org/apache/maven/internal/transformation/impl/ConsumerPomBuilderTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ void testTrivialConsumer() throws Exception {
7777
Model orgModel = mbs.build(ModelBuilderRequest.builder()
7878
.session(InternalSession.from(session))
7979
.source(ModelSource.fromPath(file))
80-
.requestType(ModelBuilderRequest.RequestType.BUILD_POM)
80+
.requestType(ModelBuilderRequest.RequestType.BUILD_PROJECT)
8181
.build())
8282
.getEffectiveModel();
8383

@@ -103,7 +103,7 @@ void testSimpleConsumer() throws Exception {
103103
Model orgModel = mbs.build(ModelBuilderRequest.builder()
104104
.session(InternalSession.from(session))
105105
.source(ModelSource.fromPath(file))
106-
.requestType(ModelBuilderRequest.RequestType.BUILD_POM)
106+
.requestType(ModelBuilderRequest.RequestType.BUILD_PROJECT)
107107
.build())
108108
.getEffectiveModel();
109109

0 commit comments

Comments
 (0)