Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MNG-8059] Paths everywhere #1413

Merged
merged 3 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions api/maven-api-model/src/main/mdo/maven.mdo
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -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}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(", ");
}
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
*/
class DefaultProblemCollector implements ProblemCollector {

private List<Problem> problems;
private final List<Problem> problems;

private String source;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,44 +22,69 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;

/**
* Wraps an ordinary {@link File} as a source.
*
*/
public class FileSource implements Source {
private final File file;
private final Path path;

private final int hashCode;

/**
* 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
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why actually?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

url equality check performs hostname resolution

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really? WTF?! Why not #toAsciiString()?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whatever, am just creating string out of both in same way and comparing the two.

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -63,7 +64,7 @@ public List<ArtifactResult> 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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -144,7 +145,7 @@ public static Artifact toArtifact(org.apache.maven.artifact.Artifact artifact) {
Map<String, String> props = null;
if (org.apache.maven.artifact.Artifact.SCOPE_SYSTEM.equals(artifact.getScope())) {
String localPath = (artifact.getFile() != null) ? artifact.getFile().getPath() : "";
props = Collections.singletonMap(ArtifactProperties.LOCAL_PATH, localPath);
props = Collections.singletonMap(MavenArtifactProperties.LOCAL_PATH, localPath);
}

Artifact result = new DefaultArtifact(
Expand Down Expand Up @@ -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) {
Expand All @@ -279,7 +280,7 @@ public static Dependency toDependency(

Map<String, String> props = null;
if (system) {
props = Collections.singletonMap(ArtifactProperties.LOCAL_PATH, dependency.getSystemPath());
props = Collections.singletonMap(MavenArtifactProperties.LOCAL_PATH, dependency.getSystemPath());
}

Artifact artifact = new DefaultArtifact(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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<File> paths = new ArrayList<>();
paths.add(new File(request.getLocalRepository().getBasedir()));
ArrayList<Path> paths = new ArrayList<>();
paths.add(Paths.get(request.getLocalRepository().getBasedir()));
String localRepoTail = mergedProps.get(MAVEN_REPO_LOCAL_TAIL);
if (localRepoTail != null) {
Arrays.stream(localRepoTail.split(","))
.filter(p -> p != null && !p.trim().isEmpty())
.map(File::new)
.map(Paths::get)
.forEach(paths::add);
}
sessionBuilder.withLocalRepositoryBaseDirectories(paths);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -1381,7 +1381,7 @@ private Model interpolateModel(Model model, ModelBuildingRequest request, ModelP
Map<String, Activation> originalActivations = getProfileActivations(model, true);

Model interpolatedModel = new Model(modelInterpolator.interpolateModel(
model.getDelegate(), model.getProjectDirectory(), request, problems));
model.getDelegate(), model.getProjectDirectoryPath(), request, problems));
if (interpolatedModel.getParent() != null) {
StringSearchInterpolator ssi = new StringSearchInterpolator();
ssi.addValueSource(new MapBasedValueSource(request.getUserProperties()));
Expand All @@ -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);
Expand Down Expand Up @@ -1468,7 +1468,7 @@ private ModelData readParentLocally(
if (candidateModel == null) {
return null;
}
candidateSource = new FileModelSource(candidateModel.getPomFile());
candidateSource = new FileModelSource(candidateModel.getPomPath());
}

//
Expand Down Expand Up @@ -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 {
Expand Down
Loading