Add a o.a.maven.api.services.Source#resolve method

This commit is contained in:
Guillaume Nodet 2023-09-20 21:03:22 +02:00
parent 1865c9c6fd
commit 59a8d90397
2 changed files with 47 additions and 12 deletions

View File

@ -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);
}

View File

@ -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());
}
}
}