Skip to content

Commit

Permalink
[MNG-8340] Resolve parent according to the exact model location
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Nov 1, 2024
1 parent 4e1152f commit 79f4756
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,16 @@ TransformedArtifact createConsumerPomArtifact(
void transform(MavenProject project, RepositorySystemSession session, Path src, Path tgt)
throws ModelBuildingException, XMLStreamException, IOException {
Model model = builder.build(session, project, src);
if (model.getParent() != null) {
String rel = model.getParent().getRelativePath();
if (rel == null) {
rel = "..";
}
if (!rel.isEmpty()) {
String newRel = tgt.relativize(src.resolve(src)).toString();
model = model.withParent(model.getParent().withRelativePath(newRel));
}
}
write(model, tgt);
}

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 @@ -97,8 +91,7 @@ private ModelBuilderResult buildModel(RepositorySystemSession session, MavenProj
ModelBuilderRequest.ModelBuilderRequestBuilder request = ModelBuilderRequest.builder();
request.requestType(ModelBuilderRequest.RequestType.CONSUMER_POM);
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 @@ -1169,8 +1169,12 @@ private Model readEffectiveModel() throws ModelBuilderException {
relPath = inputModel
.getPomFile()
.getParent()
.toAbsolutePath()
.relativize(parentModel.getPomFile().getParent())
.toString();
if (relPath.equals("..")) {
relPath = null;
}
} else {
relPath = "..";
}
Expand Down

0 comments on commit 79f4756

Please sign in to comment.