Skip to content

Commit

Permalink
[MNG-8340] Resolve parent according to the exact model location (#1857)
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet authored Nov 13, 2024
1 parent 903cf59 commit 46707e0
Show file tree
Hide file tree
Showing 15 changed files with 251 additions and 166 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,26 @@ public interface ModelBuilderRequest {
*/
enum RequestType {
/**
* The request is for building a model from a POM file in a project on the filesystem.
* The request is for building an initial model from a POM file in a project on the filesystem.
*/
BUILD_POM,
BUILD_PROJECT,
/**
* The request is for building the consumer POM.
* The request is for rebuilding the effective POM in a project on the filesystem.
*/
CONSUMER_POM,
BUILD_EFFECTIVE,
/**
* The request is used specifically to parse the POM used as a basis for creating the consumer POM.
* This POM will not ungergo any profile activation.
*/
BUILD_CONSUMER,
/**
* The request is for building a model from a parent POM file from a downloaded artifact.
*/
PARENT_POM,
CONSUMER_PARENT,
/**
* The request is for building a model from a dependency POM file from a downloaded artifact.
*/
DEPENDENCY
CONSUMER_DEPENDENCY
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,9 @@
import javax.inject.Inject;
import javax.inject.Named;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import org.apache.maven.api.SessionData;
Expand All @@ -44,7 +39,6 @@
import org.apache.maven.api.services.ModelBuilderRequest;
import org.apache.maven.api.services.ModelBuilderResult;
import org.apache.maven.api.services.ModelSource;
import org.apache.maven.api.services.Source;
import org.apache.maven.api.services.model.LifecycleBindingsInjector;
import org.apache.maven.internal.impl.InternalSession;
import org.apache.maven.model.v4.MavenModelVersion;
Expand Down Expand Up @@ -95,10 +89,9 @@ private ModelBuilderResult buildModel(RepositorySystemSession session, MavenProj
throws ModelBuilderException {
InternalSession iSession = InternalSession.from(session);
ModelBuilderRequest.ModelBuilderRequestBuilder request = ModelBuilderRequest.builder();
request.requestType(ModelBuilderRequest.RequestType.CONSUMER_POM);
request.requestType(ModelBuilderRequest.RequestType.BUILD_CONSUMER);
request.session(iSession);
// in order to resolve parents, we need to fake being at the correct location
request.source(new PomConsumerModelSource(project.getModel().getPomPath(), src));
request.source(ModelSource.fromPath(src));
request.locationTracking(false);
request.systemProperties(session.getSystemProperties());
request.userProperties(session.getUserProperties());
Expand Down Expand Up @@ -208,63 +201,4 @@ private static List<Repository> pruneRepositories(List<Repository> repositories)
.filter(r -> !org.apache.maven.api.Repository.CENTRAL_ID.equals(r.getId()))
.collect(Collectors.toList());
}

static class PomConsumerModelSource implements ModelSource {
final Path path;
final Path src;

PomConsumerModelSource(Path path, Path src) {
this.path = path;
this.src = src;
}

@Override
public Path getPath() {
return path;
}

@Override
public InputStream openStream() throws IOException {
return Files.newInputStream(src);
}

@Override
public String getLocation() {
return src.toString();
}

@Override
public Source resolve(String relative) {
return ModelSource.fromPath(path.resolve(relative));
}

@Override
public ModelSource resolve(ModelLocator locator, String relative) {
String norm = relative.replace('\\', File.separatorChar).replace('/', File.separatorChar);
Path path = getPath().getParent().resolve(norm);
Path relatedPom = locator.locateExistingPom(path);
if (relatedPom != null) {
return ModelSource.fromPath(relatedPom);
}
return null;
}

@Override
public boolean equals(Object o) {
return this == o
|| o.getClass() == getClass()
&& Objects.equals(path, ((PomConsumerModelSource) o).path)
&& Objects.equals(src, ((PomConsumerModelSource) o).src);
}

@Override
public int hashCode() {
return Objects.hash(path, src);
}

@Override
public String toString() {
return "PomConsumerModelSource[" + "path=" + path + ']';
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,8 @@ ProjectBuildingResult build(Path pomFile, ModelSource modelSource) throws Projec
ModelBuilderRequest.RequestType type = pomFile != null
&& this.request.isProcessPlugins()
&& this.request.getValidationLevel() == ModelBuildingRequest.VALIDATION_LEVEL_STRICT
? ModelBuilderRequest.RequestType.BUILD_POM
: ModelBuilderRequest.RequestType.PARENT_POM;
? ModelBuilderRequest.RequestType.BUILD_EFFECTIVE
: ModelBuilderRequest.RequestType.CONSUMER_PARENT;
MavenProject theProject = project;
ModelBuilderRequest request = builder.source(modelSource)
.requestType(type)
Expand Down Expand Up @@ -489,7 +489,7 @@ private List<ProjectBuildingResult> build(File pomFile, boolean recursive) {
};
ModelBuilderRequest modelBuildingRequest = getModelBuildingRequest()
.source(ModelSource.fromPath(pomFile.toPath()))
.requestType(ModelBuilderRequest.RequestType.BUILD_POM)
.requestType(ModelBuilderRequest.RequestType.BUILD_PROJECT)
.locationTracking(true)
.recursive(recursive)
.lifecycleBindingsInjector(injector)
Expand Down Expand Up @@ -780,7 +780,7 @@ private ModelBuilderRequest.ModelBuilderRequestBuilder getModelBuildingRequest()

InternalSession internalSession = InternalSession.from(session);
modelBuildingRequest.session(internalSession);
modelBuildingRequest.requestType(ModelBuilderRequest.RequestType.BUILD_POM);
modelBuildingRequest.requestType(ModelBuilderRequest.RequestType.BUILD_PROJECT);
modelBuildingRequest.profiles(
request.getProfiles() != null
? request.getProfiles().stream()
Expand Down Expand Up @@ -913,7 +913,7 @@ private Model injectLifecycleBindings(
}
project.setPluginArtifactRepositories(pluginRepositories);

if (request.getRequestType() == ModelBuilderRequest.RequestType.BUILD_POM) {
if (request.getRequestType() == ModelBuilderRequest.RequestType.BUILD_PROJECT) {
try {
ProjectRealmCache.CacheRecord record =
projectBuildingHelper.createProjectRealm(project, model3, projectBuildingRequest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void testTrivialConsumer() throws Exception {
Model orgModel = mbs.build(ModelBuilderRequest.builder()
.session(InternalSession.from(session))
.source(ModelSource.fromPath(file))
.requestType(ModelBuilderRequest.RequestType.BUILD_POM)
.requestType(ModelBuilderRequest.RequestType.BUILD_PROJECT)
.build())
.getEffectiveModel();

Expand All @@ -103,7 +103,7 @@ void testSimpleConsumer() throws Exception {
Model orgModel = mbs.build(ModelBuilderRequest.builder()
.session(InternalSession.from(session))
.source(ModelSource.fromPath(file))
.requestType(ModelBuilderRequest.RequestType.BUILD_POM)
.requestType(ModelBuilderRequest.RequestType.BUILD_PROJECT)
.build())
.getEffectiveModel();

Expand Down
Loading

0 comments on commit 46707e0

Please sign in to comment.