This is an automated email from the ASF dual-hosted git repository. gnodet pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/maven.git
commit 59a8d903977b5b7d8decaf8bca4004689200d134 Author: Guillaume Nodet <gno...@gmail.com> AuthorDate: Wed Sep 20 21:03:22 2023 +0200 Add a o.a.maven.api.services.Source#resolve method --- .../java/org/apache/maven/api/services/Source.java | 14 +++++++ .../maven/internal/impl/DefaultProjectBuilder.java | 45 ++++++++++++++++------ 2 files changed, 47 insertions(+), 12 deletions(-) diff --git a/api/maven-api-core/src/main/java/org/apache/maven/api/services/Source.java b/api/maven-api-core/src/main/java/org/apache/maven/api/services/Source.java index 308adde257..9b393d668d 100644 --- a/api/maven-api-core/src/main/java/org/apache/maven/api/services/Source.java +++ b/api/maven-api-core/src/main/java/org/apache/maven/api/services/Source.java @@ -72,4 +72,18 @@ public interface Source { */ @Nonnull String getLocation(); + + /** + * Returns a new source identified by a relative path. Implementation <strong>MUST</strong> + * be able to accept <code>relative</code> parameter values that + * <ul> + * <li>use either / or \ file path separator,</li> + * <li>have .. parent directory references,</li> + * <li>point either at file or directory.</li> + * </ul> + * + * @param relative is the path of the requested source relative to this source + * @return related source or <code>null</code> if no such source + */ + Source resolve(String relative); } diff --git a/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultProjectBuilder.java b/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultProjectBuilder.java index 30520920be..cd41925c9a 100644 --- a/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultProjectBuilder.java +++ b/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultProjectBuilder.java @@ -25,6 +25,7 @@ import javax.inject.Singleton; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.net.URI; import java.nio.file.Path; import java.util.Collection; import java.util.List; @@ -46,7 +47,7 @@ import org.apache.maven.api.services.Source; import org.apache.maven.artifact.DefaultArtifact; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.model.building.ModelProblem; -import org.apache.maven.model.building.ModelSource; +import org.apache.maven.model.building.ModelSource2; import org.apache.maven.project.DefaultProjectBuildingRequest; import org.apache.maven.project.ProjectBuildingException; import org.apache.maven.project.ProjectBuildingRequest; @@ -82,17 +83,7 @@ public class DefaultProjectBuilder implements ProjectBuilder { res = builder.build(path.toFile(), req); } else if (request.getSource().isPresent()) { Source source = request.getSource().get(); - ModelSource modelSource = new ModelSource() { - @Override - public InputStream getInputStream() throws IOException { - return source.openStream(); - } - - @Override - public String getLocation() { - return source.getLocation(); - } - }; + ModelSource2 modelSource = new SourceWrapper(source); res = builder.build(modelSource, req); } else if (request.getArtifact().isPresent()) { Artifact a = request.getArtifact().get(); @@ -228,4 +219,34 @@ public class DefaultProjectBuilder implements ProjectBuilder { throw new ProjectBuilderException("Unable to build project", e); } } + + private static class SourceWrapper implements ModelSource2 { + private final Source source; + + SourceWrapper(Source source) { + this.source = source; + } + + @Override + public InputStream getInputStream() throws IOException { + return source.openStream(); + } + + @Override + public String getLocation() { + return source.getLocation(); + } + + @Override + public ModelSource2 getRelatedSource(String relPath) { + Source rel = source.resolve(relPath); + return rel != null ? new SourceWrapper(rel) : null; + } + + @Override + public URI getLocationURI() { + Path path = source.getPath(); + return path != null ? path.toUri() : URI.create(source.getLocation()); + } + } }