[MNG-8059] Paths everywhere (#1413)

Make execution flow never invoke path.toFile, at least what model-builder and maven-resolver-provider matters. Using new methods from Resolver 2 alpha8 and also adding in relevant maven bits, as half of the work was already done.

---

https://issues.apache.org/jira/browse/MNG-8059
This commit is contained in:
Tamas Cservenak 2024-02-26 18:04:44 +01:00 committed by GitHub
parent a31c145ae9
commit cf438ca624
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
51 changed files with 595 additions and 176 deletions

View File

@ -388,14 +388,30 @@
* @return The POM file from which this model originated or {@code null} if this model does not belong to a local
* project (e.g. describes the metadata of some artifact from the repository).
*/
@Deprecated
public java.io.File getPomFile() {
return (getDelegate().getPomFile() != null) ? getDelegate().getPomFile().toFile() : null;
}
@Deprecated
public void setPomFile(java.io.File pomFile) {
update( getDelegate().withPomFile(pomFile != null ? pomFile.toPath() : null));
}
/**
* Gets the POM file for the corresponding project (if any).
*
* @return The POM file from which this model originated or {@code null} if this model does not belong to a local
* project (e.g. describes the metadata of some artifact from the repository).
*/
public java.nio.file.Path getPomPath() {
return (getDelegate().getPomFile() != null) ? getDelegate().getPomFile() : null;
}
public void setPomPath(java.nio.file.Path pomPath) {
update( getDelegate().withPomFile(pomPath));
}
public void setModelEncoding(String modelEncoding) {
update(getDelegate().with().modelEncoding(modelEncoding).build());
}
@ -406,10 +422,21 @@
* @return The base directory for the corresponding project or {@code null} if this model does not belong to a local
* project (e.g. describes the metadata of some artifact from the repository).
*/
@Deprecated
public java.io.File getProjectDirectory() {
return (getDelegate().getProjectDirectory() != null) ? getDelegate().getProjectDirectory().toFile() : null;
}
/**
* Gets the base directory for the corresponding project (if any).
*
* @return The base directory for the corresponding project or {@code null} if this model does not belong to a local
* project (e.g. describes the metadata of some artifact from the repository).
*/
public java.nio.file.Path getProjectDirectoryPath() {
return getDelegate().getProjectDirectory();
}
/**
* @return the model id as {@code groupId:artifactId:packaging:version}
*/

View File

@ -75,7 +75,7 @@ class DefaultProblem implements Problem {
public String getLocation() {
StringBuilder buffer = new StringBuilder(256);
if (getSource().length() > 0) {
if (!getSource().isEmpty()) {
if (buffer.length() > 0) {
buffer.append(", ");
}
@ -106,7 +106,7 @@ class DefaultProblem implements Problem {
public String getMessage() {
String msg;
if (message != null && message.length() > 0) {
if (message != null && !message.isEmpty()) {
msg = message;
} else {
msg = exception.getMessage();

View File

@ -27,7 +27,7 @@ import java.util.List;
*/
class DefaultProblemCollector implements ProblemCollector {
private List<Problem> problems;
private final List<Problem> problems;
private String source;

View File

@ -22,6 +22,7 @@ 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.Objects;
/**
@ -29,7 +30,7 @@ import java.util.Objects;
*
*/
public class FileSource implements Source {
private final File file;
private final Path path;
private final int hashCode;
@ -37,29 +38,53 @@ public class FileSource implements Source {
* Creates a new source backed by the specified file.
*
* @param file The file, must not be {@code null}.
* @deprecated Use {@link #FileSource(Path)} instead.
*/
@Deprecated
public FileSource(File file) {
this.file = Objects.requireNonNull(file, "file cannot be null").getAbsoluteFile();
this.hashCode = Objects.hash(file);
this(Objects.requireNonNull(file, "file cannot be null").toPath());
}
/**
* Creates a new source backed by the specified file.
*
* @param path The file, must not be {@code null}.
* @since 4.0.0
*/
public FileSource(Path path) {
this.path = Objects.requireNonNull(path, "path cannot be null").toAbsolutePath();
this.hashCode = Objects.hash(path);
}
@Override
public InputStream getInputStream() throws IOException {
return Files.newInputStream(file.toPath());
return Files.newInputStream(path);
}
@Override
public String getLocation() {
return file.getPath();
return path.toString();
}
/**
* Gets the file of this source.
*
* @return The underlying file, never {@code null}.
* @deprecated Use {@link #getPath()} instead.
*/
@Deprecated
public File getFile() {
return file;
return path.toFile();
}
/**
* Gets the file of this source.
*
* @return The underlying file, never {@code null}.
* @since 4.0.0
*/
public Path getPath() {
return path;
}
@Override
@ -87,6 +112,6 @@ public class FileSource implements Source {
}
FileSource other = (FileSource) obj;
return this.file.equals(other.file);
return this.path.equals(other.path);
}
}

View File

@ -87,6 +87,6 @@ public class UrlSource implements Source {
}
UrlSource other = (UrlSource) obj;
return this.url.equals(other.url);
return Objects.equals(url.toExternalForm(), other.url.toExternalForm());
}
}

View File

@ -32,7 +32,9 @@ class FileSourceTest {
@Test
void testFileSource() {
NullPointerException e = assertThrows(
NullPointerException.class, () -> new FileSource(null), "Should fail, since you must specify a file");
NullPointerException.class,
() -> new FileSource((File) null),
"Should fail, since you must specify a file");
assertEquals("file cannot be null", e.getMessage());
}

View File

@ -20,6 +20,7 @@ package org.apache.maven.artifact.repository.metadata;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Map;
@ -93,6 +94,11 @@ public final class MetadataBridge extends AbstractMetadata implements MergeableM
return this;
}
@Override
public Path getPath() {
return null;
}
public Nature getNature() {
if (metadata instanceof RepositoryMetadata) {
switch (((RepositoryMetadata) metadata).getNature()) {

View File

@ -31,6 +31,7 @@ import java.util.List;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.impl.ArtifactResolver;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.resolution.ArtifactRequest;
import org.eclipse.aether.resolution.ArtifactResolutionException;
import org.eclipse.aether.resolution.ArtifactResult;
@ -63,7 +64,7 @@ public class ClasspathArtifactResolver implements ArtifactResolver {
throw new IllegalStateException("Missing test POM for " + artifact, e);
}
} else {
result.addException(new ArtifactNotFoundException(artifact, null));
result.addException(new ArtifactNotFoundException(artifact, (RemoteRepository) null));
throw new ArtifactResolutionException(results);
}
}

View File

@ -34,6 +34,7 @@ import org.apache.maven.artifact.handler.DefaultArtifactHandler;
import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
import org.apache.maven.repository.internal.artifact.MavenArtifactProperties;
import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession;
@ -144,7 +145,7 @@ public class RepositoryUtils {
Map<String, String> props = null;
if (org.apache.maven.artifact.Artifact.SCOPE_SYSTEM.equals(artifact.getScope())) {
String localPath = (artifact.getFile() != null) ? artifact.getFile().getPath() : "";
props = Collections.singletonMap(ArtifactProperties.LOCAL_PATH, localPath);
props = Collections.singletonMap(MavenArtifactProperties.LOCAL_PATH, localPath);
}
Artifact result = new DefaultArtifact(
@ -252,9 +253,9 @@ public class RepositoryUtils {
null,
null,
null,
Boolean.parseBoolean(artifact.getProperty(ArtifactProperties.INCLUDES_DEPENDENCIES, "")),
Boolean.parseBoolean(artifact.getProperty(MavenArtifactProperties.INCLUDES_DEPENDENCIES, "")),
artifact.getProperty(ArtifactProperties.LANGUAGE, null),
Boolean.parseBoolean(artifact.getProperty(ArtifactProperties.CONSTITUTES_BUILD_PATH, "")));
Boolean.parseBoolean(artifact.getProperty(MavenArtifactProperties.CONSTITUTES_BUILD_PATH, "")));
}
public static ArtifactType newArtifactType(String id, ArtifactHandler handler) {
@ -279,7 +280,7 @@ public class RepositoryUtils {
Map<String, String> props = null;
if (system) {
props = Collections.singletonMap(ArtifactProperties.LOCAL_PATH, dependency.getSystemPath());
props = Collections.singletonMap(MavenArtifactProperties.LOCAL_PATH, dependency.getSystemPath());
}
Artifact artifact = new DefaultArtifact(

View File

@ -22,8 +22,15 @@ import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import java.io.File;
import java.util.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;
@ -47,10 +54,7 @@ import org.apache.maven.settings.crypto.DefaultSettingsDecryptionRequest;
import org.apache.maven.settings.crypto.SettingsDecrypter;
import org.apache.maven.settings.crypto.SettingsDecryptionResult;
import org.codehaus.plexus.configuration.PlexusConfiguration;
import org.eclipse.aether.ConfigurationProperties;
import org.eclipse.aether.RepositoryListener;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.*;
import org.eclipse.aether.RepositorySystemSession.SessionBuilder;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;
@ -431,16 +435,16 @@ class DefaultRepositorySystemSessionFactory implements RepositorySystemSessionFa
String resolverDependencyManagerTransitivity =
mergedProps.getOrDefault(MAVEN_RESOLVER_DEPENDENCY_MANAGER_TRANSITIVITY_KEY, Boolean.TRUE.toString());
sessionBuilder.setDependencyManager(
new ClassicDependencyManager(Boolean.parseBoolean(resolverDependencyManagerTransitivity)));
sessionBuilder.setDependencyManager(new ClassicDependencyManager(
Boolean.parseBoolean(resolverDependencyManagerTransitivity), SystemScopeHandler.LEGACY));
ArrayList<File> paths = new ArrayList<>();
paths.add(new File(request.getLocalRepository().getBasedir()));
ArrayList<Path> paths = new ArrayList<>();
paths.add(Paths.get(request.getLocalRepository().getBasedir()));
String localRepoTail = mergedProps.get(MAVEN_REPO_LOCAL_TAIL);
if (localRepoTail != null) {
Arrays.stream(localRepoTail.split(","))
.filter(p -> p != null && !p.trim().isEmpty())
.map(File::new)
.map(Paths::get)
.forEach(paths::add);
}
sessionBuilder.withLocalRepositoryBaseDirectories(paths);

View File

@ -103,7 +103,7 @@ class TestApi {
// create session with any local repo, is redefined anyway below
RepositorySystemSession rss = new MavenSessionBuilderSupplier(repositorySystem)
.get()
.withLocalRepositoryBaseDirectories(new File("target"))
.withLocalRepositoryBaseDirectories(new File("target").toPath())
.build();
DefaultMavenExecutionRequest mer = new DefaultMavenExecutionRequest();
DefaultMavenExecutionResult meres = new DefaultMavenExecutionResult();

View File

@ -19,6 +19,7 @@
package org.apache.maven.model.building;
import java.io.File;
import java.nio.file.Path;
import java.util.Objects;
import org.apache.maven.building.FileSource;
@ -37,6 +38,7 @@ public class ArtifactModelSource extends FileSource implements ModelSource {
private final int hashCode;
@Deprecated
public ArtifactModelSource(File file, String groupId, String artifactId, String version) {
super(file);
this.groupId = groupId;
@ -45,6 +47,14 @@ public class ArtifactModelSource extends FileSource implements ModelSource {
this.hashCode = Objects.hash(groupId, artifactId, version);
}
public ArtifactModelSource(Path path, String groupId, String artifactId, String version) {
super(path);
this.groupId = groupId;
this.artifactId = artifactId;
this.version = version;
this.hashCode = Objects.hash(groupId, artifactId, version);
}
public String getGroupId() {
return groupId;
}

View File

@ -982,7 +982,7 @@ public class DefaultModelBuilder implements ModelBuilder {
problems.setRootModel(resultModel);
// model path translation
modelPathTranslator.alignToBaseDirectory(resultModel, resultModel.getProjectDirectory(), request);
modelPathTranslator.alignToBaseDirectory(resultModel, resultModel.getProjectDirectoryPath(), request);
// plugin management injection
pluginManagementInjector.injectManagement(resultModel, request, problems);
@ -1381,7 +1381,7 @@ public class DefaultModelBuilder implements ModelBuilder {
Map<String, Activation> originalActivations = getProfileActivations(model, true);
Model interpolatedModel = new Model(modelInterpolator.interpolateModel(
model.getDelegate(), model.getProjectDirectory(), request, problems));
model.getDelegate(), model.getProjectDirectoryPath(), request, problems));
if (interpolatedModel.getParent() != null) {
StringSearchInterpolator ssi = new StringSearchInterpolator();
ssi.addValueSource(new MapBasedValueSource(request.getUserProperties()));
@ -1404,7 +1404,7 @@ public class DefaultModelBuilder implements ModelBuilder {
problems.add(mpcr);
}
}
interpolatedModel.setPomFile(model.getPomFile());
interpolatedModel.setPomPath(model.getPomPath());
// restore profiles with file activation to their value before full interpolation
injectProfileActivations(model, originalActivations);
@ -1468,7 +1468,7 @@ public class DefaultModelBuilder implements ModelBuilder {
if (candidateModel == null) {
return null;
}
candidateSource = new FileModelSource(candidateModel.getPomFile());
candidateSource = new FileModelSource(candidateModel.getPomPath());
}
//
@ -1604,7 +1604,7 @@ public class DefaultModelBuilder implements ModelBuilder {
buffer.append(" for ").append(ModelProblemUtils.toId(childModel));
}
buffer.append(": ").append(e.getMessage());
if (childModel.getProjectDirectory() != null) {
if (childModel.getProjectDirectoryPath() != null) {
if (parent.getRelativePath() == null || parent.getRelativePath().length() <= 0) {
buffer.append(" and 'parent.relativePath' points at no local POM");
} else {

View File

@ -37,7 +37,7 @@ import org.apache.maven.model.resolution.WorkspaceModelResolver;
public class DefaultModelBuildingRequest implements ModelBuildingRequest {
private Model fileModel;
private File pomFile;
private Path pomPath;
private ModelSource modelSource;
@ -84,7 +84,7 @@ public class DefaultModelBuildingRequest implements ModelBuildingRequest {
* @param request The request to copy, must not be {@code null}.
*/
public DefaultModelBuildingRequest(ModelBuildingRequest request) {
setPomFile(request.getPomFile());
setPomPath(request.getPomPath());
setModelSource(request.getModelSource());
setValidationLevel(request.getValidationLevel());
setProcessPlugins(request.isProcessPlugins());
@ -104,22 +104,34 @@ public class DefaultModelBuildingRequest implements ModelBuildingRequest {
setRootDirectory(request.getRootDirectory());
}
@Deprecated
@Override
public File getPomFile() {
return pomFile;
return pomPath != null ? pomPath.toFile() : null;
}
@Override
public DefaultModelBuildingRequest setPomFile(File pomFile) {
this.pomFile = (pomFile != null) ? pomFile.getAbsoluteFile() : null;
public Path getPomPath() {
return pomPath;
}
@Deprecated
@Override
public DefaultModelBuildingRequest setPomFile(File pomFile) {
this.pomPath = (pomFile != null) ? pomFile.toPath().toAbsolutePath() : null;
return this;
}
@Override
public DefaultModelBuildingRequest setPomPath(Path pomPath) {
this.pomPath = (pomPath != null) ? pomPath.toAbsolutePath() : null;
return this;
}
@Override
public synchronized ModelSource getModelSource() {
if (modelSource == null && pomFile != null) {
modelSource = new FileModelSource(pomFile);
if (modelSource == null && pomPath != null) {
modelSource = new FileModelSource(pomPath);
}
return modelSource;
}

View File

@ -85,11 +85,13 @@ public class DefaultModelProcessor implements ModelProcessor {
this.modelReader = modelReader;
}
@Deprecated
@Override
public File locatePom(File projectDirectory) {
return locatePom(projectDirectory.toPath()).toFile();
}
@Override
public Path locatePom(Path projectDirectory) {
// Note that the ModelProcessor#locatePom never returns null
// while the ModelParser#locatePom needs to return an existing path!
@ -99,30 +101,31 @@ public class DefaultModelProcessor implements ModelProcessor {
.orElse(null))
.filter(Objects::nonNull)
.findFirst()
.orElseGet(
() -> modelLocator.locatePom(projectDirectory.toFile()).toPath());
.orElseGet(() -> modelLocator.locatePom(projectDirectory));
if (!pom.equals(projectDirectory) && !pom.getParent().equals(projectDirectory)) {
throw new IllegalArgumentException("The POM found does not belong to the given directory: " + pom);
}
return pom;
}
@Deprecated
@Override
public File locateExistingPom(File projectDirectory) {
Path path = locateExistingPom(projectDirectory.toPath());
return path != null ? path.toFile() : null;
}
@Override
public Path locateExistingPom(Path projectDirectory) {
// Note that the ModelProcessor#locatePom never returns null
// while the ModelParser#locatePom needs to return an existing path!
Path pom = modelParsers.stream()
.map(m -> m.locate(projectDirectory).map(s -> s.getPath()).orElse(null))
.map(m -> m.locate(projectDirectory)
.map(org.apache.maven.api.services.Source::getPath)
.orElse(null))
.filter(Objects::nonNull)
.findFirst()
.orElseGet(() -> {
File f = modelLocator.locateExistingPom(projectDirectory.toFile());
return f != null ? f.toPath() : null;
});
.orElseGet(() -> modelLocator.locateExistingPom(projectDirectory));
if (pom != null && !pom.equals(projectDirectory) && !pom.getParent().equals(projectDirectory)) {
throw new IllegalArgumentException("The POM found does not belong to the given directory: " + pom);
}
@ -133,7 +136,7 @@ public class DefaultModelProcessor implements ModelProcessor {
Path pomFile, InputStream input, Reader reader, Map<String, ?> options) throws IOException {
Source source = (Source) options.get(ModelProcessor.SOURCE);
if (pomFile == null && source instanceof org.apache.maven.building.FileSource) {
pomFile = ((org.apache.maven.building.FileSource) source).getFile().toPath();
pomFile = ((org.apache.maven.building.FileSource) source).getPath();
}
if (pomFile != null) {
Path projectDirectory = pomFile.getParent();
@ -162,7 +165,7 @@ public class DefaultModelProcessor implements ModelProcessor {
private org.apache.maven.api.model.Model readXmlModel(
Path pomFile, InputStream input, Reader reader, Map<String, ?> options) throws IOException {
if (pomFile != null) {
return modelReader.read(pomFile.toFile(), options).getDelegate();
return modelReader.read(pomFile, options).getDelegate();
} else if (input != null) {
return modelReader.read(input, options).getDelegate();
} else {
@ -170,10 +173,16 @@ public class DefaultModelProcessor implements ModelProcessor {
}
}
@Deprecated
@Override
public org.apache.maven.model.Model read(File file, Map<String, ?> options) throws IOException {
Objects.requireNonNull(file, "file cannot be null");
Path path = file.toPath();
return read(file.toPath(), options);
}
@Override
public org.apache.maven.model.Model read(Path path, Map<String, ?> options) throws IOException {
Objects.requireNonNull(path, "path cannot be null");
org.apache.maven.api.model.Model model = read(path, null, null, options);
return new org.apache.maven.model.Model(model);
}

View File

@ -18,7 +18,6 @@
*/
package org.apache.maven.model.building;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
@ -78,7 +77,7 @@ class DefaultTransformerContextBuilder implements TransformerContextBuilder {
Model model = findRawModel(from, gId, aId);
if (model != null) {
context.modelByGA.put(new GAKey(gId, aId), new Holder(model));
context.modelByPath.put(model.getPomFile().toPath(), new Holder(model));
context.modelByPath.put(model.getPomPath(), new Holder(model));
}
return model;
}
@ -103,7 +102,7 @@ class DefaultTransformerContextBuilder implements TransformerContextBuilder {
source = getSource(groupId, artifactId);
}
if (source != null) {
if (!addEdge(from, source.getFile().toPath(), problems)) {
if (!addEdge(from, source.getPath(), problems)) {
return null;
}
try {
@ -133,19 +132,19 @@ class DefaultTransformerContextBuilder implements TransformerContextBuilder {
if (rootDirectory == null) {
return;
}
List<File> toLoad = new ArrayList<>();
File root = defaultModelBuilder.getModelProcessor().locateExistingPom(rootDirectory.toFile());
List<Path> toLoad = new ArrayList<>();
Path root = defaultModelBuilder.getModelProcessor().locateExistingPom(rootDirectory);
toLoad.add(root);
while (!toLoad.isEmpty()) {
File pom = toLoad.remove(0);
Path pom = toLoad.remove(0);
try {
ModelBuildingRequest gaBuildingRequest =
new DefaultModelBuildingRequest(request).setModelSource(new FileModelSource(pom));
Model rawModel = defaultModelBuilder.readFileModel(gaBuildingRequest, problems);
for (String module : rawModel.getModules()) {
File moduleFile = defaultModelBuilder
Path moduleFile = defaultModelBuilder
.getModelProcessor()
.locateExistingPom(new File(pom.getParent(), module));
.locateExistingPom(pom.getParent().resolve(module));
if (moduleFile != null) {
toLoad.add(moduleFile);
}
@ -165,9 +164,8 @@ class DefaultTransformerContextBuilder implements TransformerContextBuilder {
return null;
}
DefaultModelBuildingRequest req = new DefaultModelBuildingRequest(request)
.setPomFile(p.toFile())
.setModelSource(new FileModelSource(p.toFile()));
DefaultModelBuildingRequest req =
new DefaultModelBuildingRequest(request).setPomPath(p).setModelSource(new FileModelSource(p));
try {
return defaultModelBuilder.readRawModel(req, problems);

View File

@ -20,6 +20,7 @@ package org.apache.maven.model.building;
import java.io.File;
import java.net.URI;
import java.nio.file.Path;
import org.apache.maven.building.FileSource;
import org.apache.maven.model.locator.ModelLocator;
@ -34,11 +35,23 @@ public class FileModelSource extends FileSource implements ModelSource3 {
* Creates a new model source backed by the specified file.
*
* @param pomFile The POM file, must not be {@code null}.
* @deprecated Use {@link #FileModelSource(Path)} instead.
*/
@Deprecated
public FileModelSource(File pomFile) {
super(pomFile);
}
/**
* Creates a new model source backed by the specified file.
*
* @param pomPath The POM file, must not be {@code null}.
* @since 4.0.0
*/
public FileModelSource(Path pomPath) {
super(pomPath);
}
/**
*
* @return the file of this source
@ -54,12 +67,12 @@ public class FileModelSource extends FileSource implements ModelSource3 {
public ModelSource3 getRelatedSource(ModelLocator locator, String relPath) {
relPath = relPath.replace('\\', File.separatorChar).replace('/', File.separatorChar);
File path = new File(getFile().getParentFile(), relPath);
Path path = getPath().getParent().resolve(relPath);
File relatedPom = locator.locateExistingPom(path);
Path relatedPom = locator.locateExistingPom(path);
if (relatedPom != null) {
return new FileModelSource(relatedPom.toPath().normalize().toFile());
return new FileModelSource(relatedPom.normalize());
}
return null;
@ -67,7 +80,7 @@ public class FileModelSource extends FileSource implements ModelSource3 {
@Override
public URI getLocationURI() {
return getFile().toURI();
return getPath().toUri();
}
@Override
@ -84,11 +97,11 @@ public class FileModelSource extends FileSource implements ModelSource3 {
return false;
}
FileModelSource other = (FileModelSource) obj;
return getFile().equals(other.getFile());
return getPath().equals(other.getPath());
}
@Override
public int hashCode() {
return getFile().hashCode();
return getPath().hashCode();
}
}

View File

@ -42,15 +42,27 @@ class FilterModelBuildingRequest implements ModelBuildingRequest {
this.request = request;
}
@Deprecated
@Override
public File getPomFile() {
return request.getPomFile();
}
@Override
public FilterModelBuildingRequest setPomFile(File pomFile) {
request.setPomFile(pomFile);
public Path getPomPath() {
return request.getPomPath();
}
@Deprecated
@Override
public ModelBuildingRequest setPomFile(File pomFile) {
request.setPomFile(pomFile);
return this;
}
@Override
public FilterModelBuildingRequest setPomPath(Path pomPath) {
request.setPomPath(pomPath);
return this;
}

View File

@ -106,7 +106,7 @@ public interface ModelBuildingRequest {
/**
* Sets the source of the POM to process. Eventually, either {@link #setModelSource(ModelSource)} or
* {@link #setPomFile(File)} must be set.
* {@link #setPomPath(Path)} must be set.
*
* @param modelSource The source of the POM to process, may be {@code null}.
* @return This request, never {@code null}.
@ -118,9 +118,20 @@ public interface ModelBuildingRequest {
*
* @return The POM file of the project or {@code null} if not applicable (i.e. when processing a POM from the
* repository).
* @deprecated Use {@link #getPomPath()} instead.
*/
@Deprecated
File getPomFile();
/**
* Gets the POM file of the project to build.
*
* @return The POM file of the project or {@code null} if not applicable (i.e. when processing a POM from the
* repository).
* @since 4.0.0
*/
Path getPomPath();
/**
* Sets the POM file of the project to build. Note that providing the path to a POM file via this method will make
* the model builder operate in project mode. This mode is meant for effective models that are employed during the
@ -131,9 +142,25 @@ public interface ModelBuildingRequest {
* @param pomFile The POM file of the project to build the effective model for, may be {@code null} to build the
* model of some POM from the repository.
* @return This request, never {@code null}.
* @deprecated Use {@link #setPomPath(Path)} instead.
*/
@Deprecated
ModelBuildingRequest setPomFile(File pomFile);
/**
* Sets the POM file of the project to build. Note that providing the path to a POM file via this method will make
* the model builder operate in project mode. This mode is meant for effective models that are employed during the
* build process of a local project. Hence the effective model will support the notion of a project directory. To
* build the model for a POM from the repository, use {@link #setModelSource(ModelSource)} in combination with a
* {@link FileModelSource} instead.
*
* @param pomPath The POM file of the project to build the effective model for, may be {@code null} to build the
* model of some POM from the repository.
* @return This request, never {@code null}.
* @since 4.0.0
*/
ModelBuildingRequest setPomPath(Path pomPath);
/**
* Gets the level of validation to perform on processed models.
*

View File

@ -18,7 +18,7 @@
*/
package org.apache.maven.model.building;
import java.io.File;
import java.nio.file.Path;
import org.apache.maven.model.Model;
@ -43,9 +43,9 @@ public class ModelProblemUtils {
buffer.append(toId(model));
File pomFile = model.getPomFile();
if (pomFile != null) {
buffer.append(" (").append(pomFile).append(')');
Path pomPath = model.getPomPath();
if (pomPath != null) {
buffer.append(" (").append(pomPath).append(')');
}
return buffer.toString();
@ -55,10 +55,10 @@ public class ModelProblemUtils {
String path = "";
if (model != null) {
File pomFile = model.getPomFile();
Path pomPath = model.getPomPath();
if (pomFile != null) {
path = pomFile.getAbsolutePath();
if (pomPath != null) {
path = pomPath.toAbsolutePath().toString();
}
}

View File

@ -20,7 +20,6 @@ package org.apache.maven.model.interpolation;
import javax.inject.Inject;
import java.io.File;
import java.net.URI;
import java.nio.file.Path;
import java.util.ArrayList;
@ -92,7 +91,17 @@ public abstract class AbstractStringBasedModelInterpolator implements ModelInter
@Override
public org.apache.maven.model.Model interpolateModel(
org.apache.maven.model.Model model,
File projectDir,
java.io.File projectDir,
ModelBuildingRequest request,
ModelProblemCollector problems) {
return new org.apache.maven.model.Model(interpolateModel(
model.getDelegate(), projectDir != null ? projectDir.toPath() : null, request, problems));
}
@Override
public org.apache.maven.model.Model interpolateModel(
org.apache.maven.model.Model model,
Path projectDir,
ModelBuildingRequest request,
ModelProblemCollector problems) {
return new org.apache.maven.model.Model(interpolateModel(model.getDelegate(), projectDir, request, problems));
@ -106,7 +115,7 @@ public abstract class AbstractStringBasedModelInterpolator implements ModelInter
protected List<ValueSource> createValueSources(
final Model model,
final File projectDir,
final Path projectDir,
final ModelBuildingRequest config,
ModelProblemCollector problems) {
Map<String, String> modelProperties = model.getProperties();
@ -139,9 +148,9 @@ public abstract class AbstractStringBasedModelInterpolator implements ModelInter
@Override
public Object getValue(String expression) {
if ("basedir".equals(expression)) {
return projectDir.getAbsoluteFile().toPath().toString();
return projectDir.toAbsolutePath().toString();
} else if (expression.startsWith("basedir.")) {
Path basedir = projectDir.getAbsoluteFile().toPath();
Path basedir = projectDir.toAbsolutePath();
return new ObjectBasedValueSource(basedir)
.getValue(expression.substring("basedir.".length()));
}
@ -157,14 +166,9 @@ public abstract class AbstractStringBasedModelInterpolator implements ModelInter
@Override
public Object getValue(String expression) {
if ("baseUri".equals(expression)) {
return projectDir
.getAbsoluteFile()
.toPath()
.toUri()
.toASCIIString();
return projectDir.toAbsolutePath().toUri().toASCIIString();
} else if (expression.startsWith("baseUri.")) {
URI baseUri =
projectDir.getAbsoluteFile().toPath().toUri();
URI baseUri = projectDir.toAbsolutePath().toUri();
return new ObjectBasedValueSource(baseUri)
.getValue(expression.substring("baseUri.".length()));
}
@ -182,12 +186,10 @@ public abstract class AbstractStringBasedModelInterpolator implements ModelInter
@Override
public Object getValue(String expression) {
if ("rootDirectory".equals(expression)) {
Path base = projectDir != null ? projectDir.toPath() : null;
Path root = rootLocator.findMandatoryRoot(base);
Path root = rootLocator.findMandatoryRoot(projectDir);
return root.toFile().getPath();
} else if (expression.startsWith("rootDirectory.")) {
Path base = projectDir != null ? projectDir.toPath() : null;
Path root = rootLocator.findMandatoryRoot(base);
Path root = rootLocator.findMandatoryRoot(projectDir);
return new ObjectBasedValueSource(root)
.getValue(expression.substring("rootDirectory.".length()));
}
@ -217,7 +219,7 @@ public abstract class AbstractStringBasedModelInterpolator implements ModelInter
}
protected List<? extends InterpolationPostProcessor> createPostProcessors(
final Model model, final File projectDir, final ModelBuildingRequest config) {
final Model model, final Path projectDir, final ModelBuildingRequest config) {
List<InterpolationPostProcessor> processors = new ArrayList<>(2);
if (projectDir != null) {
processors.add(new PathTranslatingPostProcessor(

View File

@ -19,6 +19,7 @@
package org.apache.maven.model.interpolation;
import java.io.File;
import java.nio.file.Path;
import org.apache.maven.model.Model;
import org.apache.maven.model.building.ModelBuildingRequest;
@ -42,12 +43,39 @@ public interface ModelInterpolator {
* @param request The model building request that holds further settings, must not be {@code null}.
* @param problems The container used to collect problems that were encountered, must not be {@code null}.
* @return The interpolated model, never {@code null}.
* @deprecated Use {@link #interpolateModel(Model, Path, ModelBuildingRequest, ModelProblemCollector)} instead.
*/
@Deprecated
Model interpolateModel(Model model, File projectDir, ModelBuildingRequest request, ModelProblemCollector problems);
@Deprecated
org.apache.maven.api.model.Model interpolateModel(
org.apache.maven.api.model.Model model,
File projectDir,
ModelBuildingRequest request,
ModelProblemCollector problems);
/**
* Interpolates expressions in the specified model. Note that implementations are free to either interpolate the
* provided model directly or to create a clone of the model and interpolate the clone. Callers should always use
* the returned model and must not rely on the input model being updated.
*
* @param model The model to interpolate, must not be {@code null}.
* @param projectDir The project directory, may be {@code null} if the model does not belong to a local project but
* to some artifact's metadata.
* @param request The model building request that holds further settings, must not be {@code null}.
* @param problems The container used to collect problems that were encountered, must not be {@code null}.
* @return The interpolated model, never {@code null}.
* @since 4.0.0
*/
Model interpolateModel(Model model, Path projectDir, ModelBuildingRequest request, ModelProblemCollector problems);
/**
* @since 4.0.0
*/
org.apache.maven.api.model.Model interpolateModel(
org.apache.maven.api.model.Model model,
Path projectDir,
ModelBuildingRequest request,
ModelProblemCollector problems);
}

View File

@ -18,7 +18,7 @@
*/
package org.apache.maven.model.interpolation;
import java.io.File;
import java.nio.file.Path;
import java.util.Collection;
import java.util.List;
@ -32,14 +32,14 @@ import org.codehaus.plexus.interpolation.util.ValueSourceUtils;
class PathTranslatingPostProcessor implements InterpolationPostProcessor {
private final Collection<String> unprefixedPathKeys;
private final File projectDir;
private final Path projectDir;
private final PathTranslator pathTranslator;
private final List<String> expressionPrefixes;
PathTranslatingPostProcessor(
List<String> expressionPrefixes,
Collection<String> unprefixedPathKeys,
File projectDir,
Path projectDir,
PathTranslator pathTranslator) {
this.expressionPrefixes = expressionPrefixes;
this.unprefixedPathKeys = unprefixedPathKeys;

View File

@ -23,6 +23,7 @@ import javax.inject.Named;
import javax.inject.Singleton;
import java.io.File;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -61,9 +62,16 @@ public class StringVisitorModelInterpolator extends AbstractStringBasedModelInte
String interpolate(String value);
}
@Deprecated
@Override
public Model interpolateModel(
Model model, File projectDir, ModelBuildingRequest config, ModelProblemCollector problems) {
return interpolateModel(model, projectDir != null ? projectDir.toPath() : null, config, problems);
}
@Override
public Model interpolateModel(
Model model, Path projectDir, ModelBuildingRequest config, ModelProblemCollector problems) {
List<? extends ValueSource> valueSources = createValueSources(model, projectDir, config, problems);
List<? extends InterpolationPostProcessor> postProcessors = createPostProcessors(model, projectDir, config);

View File

@ -57,11 +57,17 @@ public class DefaultModelReader implements ModelReader {
@Override
public Model read(File input, Map<String, ?> options) throws IOException {
Objects.requireNonNull(input, "input cannot be null");
return read(input.toPath(), options);
}
try (InputStream in = Files.newInputStream(input.toPath())) {
Model model = read(in, input.toPath(), options);
@Override
public Model read(Path path, Map<String, ?> options) throws IOException {
Objects.requireNonNull(path, "path cannot be null");
model.setPomFile(input);
try (InputStream in = Files.newInputStream(path)) {
Model model = read(in, path, options);
model.setPomPath(path);
return model;
}

View File

@ -22,6 +22,7 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.nio.file.Path;
import java.util.Map;
import org.apache.maven.model.Model;
@ -59,9 +60,22 @@ public interface ModelReader {
* @return The deserialized model, never {@code null}.
* @throws IOException If the model could not be deserialized.
* @throws ModelParseException If the input format could not be parsed.
* @deprecated Use {@link #read(Path, Map)} instead.
*/
@Deprecated
Model read(File input, Map<String, ?> options) throws IOException, ModelParseException;
/**
* Reads the model from the specified file.
*
* @param input The file to deserialize the model from, must not be {@code null}.
* @param options The options to use for deserialization, may be {@code null} to use the default values.
* @return The deserialized model, never {@code null}.
* @throws IOException If the model could not be deserialized.
* @throws ModelParseException If the input format could not be parsed.
*/
Model read(Path input, Map<String, ?> options) throws IOException, ModelParseException;
/**
* Reads the model from the specified character reader. The reader will be automatically closed before the method
* returns.

View File

@ -22,6 +22,9 @@ import javax.inject.Named;
import javax.inject.Singleton;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* Locates a POM file within a project base directory.
@ -31,20 +34,34 @@ import java.io.File;
@Singleton
public class DefaultModelLocator implements ModelLocator {
@Deprecated
@Override
public File locatePom(File projectDirectory) {
return projectDirectory != null ? projectDirectory : new File(".");
Path path = locatePom(projectDirectory != null ? projectDirectory.toPath() : null);
return path != null ? path.toFile() : null;
}
@Override
public Path locatePom(Path projectDirectory) {
return projectDirectory != null ? projectDirectory : Paths.get(System.getProperty("user.dir"));
}
@Deprecated
@Override
public File locateExistingPom(File project) {
if (project == null || project.isDirectory()) {
Path path = locateExistingPom(project != null ? project.toPath() : null);
return path != null ? path.toFile() : null;
}
@Override
public Path locateExistingPom(Path project) {
if (project == null || Files.isDirectory(project)) {
project = locatePom(project);
}
if (project.isDirectory()) {
File pom = new File(project, "pom.xml");
return pom.isFile() ? pom : null;
} else if (project.isFile()) {
if (Files.isDirectory(project)) {
Path pom = project.resolve("pom.xml");
return Files.isRegularFile(pom) ? pom : null;
} else if (Files.isRegularFile(project)) {
return project;
} else {
return null;

View File

@ -19,6 +19,7 @@
package org.apache.maven.model.locator;
import java.io.File;
import java.nio.file.Path;
/**
* Locates a POM file within a project base directory.
@ -35,16 +36,36 @@ public interface ModelLocator {
* @param projectDirectory The (possibly non-existent) base directory to locate the POM file in, must not be {@code
* null}.
* @return The path to the (possibly non-existent) POM file, never {@code null}.
* @deprecated Use {@link #locatePom(Path)} instead.
*/
@Deprecated
File locatePom(File projectDirectory);
/**
* Returns the file containing the pom or null if a pom can not be found at the given file or in the given directory.
* Locates the POM file within the specified project directory. In case the given project directory does not exist
* or does not contain a POM file, the return value indicates the expected path to the POM file. Subdirectories of
* the project directory will not be considered when locating the POM file. The return value will be an absolute
* path if the project directory is given as an absolute path.
*
* @param projectDirectory The (possibly non-existent) base directory to locate the POM file in, must not be {@code
* null}.
* @return The path to the (possibly non-existent) POM file, never {@code null}.
* @since 4.0.0
*/
default File locateExistingPom(File project) {
if (project == null || project.isDirectory()) {
project = locatePom(project);
}
return project.isFile() ? project : null;
}
Path locatePom(Path projectDirectory);
/**
* Returns the file containing the pom or null if a pom can not be found at the given file or in the given directory.
*
* @deprecated Use {@link #locateExistingPom(Path)} instead.
*/
@Deprecated
File locateExistingPom(File project);
/**
* Returns the file containing the pom or null if a pom can not be found at the given file or in the given directory.
*
* @since 4.0.0
*/
Path locateExistingPom(Path project);
}

View File

@ -23,6 +23,7 @@ import javax.inject.Named;
import javax.inject.Singleton;
import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@ -49,11 +50,20 @@ public class DefaultModelPathTranslator implements ModelPathTranslator {
this.pathTranslator = pathTranslator;
}
@Deprecated
@Override
public void alignToBaseDirectory(org.apache.maven.model.Model modelV3, File basedir, ModelBuildingRequest request) {
if (modelV3 == null || basedir == null) {
return;
}
alignToBaseDirectory(modelV3, basedir.toPath(), request);
}
@Override
public void alignToBaseDirectory(org.apache.maven.model.Model modelV3, Path basedir, ModelBuildingRequest request) {
if (modelV3 == null || basedir == null) {
return;
}
Model model = modelV3.getDelegate();
Build build = model.getBuild();
@ -104,7 +114,7 @@ public class DefaultModelPathTranslator implements ModelPathTranslator {
return newResources;
}
private Resource alignToBaseDirectory(Resource resource, File basedir) {
private Resource alignToBaseDirectory(Resource resource, Path basedir) {
if (resource != null) {
String newDir = alignToBaseDirectory(resource.getDirectory(), basedir);
if (newDir != null) {
@ -114,7 +124,7 @@ public class DefaultModelPathTranslator implements ModelPathTranslator {
return resource;
}
private String alignToBaseDirectory(String path, File basedir) {
private String alignToBaseDirectory(String path, Path basedir) {
String newPath = pathTranslator.alignToBaseDirectory(path, basedir);
return Objects.equals(path, newPath) ? null : newPath;
}

View File

@ -22,6 +22,7 @@ import javax.inject.Named;
import javax.inject.Singleton;
import java.io.File;
import java.nio.file.Path;
/**
* Resolves relative paths against a specific base directory.
@ -33,6 +34,11 @@ public class DefaultPathTranslator implements PathTranslator {
@Override
public String alignToBaseDirectory(String path, File basedir) {
return alignToBaseDirectory(path, basedir != null ? basedir.toPath() : null);
}
@Override
public String alignToBaseDirectory(String path, Path basedir) {
String result = path;
if (path != null && basedir != null) {
@ -47,7 +53,7 @@ public class DefaultPathTranslator implements PathTranslator {
result = file.getAbsolutePath();
} else {
// an ordinary relative path, align with project directory
result = new File(new File(basedir, path).toURI().normalize()).getAbsolutePath();
result = basedir.resolve(path).normalize().toString();
}
}

View File

@ -19,6 +19,7 @@
package org.apache.maven.model.path;
import java.io.File;
import java.nio.file.Path;
import org.apache.maven.model.Model;
import org.apache.maven.model.building.ModelBuildingRequest;
@ -36,6 +37,19 @@ public interface ModelPathTranslator {
* @param model The model whose paths should be resolved, may be {@code null}.
* @param basedir The base directory to resolve relative paths against, may be {@code null}.
* @param request The model building request that holds further settings, must not be {@code null}.
* @deprecated Use {@link #alignToBaseDirectory(Model, Path, ModelBuildingRequest)} instead.
*/
@Deprecated
void alignToBaseDirectory(Model model, File basedir, ModelBuildingRequest request);
/**
* Resolves the well-known paths of the specified model against the given base directory. Paths within plugin
* configuration are not processed.
*
* @param model The model whose paths should be resolved, may be {@code null}.
* @param basedir The base directory to resolve relative paths against, may be {@code null}.
* @param request The model building request that holds further settings, must not be {@code null}.
* @since 4.0.0
*/
void alignToBaseDirectory(Model model, Path basedir, ModelBuildingRequest request);
}

View File

@ -19,6 +19,7 @@
package org.apache.maven.model.path;
import java.io.File;
import java.nio.file.Path;
/**
* Resolves relative paths against a specific base directory.
@ -34,6 +35,20 @@ public interface PathTranslator {
* @param path The path to resolve, may be {@code null}.
* @param basedir The base directory to resolve relative paths against, may be {@code null}.
* @return The resolved path or {@code null} if the input path was {@code null}.
* @deprecated Use {@link #alignToBaseDirectory(String, Path)} instead.
*/
@Deprecated
String alignToBaseDirectory(String path, File basedir);
/**
* Resolves the specified path against the given base directory. The resolved path will be absolute and uses the
* platform-specific file separator if a base directory is given. Otherwise, the input path will be returned
* unaltered.
*
* @param path The path to resolve, may be {@code null}.
* @param basedir The base directory to resolve relative paths against, may be {@code null}.
* @return The resolved path or {@code null} if the input path was {@code null}.
* @since 4.0.0
*/
String alignToBaseDirectory(String path, Path basedir);
}

View File

@ -164,7 +164,7 @@ public abstract class AbstractModelInterpolatorTest {
ModelInterpolator interpolator = createInterpolator();
final SimpleProblemCollector collector = new SimpleProblemCollector();
interpolator.interpolateModel(model, null, createModelBuildingRequest(context), collector);
interpolator.interpolateModel(model, (Path) null, createModelBuildingRequest(context), collector);
assertCollectorState(0, 1, 0, collector);
}
@ -283,7 +283,7 @@ public abstract class AbstractModelInterpolatorTest {
ModelInterpolator interpolator = createInterpolator();
final SimpleProblemCollector collector = new SimpleProblemCollector();
Model out = interpolator.interpolateModel(model, null, createModelBuildingRequest(context), collector);
Model out = interpolator.interpolateModel(model, (Path) null, createModelBuildingRequest(context), collector);
assertProblemFree(collector);
assertEquals(
@ -303,7 +303,7 @@ public abstract class AbstractModelInterpolatorTest {
ModelInterpolator interpolator = createInterpolator();
final SimpleProblemCollector collector = new SimpleProblemCollector();
Model out = interpolator.interpolateModel(model, null, createModelBuildingRequest(context), collector);
Model out = interpolator.interpolateModel(model, (Path) null, createModelBuildingRequest(context), collector);
assertProblemFree(collector);
assertEquals("myBaseUri/temp-repo", (out.getRepositories().get(0)).getUrl());
@ -370,7 +370,8 @@ public abstract class AbstractModelInterpolatorTest {
final SimpleProblemCollector collector = new SimpleProblemCollector();
IllegalStateException e = assertThrows(
IllegalStateException.class,
() -> interpolator.interpolateModel(model, null, createModelBuildingRequest(context), collector));
() -> interpolator.interpolateModel(
model, (Path) null, createModelBuildingRequest(context), collector));
assertEquals(RootLocator.UNABLE_TO_FIND_ROOT_PROJECT_MESSAGE, e.getMessage());
}
@ -440,7 +441,7 @@ public abstract class AbstractModelInterpolatorTest {
ModelInterpolator interpolator = createInterpolator();
final SimpleProblemCollector collector = new SimpleProblemCollector();
Model out = interpolator.interpolateModel(model, null, createModelBuildingRequest(context), collector);
Model out = interpolator.interpolateModel(model, (Path) null, createModelBuildingRequest(context), collector);
assertCollectorState(0, 0, 0, collector);
List<Resource> outResources = out.getBuild().getResources();
@ -483,7 +484,7 @@ public abstract class AbstractModelInterpolatorTest {
SimpleProblemCollector collector = new SimpleProblemCollector();
ModelInterpolator interpolator = createInterpolator();
interpolator.interpolateModel(model, null, request, collector);
interpolator.interpolateModel(model, (Path) null, request, collector);
assertCollectorState(0, 2, 0, collector);
assertTrue(collector.getErrors().get(0).contains("Detected the following recursive expression cycle"));
@ -499,7 +500,7 @@ public abstract class AbstractModelInterpolatorTest {
SimpleProblemCollector collector = new SimpleProblemCollector();
ModelInterpolator interpolator = createInterpolator();
interpolator.interpolateModel(model, null, request, collector);
interpolator.interpolateModel(model, (Path) null, request, collector);
assertCollectorState(0, 1, 0, collector);
assertEquals(
@ -522,7 +523,7 @@ public abstract class AbstractModelInterpolatorTest {
SimpleProblemCollector collector = new SimpleProblemCollector();
Model out = interpolator.interpolateModel(
model,
null,
(Path) null,
createModelBuildingRequest(context).setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MAVEN_4_0),
collector);
@ -545,7 +546,7 @@ public abstract class AbstractModelInterpolatorTest {
SimpleProblemCollector collector = new SimpleProblemCollector();
Model out = interpolator.interpolateModel(
model,
null,
(Path) null,
createModelBuildingRequest(context).setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MAVEN_3_1),
collector);

View File

@ -25,19 +25,21 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.maven.api.Language;
import org.apache.maven.model.DependencyManagement;
import org.apache.maven.model.DistributionManagement;
import org.apache.maven.model.License;
import org.apache.maven.model.Model;
import org.apache.maven.model.Prerequisites;
import org.apache.maven.model.Repository;
import org.apache.maven.repository.internal.artifact.MavenArtifactProperties;
import org.apache.maven.repository.internal.type.DefaultType;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.ArtifactProperties;
import org.eclipse.aether.artifact.ArtifactType;
import org.eclipse.aether.artifact.ArtifactTypeRegistry;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.artifact.DefaultArtifactType;
import org.eclipse.aether.graph.Dependency;
import org.eclipse.aether.graph.Exclusion;
import org.eclipse.aether.resolution.ArtifactDescriptorResult;
@ -92,7 +94,8 @@ public class ArtifactDescriptorReaderDelegate {
private Dependency convert(org.apache.maven.model.Dependency dependency, ArtifactTypeRegistry stereotypes) {
ArtifactType stereotype = stereotypes.get(dependency.getType());
if (stereotype == null) {
stereotype = new DefaultArtifactType(dependency.getType());
// TODO: this here is fishy
stereotype = new DefaultType(dependency.getType(), Language.NONE, "", null, false, false);
}
boolean system = dependency.getSystemPath() != null
@ -100,7 +103,7 @@ public class ArtifactDescriptorReaderDelegate {
Map<String, String> props = null;
if (system) {
props = Collections.singletonMap(ArtifactProperties.LOCAL_PATH, dependency.getSystemPath());
props = Collections.singletonMap(MavenArtifactProperties.LOCAL_PATH, dependency.getSystemPath());
}
Artifact artifact = new DefaultArtifact(

View File

@ -211,10 +211,10 @@ public class DefaultArtifactDescriptorReader implements ArtifactDescriptorReader
remoteRepositoryManager,
request.getRepositories()));
if (resolveResult.getRepository() instanceof WorkspaceRepository) {
modelRequest.setPomFile(pomArtifact.getFile());
modelRequest.setPomPath(pomArtifact.getPath());
} else {
modelRequest.setModelSource(new ArtifactModelSource(
pomArtifact.getFile(),
pomArtifact.getPath(),
pomArtifact.getGroupId(),
pomArtifact.getArtifactId(),
pomArtifact.getVersion()));

View File

@ -159,7 +159,7 @@ class DefaultModelResolver implements ModelResolver {
throw new UnresolvableModelException(e.getMessage(), groupId, artifactId, version, e);
}
return new ArtifactModelSource(pomArtifact.getFile(), groupId, artifactId, version);
return new ArtifactModelSource(pomArtifact.getPath(), groupId, artifactId, version);
}
@Override

View File

@ -200,9 +200,8 @@ public class DefaultVersionRangeResolver implements VersionRangeResolver {
try (SyncContext syncContext = syncContextFactory.newInstance(session, true)) {
syncContext.acquire(null, Collections.singleton(metadata));
if (metadata.getFile() != null && metadata.getFile().exists()) {
try (InputStream in =
Files.newInputStream(metadata.getFile().toPath())) {
if (metadata.getPath() != null && Files.exists(metadata.getPath())) {
try (InputStream in = Files.newInputStream(metadata.getPath())) {
versioning = new Versioning(
new MetadataStaxReader().read(in, false).getVersioning());
}

View File

@ -22,10 +22,10 @@ import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
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.Collections;
import java.util.HashMap;
@ -243,9 +243,8 @@ public class DefaultVersionResolver implements VersionResolver {
try (SyncContext syncContext = syncContextFactory.newInstance(session, true)) {
syncContext.acquire(null, Collections.singleton(metadata));
if (metadata.getFile() != null && metadata.getFile().exists()) {
try (InputStream in =
Files.newInputStream(metadata.getFile().toPath())) {
if (metadata.getPath() != null && Files.exists(metadata.getPath())) {
try (InputStream in = Files.newInputStream(metadata.getPath())) {
versioning = new Versioning(
new MetadataStaxReader().read(in, false).getVersioning());
@ -422,7 +421,7 @@ public class DefaultVersionResolver implements VersionResolver {
private final String context;
private final File localRepo;
private final Path localRepo;
private final WorkspaceRepository workspace;
@ -437,7 +436,7 @@ public class DefaultVersionResolver implements VersionResolver {
classifier = artifact.getClassifier();
extension = artifact.getExtension();
version = artifact.getVersion();
localRepo = session.getLocalRepository().getBasedir();
localRepo = session.getLocalRepository().getBasePath();
WorkspaceReader reader = session.getWorkspaceReader();
workspace = (reader != null) ? reader.getRepository() : null;
repositories = new ArrayList<>(request.getRepositories().size());

View File

@ -19,6 +19,7 @@
package org.apache.maven.repository.internal;
import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
@ -39,11 +40,11 @@ final class LocalSnapshotMetadata extends MavenMetadata {
private final Collection<Artifact> artifacts = new ArrayList<>();
LocalSnapshotMetadata(Artifact artifact, Date timestamp) {
super(createMetadata(artifact), null, timestamp);
super(createMetadata(artifact), (Path) null, timestamp);
}
LocalSnapshotMetadata(Metadata metadata, File file, Date timestamp) {
super(metadata, file, timestamp);
LocalSnapshotMetadata(Metadata metadata, Path path, Date timestamp) {
super(metadata, path, timestamp);
}
private static Metadata createMetadata(Artifact artifact) {
@ -65,9 +66,15 @@ final class LocalSnapshotMetadata extends MavenMetadata {
artifacts.add(artifact);
}
@Deprecated
@Override
public MavenMetadata setFile(File file) {
return new LocalSnapshotMetadata(metadata, file, timestamp);
return new LocalSnapshotMetadata(metadata, file.toPath(), timestamp);
}
@Override
public MavenMetadata setPath(Path path) {
return new LocalSnapshotMetadata(metadata, path, timestamp);
}
public Object getKey() {

View File

@ -38,7 +38,7 @@ import org.eclipse.aether.util.ConfigUtils;
*/
class LocalSnapshotMetadataGenerator implements MetadataGenerator {
private Map<Object, LocalSnapshotMetadata> snapshots;
private final Map<Object, LocalSnapshotMetadata> snapshots;
private final Date timestamp;

View File

@ -25,6 +25,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Date;
import java.util.Map;
@ -44,15 +45,20 @@ abstract class MavenMetadata extends AbstractMetadata implements MergeableMetada
protected Metadata metadata;
private final File file;
private final Path path;
protected final Date timestamp;
private boolean merged;
@Deprecated
protected MavenMetadata(Metadata metadata, File file, Date timestamp) {
this(metadata, file != null ? file.toPath() : null, timestamp);
}
protected MavenMetadata(Metadata metadata, Path path, Date timestamp) {
this.metadata = metadata;
this.file = file;
this.path = path;
this.timestamp = timestamp;
}
@ -61,13 +67,23 @@ abstract class MavenMetadata extends AbstractMetadata implements MergeableMetada
return MAVEN_METADATA_XML;
}
@Deprecated
@Override
public File getFile() {
return file;
return path != null ? path.toFile() : null;
}
@Override
public Path getPath() {
return path;
}
public void merge(File existing, File result) throws RepositoryException {
merge(existing != null ? existing.toPath() : null, result != null ? result.toPath() : null);
}
@Override
public void merge(Path existing, Path result) throws RepositoryException {
Metadata recessive = read(existing);
merge(recessive);
@ -84,24 +100,26 @@ abstract class MavenMetadata extends AbstractMetadata implements MergeableMetada
protected abstract void merge(Metadata recessive);
static Metadata read(File metadataFile) throws RepositoryException {
if (metadataFile.length() <= 0) {
static Metadata read(Path metadataPath) throws RepositoryException {
if (!Files.exists(metadataPath)) {
return new Metadata();
}
try (InputStream input = Files.newInputStream(metadataFile.toPath())) {
try (InputStream input = Files.newInputStream(metadataPath)) {
return new Metadata(new MetadataStaxReader().read(input, false));
} catch (IOException | XMLStreamException e) {
throw new RepositoryException("Could not parse metadata " + metadataFile + ": " + e.getMessage(), e);
throw new RepositoryException("Could not parse metadata " + metadataPath + ": " + e.getMessage(), e);
}
}
private void write(File metadataFile, Metadata metadata) throws RepositoryException {
metadataFile.getParentFile().mkdirs();
try (OutputStream output = Files.newOutputStream(metadataFile.toPath())) {
new MetadataStaxWriter().write(output, metadata.getDelegate());
private void write(Path metadataPath, Metadata metadata) throws RepositoryException {
try {
Files.createDirectories(metadataPath.getParent());
try (OutputStream output = Files.newOutputStream(metadataPath)) {
new MetadataStaxWriter().write(output, metadata.getDelegate());
}
} catch (IOException | XMLStreamException e) {
throw new RepositoryException("Could not write metadata " + metadataFile + ": " + e.getMessage(), e);
throw new RepositoryException("Could not write metadata " + metadataPath + ": " + e.getMessage(), e);
}
}

View File

@ -24,10 +24,12 @@ import org.apache.maven.repository.internal.artifact.FatArtifactTraverser;
import org.apache.maven.repository.internal.scopes.MavenDependencyContextRefiner;
import org.apache.maven.repository.internal.scopes.MavenScopeDeriver;
import org.apache.maven.repository.internal.scopes.MavenScopeSelector;
import org.apache.maven.repository.internal.scopes.MavenSystemScopeHandler;
import org.apache.maven.repository.internal.type.DefaultTypeProvider;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession.CloseableSession;
import org.eclipse.aether.RepositorySystemSession.SessionBuilder;
import org.eclipse.aether.SystemScopeHandler;
import org.eclipse.aether.artifact.ArtifactTypeRegistry;
import org.eclipse.aether.collection.DependencyGraphTransformer;
import org.eclipse.aether.collection.DependencyManager;
@ -76,8 +78,12 @@ public class MavenSessionBuilderSupplier implements Supplier<SessionBuilder> {
return new FatArtifactTraverser();
}
protected SystemScopeHandler getSystemScopeHandler() {
return new MavenSystemScopeHandler();
}
protected DependencyManager getDependencyManager() {
return new ClassicDependencyManager(true); // same default as in Maven4
return new ClassicDependencyManager(true, getSystemScopeHandler()); // same default as in Maven4
}
protected DependencySelector getDependencySelector() {

View File

@ -18,7 +18,7 @@
*/
package org.apache.maven.repository.internal;
import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
@ -33,8 +33,8 @@ abstract class MavenSnapshotMetadata extends MavenMetadata {
protected final Collection<Artifact> artifacts = new ArrayList<>();
protected MavenSnapshotMetadata(Metadata metadata, File file, Date timestamp) {
super(metadata, file, timestamp);
protected MavenSnapshotMetadata(Metadata metadata, Path path, Date timestamp) {
super(metadata, path, timestamp);
}
protected static Metadata createRepositoryMetadata(Artifact artifact) {

View File

@ -19,6 +19,7 @@
package org.apache.maven.repository.internal;
import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
@ -51,12 +52,12 @@ final class PluginsMetadata extends MavenMetadata {
private final PluginInfo pluginInfo;
PluginsMetadata(PluginInfo pluginInfo, Date timestamp) {
super(createRepositoryMetadata(pluginInfo), null, timestamp);
super(createRepositoryMetadata(pluginInfo), (Path) null, timestamp);
this.pluginInfo = pluginInfo;
}
PluginsMetadata(PluginInfo pluginInfo, File file, Date timestamp) {
super(createRepositoryMetadata(pluginInfo), file, timestamp);
PluginsMetadata(PluginInfo pluginInfo, Path path, Date timestamp) {
super(createRepositoryMetadata(pluginInfo), path, timestamp);
this.pluginInfo = pluginInfo;
}
@ -82,9 +83,15 @@ final class PluginsMetadata extends MavenMetadata {
}
}
@Deprecated
@Override
public MavenMetadata setFile(File file) {
return new PluginsMetadata(pluginInfo, file, timestamp);
return new PluginsMetadata(pluginInfo, file.toPath(), timestamp);
}
@Override
public MavenMetadata setPath(Path path) {
return new PluginsMetadata(pluginInfo, path, timestamp);
}
@Override

View File

@ -115,8 +115,8 @@ class PluginsMetadataGenerator implements MetadataGenerator {
if (artifact != null
&& "jar".equals(artifact.getExtension())
&& "".equals(artifact.getClassifier())
&& artifact.getFile() != null) {
Path artifactPath = artifact.getFile().toPath();
&& artifact.getPath() != null) {
Path artifactPath = artifact.getPath();
if (Files.isRegularFile(artifactPath)) {
try (JarFile artifactJar = new JarFile(artifactPath.toFile(), false)) {
ZipEntry pluginDescriptorEntry = artifactJar.getEntry(PLUGIN_DESCRIPTOR_LOCATION);

View File

@ -19,6 +19,7 @@
package org.apache.maven.repository.internal;
import java.io.File;
import java.nio.file.Path;
import java.util.Map;
import java.util.Objects;
@ -115,6 +116,7 @@ public final class RelocatedArtifact extends AbstractArtifact {
return new RelocatedArtifact(artifact, groupId, artifactId, classifier, extension, version, message);
}
@Deprecated
@Override
public Artifact setFile(File file) {
File current = getFile();
@ -125,6 +127,16 @@ public final class RelocatedArtifact extends AbstractArtifact {
artifact.setFile(file), groupId, artifactId, classifier, extension, version, message);
}
@Override
public Artifact setPath(Path path) {
Path current = getPath();
if (Objects.equals(current, path)) {
return this;
}
return new RelocatedArtifact(
artifact.setPath(path), groupId, artifactId, classifier, extension, version, message);
}
@Override
public Artifact setProperties(Map<String, String> properties) {
Map<String, String> current = getProperties();
@ -135,11 +147,17 @@ public final class RelocatedArtifact extends AbstractArtifact {
artifact.setProperties(properties), groupId, artifactId, classifier, extension, version, message);
}
@Deprecated
@Override
public File getFile() {
return artifact.getFile();
}
@Override
public Path getPath() {
return artifact.getPath();
}
@Override
public String getProperty(String key, String defaultValue) {
return artifact.getProperty(key, defaultValue);

View File

@ -19,6 +19,7 @@
package org.apache.maven.repository.internal;
import java.io.File;
import java.nio.file.Path;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
@ -51,14 +52,20 @@ final class RemoteSnapshotMetadata extends MavenSnapshotMetadata {
this.buildNumber = buildNumber;
}
private RemoteSnapshotMetadata(Metadata metadata, File file, Date timestamp, Integer buildNumber) {
super(metadata, file, timestamp);
private RemoteSnapshotMetadata(Metadata metadata, Path path, Date timestamp, Integer buildNumber) {
super(metadata, path, timestamp);
this.buildNumber = buildNumber;
}
@Deprecated
@Override
public MavenMetadata setFile(File file) {
return new RemoteSnapshotMetadata(metadata, file, timestamp, buildNumber);
return new RemoteSnapshotMetadata(metadata, file.toPath(), timestamp, buildNumber);
}
@Override
public MavenMetadata setPath(Path path) {
return new RemoteSnapshotMetadata(metadata, path, timestamp, buildNumber);
}
public String getExpandedVersion(Artifact artifact) {

View File

@ -19,6 +19,7 @@
package org.apache.maven.repository.internal;
import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
@ -37,12 +38,12 @@ final class VersionsMetadata extends MavenMetadata {
private final Artifact artifact;
VersionsMetadata(Artifact artifact, Date timestamp) {
super(createRepositoryMetadata(artifact), null, timestamp);
super(createRepositoryMetadata(artifact), (Path) null, timestamp);
this.artifact = artifact;
}
VersionsMetadata(Artifact artifact, File file, Date timestamp) {
super(createRepositoryMetadata(artifact), file, timestamp);
VersionsMetadata(Artifact artifact, Path path, Date timestamp) {
super(createRepositoryMetadata(artifact), path, timestamp);
this.artifact = artifact;
}
@ -93,9 +94,15 @@ final class VersionsMetadata extends MavenMetadata {
return artifact.getGroupId() + ':' + artifact.getArtifactId();
}
@Deprecated
@Override
public MavenMetadata setFile(File file) {
return new VersionsMetadata(artifact, file, timestamp);
return new VersionsMetadata(artifact, file.toPath(), timestamp);
}
@Override
public MavenMetadata setPath(Path path) {
return new VersionsMetadata(artifact, path, timestamp);
}
@Override

View File

@ -42,6 +42,14 @@ public final class MavenArtifactProperties {
*/
public static final String CONSTITUTES_BUILD_PATH = "constitutesBuildPath";
/**
* The (expected) path to the artifact on the local filesystem. An artifact which has this property set is assumed
* to be not present in any regular repository and likewise has no artifact descriptor. Artifact resolution will
* verify the path and resolve the artifact if the path actually denotes an existing file. If the path isn't valid,
* resolution will fail and no attempts to search local/remote repositories are made.
*/
public static final String LOCAL_PATH = "localPath";
private MavenArtifactProperties() {
// hide constructor
}

View File

@ -0,0 +1,51 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.repository.internal.scopes;
import java.util.Map;
import org.apache.maven.repository.internal.artifact.MavenArtifactProperties;
import org.eclipse.aether.SystemScopeHandler;
import org.eclipse.aether.artifact.Artifact;
/**
* A system scope handler.
*
* @since 4.0.0
*/
public final class MavenSystemScopeHandler implements SystemScopeHandler {
@Override
public boolean isSystemScope(String scope) {
return MavenDependencyScopes.SYSTEM.equals(scope);
}
@Override
public String getSystemPath(Artifact artifact) {
return artifact.getProperty(MavenArtifactProperties.LOCAL_PATH, null);
}
@Override
public void setSystemPath(Map<String, String> properties, String systemPath) {
if (systemPath == null) {
properties.remove(MavenArtifactProperties.LOCAL_PATH);
} else {
properties.put(MavenArtifactProperties.LOCAL_PATH, systemPath);
}
}
}

View File

@ -177,7 +177,7 @@ under the License.
<plexusInterpolationVersion>1.26</plexusInterpolationVersion>
<plexusTestingVersion>1.0.0</plexusTestingVersion>
<plexusXmlVersion>4.0.1</plexusXmlVersion>
<resolverVersion>2.0.0-alpha-7</resolverVersion>
<resolverVersion>2.0.0-alpha-8</resolverVersion>
<securityDispatcherVersion>2.0</securityDispatcherVersion>
<sisuVersion>0.9.0.M2</sisuVersion>
<slf4jVersion>2.0.11</slf4jVersion>