From cf438ca624737c8aab82fcbb8798cf17e98a0837 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Mon, 26 Feb 2024 18:04:44 +0100 Subject: [PATCH] [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 --- api/maven-api-model/src/main/mdo/maven.mdo | 27 ++++++++++ .../apache/maven/building/DefaultProblem.java | 4 +- .../building/DefaultProblemCollector.java | 2 +- .../org/apache/maven/building/FileSource.java | 39 +++++++++++--- .../org/apache/maven/building/UrlSource.java | 2 +- .../apache/maven/building/FileSourceTest.java | 4 +- .../repository/metadata/MetadataBridge.java | 6 +++ .../project/ClasspathArtifactResolver.java | 3 +- .../org/apache/maven/RepositoryUtils.java | 9 ++-- ...DefaultRepositorySystemSessionFactory.java | 26 ++++++---- .../apache/maven/internal/impl/TestApi.java | 2 +- .../model/building/ArtifactModelSource.java | 10 ++++ .../model/building/DefaultModelBuilder.java | 10 ++-- .../building/DefaultModelBuildingRequest.java | 24 ++++++--- .../model/building/DefaultModelProcessor.java | 29 +++++++---- .../DefaultTransformerContextBuilder.java | 20 ++++---- .../maven/model/building/FileModelSource.java | 25 ++++++--- .../building/FilterModelBuildingRequest.java | 14 ++++- .../model/building/ModelBuildingRequest.java | 29 ++++++++++- .../model/building/ModelProblemUtils.java | 14 ++--- .../AbstractStringBasedModelInterpolator.java | 36 ++++++------- .../interpolation/ModelInterpolator.java | 28 ++++++++++ .../PathTranslatingPostProcessor.java | 6 +-- .../StringVisitorModelInterpolator.java | 8 +++ .../maven/model/io/DefaultModelReader.java | 12 +++-- .../apache/maven/model/io/ModelReader.java | 14 +++++ .../model/locator/DefaultModelLocator.java | 29 ++++++++--- .../maven/model/locator/ModelLocator.java | 33 +++++++++--- .../path/DefaultModelPathTranslator.java | 14 ++++- .../model/path/DefaultPathTranslator.java | 8 ++- .../maven/model/path/ModelPathTranslator.java | 14 +++++ .../maven/model/path/PathTranslator.java | 15 ++++++ .../AbstractModelInterpolatorTest.java | 19 +++---- .../ArtifactDescriptorReaderDelegate.java | 9 ++-- .../DefaultArtifactDescriptorReader.java | 4 +- .../internal/DefaultModelResolver.java | 2 +- .../internal/DefaultVersionRangeResolver.java | 5 +- .../internal/DefaultVersionResolver.java | 11 ++-- .../internal/LocalSnapshotMetadata.java | 15 ++++-- .../LocalSnapshotMetadataGenerator.java | 2 +- .../repository/internal/MavenMetadata.java | 42 ++++++++++----- .../internal/MavenSessionBuilderSupplier.java | 8 ++- .../internal/MavenSnapshotMetadata.java | 6 +-- .../repository/internal/PluginsMetadata.java | 15 ++++-- .../internal/PluginsMetadataGenerator.java | 4 +- .../internal/RelocatedArtifact.java | 18 +++++++ .../internal/RemoteSnapshotMetadata.java | 13 +++-- .../repository/internal/VersionsMetadata.java | 15 ++++-- .../artifact/MavenArtifactProperties.java | 8 +++ .../scopes/MavenSystemScopeHandler.java | 51 +++++++++++++++++++ pom.xml | 2 +- 51 files changed, 592 insertions(+), 173 deletions(-) create mode 100644 maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/scopes/MavenSystemScopeHandler.java diff --git a/api/maven-api-model/src/main/mdo/maven.mdo b/api/maven-api-model/src/main/mdo/maven.mdo index 43852943713c..319341d31d4f 100644 --- a/api/maven-api-model/src/main/mdo/maven.mdo +++ b/api/maven-api-model/src/main/mdo/maven.mdo @@ -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} */ diff --git a/maven-builder-support/src/main/java/org/apache/maven/building/DefaultProblem.java b/maven-builder-support/src/main/java/org/apache/maven/building/DefaultProblem.java index 8c628a4e8a51..4710c8e4cb15 100644 --- a/maven-builder-support/src/main/java/org/apache/maven/building/DefaultProblem.java +++ b/maven-builder-support/src/main/java/org/apache/maven/building/DefaultProblem.java @@ -75,7 +75,7 @@ public int getColumnNumber() { 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 @@ public Exception getException() { public String getMessage() { String msg; - if (message != null && message.length() > 0) { + if (message != null && !message.isEmpty()) { msg = message; } else { msg = exception.getMessage(); diff --git a/maven-builder-support/src/main/java/org/apache/maven/building/DefaultProblemCollector.java b/maven-builder-support/src/main/java/org/apache/maven/building/DefaultProblemCollector.java index 3274516f203c..181c58e6606d 100644 --- a/maven-builder-support/src/main/java/org/apache/maven/building/DefaultProblemCollector.java +++ b/maven-builder-support/src/main/java/org/apache/maven/building/DefaultProblemCollector.java @@ -27,7 +27,7 @@ */ class DefaultProblemCollector implements ProblemCollector { - private List problems; + private final List problems; private String source; diff --git a/maven-builder-support/src/main/java/org/apache/maven/building/FileSource.java b/maven-builder-support/src/main/java/org/apache/maven/building/FileSource.java index d75f837f1823..bc6aa6381580 100644 --- a/maven-builder-support/src/main/java/org/apache/maven/building/FileSource.java +++ b/maven-builder-support/src/main/java/org/apache/maven/building/FileSource.java @@ -22,6 +22,7 @@ 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 @@ * */ 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 boolean equals(Object obj) { } FileSource other = (FileSource) obj; - return this.file.equals(other.file); + return this.path.equals(other.path); } } diff --git a/maven-builder-support/src/main/java/org/apache/maven/building/UrlSource.java b/maven-builder-support/src/main/java/org/apache/maven/building/UrlSource.java index 7cd2651bb045..0a2b3dc6c342 100644 --- a/maven-builder-support/src/main/java/org/apache/maven/building/UrlSource.java +++ b/maven-builder-support/src/main/java/org/apache/maven/building/UrlSource.java @@ -87,6 +87,6 @@ public boolean equals(Object obj) { } UrlSource other = (UrlSource) obj; - return this.url.equals(other.url); + return Objects.equals(url.toExternalForm(), other.url.toExternalForm()); } } diff --git a/maven-builder-support/src/test/java/org/apache/maven/building/FileSourceTest.java b/maven-builder-support/src/test/java/org/apache/maven/building/FileSourceTest.java index cfdc9d7e57ec..33dd09373548 100644 --- a/maven-builder-support/src/test/java/org/apache/maven/building/FileSourceTest.java +++ b/maven-builder-support/src/test/java/org/apache/maven/building/FileSourceTest.java @@ -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()); } diff --git a/maven-compat/src/main/java/org/apache/maven/artifact/repository/metadata/MetadataBridge.java b/maven-compat/src/main/java/org/apache/maven/artifact/repository/metadata/MetadataBridge.java index bc60117a2ef8..5199cc205188 100644 --- a/maven-compat/src/main/java/org/apache/maven/artifact/repository/metadata/MetadataBridge.java +++ b/maven-compat/src/main/java/org/apache/maven/artifact/repository/metadata/MetadataBridge.java @@ -20,6 +20,7 @@ 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 MetadataBridge setFile(File file) { return this; } + @Override + public Path getPath() { + return null; + } + public Nature getNature() { if (metadata instanceof RepositoryMetadata) { switch (((RepositoryMetadata) metadata).getNature()) { diff --git a/maven-compat/src/test/java/org/apache/maven/project/ClasspathArtifactResolver.java b/maven-compat/src/test/java/org/apache/maven/project/ClasspathArtifactResolver.java index 3051af72060c..6b06fdfd0782 100644 --- a/maven-compat/src/test/java/org/apache/maven/project/ClasspathArtifactResolver.java +++ b/maven-compat/src/test/java/org/apache/maven/project/ClasspathArtifactResolver.java @@ -31,6 +31,7 @@ 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 List resolveArtifacts( 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); } } diff --git a/maven-core/src/main/java/org/apache/maven/RepositoryUtils.java b/maven-core/src/main/java/org/apache/maven/RepositoryUtils.java index c0e5fd95913c..e94554df776f 100644 --- a/maven-core/src/main/java/org/apache/maven/RepositoryUtils.java +++ b/maven-core/src/main/java/org/apache/maven/RepositoryUtils.java @@ -34,6 +34,7 @@ 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 static Artifact toArtifact(org.apache.maven.artifact.Artifact artifact) { Map 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 static ArtifactHandler newHandler(Artifact artifact) { 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 static Dependency toDependency( Map props = null; if (system) { - props = Collections.singletonMap(ArtifactProperties.LOCAL_PATH, dependency.getSystemPath()); + props = Collections.singletonMap(MavenArtifactProperties.LOCAL_PATH, dependency.getSystemPath()); } Artifact artifact = new DefaultArtifact( diff --git a/maven-core/src/main/java/org/apache/maven/internal/aether/DefaultRepositorySystemSessionFactory.java b/maven-core/src/main/java/org/apache/maven/internal/aether/DefaultRepositorySystemSessionFactory.java index 210f876babc2..d587541eec6c 100644 --- a/maven-core/src/main/java/org/apache/maven/internal/aether/DefaultRepositorySystemSessionFactory.java +++ b/maven-core/src/main/java/org/apache/maven/internal/aether/DefaultRepositorySystemSessionFactory.java @@ -22,8 +22,15 @@ 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.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 @@ public SessionBuilder newRepositorySessionBuilder(MavenExecutionRequest request) 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 paths = new ArrayList<>(); - paths.add(new File(request.getLocalRepository().getBasedir())); + ArrayList 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); diff --git a/maven-core/src/test/java/org/apache/maven/internal/impl/TestApi.java b/maven-core/src/test/java/org/apache/maven/internal/impl/TestApi.java index 18ce020307f5..3ebe1deb9823 100644 --- a/maven-core/src/test/java/org/apache/maven/internal/impl/TestApi.java +++ b/maven-core/src/test/java/org/apache/maven/internal/impl/TestApi.java @@ -103,7 +103,7 @@ void setup() { // 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(); diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/building/ArtifactModelSource.java b/maven-model-builder/src/main/java/org/apache/maven/model/building/ArtifactModelSource.java index 065756421f37..cafec16b049c 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/building/ArtifactModelSource.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/building/ArtifactModelSource.java @@ -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 ArtifactModelSource(File file, String groupId, String artifactId, String 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; } diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java index 7d832654a12e..caeae60882ad 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java @@ -982,7 +982,7 @@ private ModelBuildingResult build( 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 @@ private Model interpolateModel(Model model, ModelBuildingRequest request, ModelP Map 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 @@ private Model interpolateModel(Model model, ModelBuildingRequest request, ModelP 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 @@ private ModelData readParentLocally( if (candidateModel == null) { return null; } - candidateSource = new FileModelSource(candidateModel.getPomFile()); + candidateSource = new FileModelSource(candidateModel.getPomPath()); } // @@ -1604,7 +1604,7 @@ private ModelData readParentExternally( 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 { diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuildingRequest.java b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuildingRequest.java index e19a72caef0b..951fa74b9b6a 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuildingRequest.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuildingRequest.java @@ -37,7 +37,7 @@ public class DefaultModelBuildingRequest implements ModelBuildingRequest { private Model fileModel; - private File pomFile; + private Path pomPath; private ModelSource modelSource; @@ -84,7 +84,7 @@ public DefaultModelBuildingRequest() {} * @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 DefaultModelBuildingRequest(ModelBuildingRequest request) { setRootDirectory(request.getRootDirectory()); } + @Deprecated @Override public File getPomFile() { - return pomFile; + return pomPath != null ? pomPath.toFile() : null; } + @Override + public Path getPomPath() { + return pomPath; + } + + @Deprecated @Override public DefaultModelBuildingRequest setPomFile(File pomFile) { - this.pomFile = (pomFile != null) ? pomFile.getAbsoluteFile() : null; + 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; } diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelProcessor.java b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelProcessor.java index b3c459c0d03a..3675b549ec25 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelProcessor.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelProcessor.java @@ -85,11 +85,13 @@ public DefaultModelProcessor( 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 Path locatePom(Path projectDirectory) { .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 @@ protected org.apache.maven.api.model.Model read( Path pomFile, InputStream input, Reader reader, Map 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 @@ protected org.apache.maven.api.model.Model read( private org.apache.maven.api.model.Model readXmlModel( Path pomFile, InputStream input, Reader reader, Map 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 @@ private org.apache.maven.api.model.Model readXmlModel( } } + @Deprecated @Override public org.apache.maven.model.Model read(File file, Map 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 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); } diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultTransformerContextBuilder.java b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultTransformerContextBuilder.java index 15799e1f739e..da0da3b78a53 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultTransformerContextBuilder.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultTransformerContextBuilder.java @@ -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 @@ public Model getRawModel(Path from, String gId, String aId) { 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 @@ private Model findRawModel(Path from, String groupId, String artifactId) { 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 @@ private void doLoadFullReactor() { if (rootDirectory == null) { return; } - List toLoad = new ArrayList<>(); - File root = defaultModelBuilder.getModelProcessor().locateExistingPom(rootDirectory.toFile()); + List 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 @@ private Model findRawModel(Path from, Path p) { 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); diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/building/FileModelSource.java b/maven-model-builder/src/main/java/org/apache/maven/model/building/FileModelSource.java index 5bbf0eb32f57..856a10dd6a98 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/building/FileModelSource.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/building/FileModelSource.java @@ -20,6 +20,7 @@ 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 File getPomFile() { 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 ModelSource3 getRelatedSource(ModelLocator locator, String relPath) { @Override public URI getLocationURI() { - return getFile().toURI(); + return getPath().toUri(); } @Override @@ -84,11 +97,11 @@ public boolean equals(Object obj) { 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(); } } diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/building/FilterModelBuildingRequest.java b/maven-model-builder/src/main/java/org/apache/maven/model/building/FilterModelBuildingRequest.java index 2f6acaca5883..b895292b409d 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/building/FilterModelBuildingRequest.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/building/FilterModelBuildingRequest.java @@ -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) { + 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; } diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/building/ModelBuildingRequest.java b/maven-model-builder/src/main/java/org/apache/maven/model/building/ModelBuildingRequest.java index fb08761f9d88..ffa59deb6742 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/building/ModelBuildingRequest.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/building/ModelBuildingRequest.java @@ -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. * diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/building/ModelProblemUtils.java b/maven-model-builder/src/main/java/org/apache/maven/model/building/ModelProblemUtils.java index f11f2856b042..3d373ebff183 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/building/ModelProblemUtils.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/building/ModelProblemUtils.java @@ -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 @@ static String toSourceHint(Model model) { 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 @@ static String toPath(Model model) { 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(); } } diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/AbstractStringBasedModelInterpolator.java b/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/AbstractStringBasedModelInterpolator.java index 97c8984c6094..c2e7e25bd469 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/AbstractStringBasedModelInterpolator.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/AbstractStringBasedModelInterpolator.java @@ -20,7 +20,6 @@ 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 AbstractStringBasedModelInterpolator( @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 @@ protected List getProjectPrefixes(ModelBuildingRequest config) { protected List createValueSources( final Model model, - final File projectDir, + final Path projectDir, final ModelBuildingRequest config, ModelProblemCollector problems) { Map modelProperties = model.getProperties(); @@ -139,9 +148,9 @@ protected List createValueSources( @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 Object getValue(String expression) { @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 Object getValue(String expression) { @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 Object getValue(String expression) { } protected List createPostProcessors( - final Model model, final File projectDir, final ModelBuildingRequest config) { + final Model model, final Path projectDir, final ModelBuildingRequest config) { List processors = new ArrayList<>(2); if (projectDir != null) { processors.add(new PathTranslatingPostProcessor( diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/ModelInterpolator.java b/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/ModelInterpolator.java index e150fdf61b49..3745ee849d9e 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/ModelInterpolator.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/ModelInterpolator.java @@ -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); } diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/PathTranslatingPostProcessor.java b/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/PathTranslatingPostProcessor.java index 674cdc6513b6..df047883bb62 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/PathTranslatingPostProcessor.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/PathTranslatingPostProcessor.java @@ -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 @@ class PathTranslatingPostProcessor implements InterpolationPostProcessor { private final Collection unprefixedPathKeys; - private final File projectDir; + private final Path projectDir; private final PathTranslator pathTranslator; private final List expressionPrefixes; PathTranslatingPostProcessor( List expressionPrefixes, Collection unprefixedPathKeys, - File projectDir, + Path projectDir, PathTranslator pathTranslator) { this.expressionPrefixes = expressionPrefixes; this.unprefixedPathKeys = unprefixedPathKeys; diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/StringVisitorModelInterpolator.java b/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/StringVisitorModelInterpolator.java index ae1d61773f46..e0b7f5dad683 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/StringVisitorModelInterpolator.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/StringVisitorModelInterpolator.java @@ -23,6 +23,7 @@ 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 @@ interface InnerInterpolator { 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 valueSources = createValueSources(model, projectDir, config, problems); List postProcessors = createPostProcessors(model, projectDir, config); diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/io/DefaultModelReader.java b/maven-model-builder/src/main/java/org/apache/maven/model/io/DefaultModelReader.java index 07d2cab1b0d0..dbd62bebfbf4 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/io/DefaultModelReader.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/io/DefaultModelReader.java @@ -57,11 +57,17 @@ public DefaultModelReader(ModelSourceTransformer transformer) { @Override public Model read(File input, Map options) throws IOException { Objects.requireNonNull(input, "input cannot be null"); + return read(input.toPath(), options); + } + + @Override + public Model read(Path path, Map options) throws IOException { + Objects.requireNonNull(path, "path cannot be null"); - try (InputStream in = Files.newInputStream(input.toPath())) { - Model model = read(in, input.toPath(), options); + try (InputStream in = Files.newInputStream(path)) { + Model model = read(in, path, options); - model.setPomFile(input); + model.setPomPath(path); return model; } diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/io/ModelReader.java b/maven-model-builder/src/main/java/org/apache/maven/model/io/ModelReader.java index 601160d59d29..c656b41f2c88 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/io/ModelReader.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/io/ModelReader.java @@ -22,6 +22,7 @@ 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 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 options) throws IOException, ModelParseException; + /** * Reads the model from the specified character reader. The reader will be automatically closed before the method * returns. diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/locator/DefaultModelLocator.java b/maven-model-builder/src/main/java/org/apache/maven/model/locator/DefaultModelLocator.java index eb51e7547556..49fddd2a3530 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/locator/DefaultModelLocator.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/locator/DefaultModelLocator.java @@ -22,6 +22,9 @@ 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 @@ @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; diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/locator/ModelLocator.java b/maven-model-builder/src/main/java/org/apache/maven/model/locator/ModelLocator.java index 27b073c71d59..75e897f4fb6a 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/locator/ModelLocator.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/locator/ModelLocator.java @@ -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); + /** + * 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 + */ + 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 */ - default File locateExistingPom(File project) { - if (project == null || project.isDirectory()) { - project = locatePom(project); - } - return project.isFile() ? project : null; - } + Path locateExistingPom(Path project); } diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/path/DefaultModelPathTranslator.java b/maven-model-builder/src/main/java/org/apache/maven/model/path/DefaultModelPathTranslator.java index 1ec7468d8381..50e6ab2e96ed 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/path/DefaultModelPathTranslator.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/path/DefaultModelPathTranslator.java @@ -23,6 +23,7 @@ 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 DefaultModelPathTranslator(PathTranslator pathTranslator) { 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 @@ private List map(List resources, Function mapper) { 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 @@ private Resource alignToBaseDirectory(Resource resource, File basedir) { 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; } diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/path/DefaultPathTranslator.java b/maven-model-builder/src/main/java/org/apache/maven/model/path/DefaultPathTranslator.java index f5119b965f1a..0c40d746f50e 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/path/DefaultPathTranslator.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/path/DefaultPathTranslator.java @@ -22,6 +22,7 @@ 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 String alignToBaseDirectory(String path, File basedir) { 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(); } } diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/path/ModelPathTranslator.java b/maven-model-builder/src/main/java/org/apache/maven/model/path/ModelPathTranslator.java index a1b34314dcd9..9d3fdb8ae5c7 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/path/ModelPathTranslator.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/path/ModelPathTranslator.java @@ -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); } diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/path/PathTranslator.java b/maven-model-builder/src/main/java/org/apache/maven/model/path/PathTranslator.java index 83b3ae5407fd..c07ddc7e972e 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/path/PathTranslator.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/path/PathTranslator.java @@ -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); } diff --git a/maven-model-builder/src/test/java/org/apache/maven/model/interpolation/AbstractModelInterpolatorTest.java b/maven-model-builder/src/test/java/org/apache/maven/model/interpolation/AbstractModelInterpolatorTest.java index 6d288322260d..78059ae707c0 100644 --- a/maven-model-builder/src/test/java/org/apache/maven/model/interpolation/AbstractModelInterpolatorTest.java +++ b/maven-model-builder/src/test/java/org/apache/maven/model/interpolation/AbstractModelInterpolatorTest.java @@ -164,7 +164,7 @@ public void testShouldThrowExceptionOnRecursiveScmConnectionReference() throws E 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 void testBasedir() throws Exception { 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 void testBaseUri() throws Exception { 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 @@ void testRootDirectoryWithNull() throws Exception { 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 void shouldInterpolateSourceDirectoryReferencedFromResourceDirectoryCorre 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 outResources = out.getBuild().getResources(); @@ -483,7 +484,7 @@ public void testRecursiveExpressionCycleNPE() throws Exception { 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 void testRecursiveExpressionCycleBaseDir() throws Exception { 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 void shouldIgnorePropertiesWithPomPrefix() throws Exception { 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 void shouldWarnPropertiesWithPomPrefix() throws Exception { SimpleProblemCollector collector = new SimpleProblemCollector(); Model out = interpolator.interpolateModel( model, - null, + (Path) null, createModelBuildingRequest(context).setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MAVEN_3_1), collector); diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/ArtifactDescriptorReaderDelegate.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/ArtifactDescriptorReaderDelegate.java index b3a73711e28e..99c05e836365 100644 --- a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/ArtifactDescriptorReaderDelegate.java +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/ArtifactDescriptorReaderDelegate.java @@ -25,19 +25,21 @@ 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 void populateResult(RepositorySystemSession session, ArtifactDescriptorRe 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 @@ private Dependency convert(org.apache.maven.model.Dependency dependency, Artifac Map props = null; if (system) { - props = Collections.singletonMap(ArtifactProperties.LOCAL_PATH, dependency.getSystemPath()); + props = Collections.singletonMap(MavenArtifactProperties.LOCAL_PATH, dependency.getSystemPath()); } Artifact artifact = new DefaultArtifact( diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultArtifactDescriptorReader.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultArtifactDescriptorReader.java index a8c61693fa64..004b440c98e3 100644 --- a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultArtifactDescriptorReader.java +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultArtifactDescriptorReader.java @@ -211,10 +211,10 @@ private Model loadPom( 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())); diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultModelResolver.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultModelResolver.java index bdd1e5b35b53..8c6335faa28e 100644 --- a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultModelResolver.java +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultModelResolver.java @@ -159,7 +159,7 @@ public ModelSource resolveModel(String groupId, String artifactId, String versio 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 diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionRangeResolver.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionRangeResolver.java index 04aa979a6ffd..7576be307219 100644 --- a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionRangeResolver.java +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionRangeResolver.java @@ -200,9 +200,8 @@ private Versioning readVersions( 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()); } diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionResolver.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionResolver.java index 4b54afc8bd5c..c4096b960f5b 100644 --- a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionResolver.java +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultVersionResolver.java @@ -22,10 +22,10 @@ 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 @@ private Versioning readVersions( 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 @@ private static class Key { private final String context; - private final File localRepo; + private final Path localRepo; private final WorkspaceRepository workspace; @@ -437,7 +436,7 @@ private static class Key { 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()); diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/LocalSnapshotMetadata.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/LocalSnapshotMetadata.java index 99b9bb36fbab..506a138b0e9f 100644 --- a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/LocalSnapshotMetadata.java +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/LocalSnapshotMetadata.java @@ -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 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 @@ public void bind(Artifact artifact) { 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() { diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/LocalSnapshotMetadataGenerator.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/LocalSnapshotMetadataGenerator.java index ad08a4cfa9de..c5f3bf8f4525 100644 --- a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/LocalSnapshotMetadataGenerator.java +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/LocalSnapshotMetadataGenerator.java @@ -38,7 +38,7 @@ */ class LocalSnapshotMetadataGenerator implements MetadataGenerator { - private Map snapshots; + private final Map snapshots; private final Date timestamp; diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/MavenMetadata.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/MavenMetadata.java index 7c8c07bbee51..539050edd494 100644 --- a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/MavenMetadata.java +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/MavenMetadata.java @@ -25,6 +25,7 @@ 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 @@ public String getType() { 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 @@ public boolean isMerged() { 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); } } diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/MavenSessionBuilderSupplier.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/MavenSessionBuilderSupplier.java index 8746fa457b67..fd59a2d87f0a 100644 --- a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/MavenSessionBuilderSupplier.java +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/MavenSessionBuilderSupplier.java @@ -24,10 +24,12 @@ 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 @@ protected DependencyTraverser getDependencyTraverser() { 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() { diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/MavenSnapshotMetadata.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/MavenSnapshotMetadata.java index 23160a9e83ce..236830679f4c 100644 --- a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/MavenSnapshotMetadata.java +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/MavenSnapshotMetadata.java @@ -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 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) { diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/PluginsMetadata.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/PluginsMetadata.java index 18fdbba38e73..46444d55f585 100644 --- a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/PluginsMetadata.java +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/PluginsMetadata.java @@ -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 @@ static final class PluginInfo { 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 @@ protected void merge(Metadata recessive) { } } + @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 diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/PluginsMetadataGenerator.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/PluginsMetadataGenerator.java index 58868076404f..f02e1d1eb2e0 100644 --- a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/PluginsMetadataGenerator.java +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/PluginsMetadataGenerator.java @@ -115,8 +115,8 @@ private PluginInfo extractPluginInfo(Artifact artifact) { 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); diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/RelocatedArtifact.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/RelocatedArtifact.java index bf1f97e29406..72208aafec83 100644 --- a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/RelocatedArtifact.java +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/RelocatedArtifact.java @@ -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 Artifact setVersion(String version) { 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 Artifact setFile(File file) { 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 properties) { Map current = getProperties(); @@ -135,11 +147,17 @@ public Artifact setProperties(Map properties) { 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); diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/RemoteSnapshotMetadata.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/RemoteSnapshotMetadata.java index 2ad4a379b0fe..ae6789b9a20c 100644 --- a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/RemoteSnapshotMetadata.java +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/RemoteSnapshotMetadata.java @@ -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) { diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/VersionsMetadata.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/VersionsMetadata.java index 12e1ce0a2538..2a7737a39535 100644 --- a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/VersionsMetadata.java +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/VersionsMetadata.java @@ -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 @@ public static Object getKey(Artifact artifact) { 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 diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/artifact/MavenArtifactProperties.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/artifact/MavenArtifactProperties.java index 5f591f8a1ad8..8de35b7cb047 100644 --- a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/artifact/MavenArtifactProperties.java +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/artifact/MavenArtifactProperties.java @@ -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 } diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/scopes/MavenSystemScopeHandler.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/scopes/MavenSystemScopeHandler.java new file mode 100644 index 000000000000..8384fe26a9f7 --- /dev/null +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/scopes/MavenSystemScopeHandler.java @@ -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 properties, String systemPath) { + if (systemPath == null) { + properties.remove(MavenArtifactProperties.LOCAL_PATH); + } else { + properties.put(MavenArtifactProperties.LOCAL_PATH, systemPath); + } + } +} diff --git a/pom.xml b/pom.xml index 375f7bcf69de..95bf360858ac 100644 --- a/pom.xml +++ b/pom.xml @@ -177,7 +177,7 @@ under the License. 1.26 1.0.0 4.0.1 - 2.0.0-alpha-7 + 2.0.0-alpha-8 2.0 0.9.0.M2 2.0.11