Skip to content

Commit

Permalink
GH-101: Use Aether artifact resolver directly
Browse files Browse the repository at this point in the history
This removes the Maven Artifact Transfer dependency entirely and integrates
with the Maven Dependency Resolver API directly instead. This helps make this
more compatible with the future Maven 4.x API that will expect this usage and
this removes a set of dependencies that we do not actually need to be using.

As a small side effect, the project dependency resolution is now even
more optimised, as we no longer need to scan project dependencies again
to re-resolve them when the direct resolution mode is in use. This should
slightly improve build speeds in some more complicated edge cases and
configurations
  • Loading branch information
ascopes committed Apr 5, 2024
1 parent 1b77c15 commit aa758c5
Show file tree
Hide file tree
Showing 8 changed files with 221 additions and 139 deletions.
31 changes: 9 additions & 22 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@
<immutables.version>2.10.1</immutables.version>
<jspecify.version>0.3.0</jspecify.version>
<junit.version>5.10.2</junit.version>
<maven-artifact-transfer.version>0.13.1</maven-artifact-transfer.version>
<maven-core.version>3.9.6</maven-core.version>
<maven-plugin-annotations.version>3.11.0</maven-plugin-annotations.version>
<maven-plugin-api.version>3.9.6</maven-plugin-api.version>
<maven-resolver-api.version>1.9.18</maven-resolver-api.version>
<memoryfilesystem.version>2.8.0</memoryfilesystem.version>
<mockito.version>5.11.0</mockito.version>
<slf4j.version>2.0.12</slf4j.version>
Expand Down Expand Up @@ -175,32 +175,19 @@
</dependency>

<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>${maven-core.version}</version>
<groupId>org.apache.maven.resolver</groupId>
<artifactId>maven-resolver-api</artifactId>
<version>${maven-resolver-api.version}</version>
<!-- Provided by Maven at runtime -->
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-artifact-transfer</artifactId>
<version>${maven-artifact-transfer.version}</version>
<scope>compile</scope>

<exclusions>
<!-- These are pulled in with provided scope by other dependencies,
so do not depend on them with compile scope as this will raise
build warnings. -->
<exclusion>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>
</exclusion>
</exclusions>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>${maven-core.version}</version>
<!-- Provided by Maven at runtime -->
<scope>provided</scope>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@
@Named
public final class BinaryPluginResolver {

private final MavenDependencyPathResolver mavenDependencyPathResolver;
private final MavenDependencyPathResolver dependencyResolver;
private final PlatformArtifactFactory platformDependencyFactory;
private final SystemPathBinaryResolver systemPathResolver;
private final UrlResourceFetcher urlResourceFetcher;

@Inject
public BinaryPluginResolver(
MavenDependencyPathResolver mavenDependencyPathResolver,
MavenDependencyPathResolver dependencyResolver,
PlatformArtifactFactory platformDependencyFactory,
SystemPathBinaryResolver systemPathResolver,
UrlResourceFetcher urlResourceFetcher
) {
this.mavenDependencyPathResolver = mavenDependencyPathResolver;
this.dependencyResolver = dependencyResolver;
this.platformDependencyFactory = platformDependencyFactory;
this.systemPathResolver = systemPathResolver;
this.urlResourceFetcher = urlResourceFetcher;
Expand Down Expand Up @@ -84,7 +84,10 @@ private ResolvedPlugin resolveMavenPlugin(
plugin.getClassifier().orElse(null)
);

var path = mavenDependencyPathResolver.resolveArtifact(session, plugin);
// Only one dependency should ever be returned here.
var path = dependencyResolver.resolveOne(session, plugin, DependencyResolutionDepth.DIRECT)
.iterator()
.next();
makeExecutable(path);
return createResolvedPlugin(path);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.inject.Inject;
import javax.inject.Named;
import org.apache.maven.execution.MavenSession;
Expand All @@ -47,17 +46,17 @@
public final class JvmPluginResolver {

private final HostSystem hostSystem;
private final MavenDependencyPathResolver dependencyPathResolver;
private final MavenDependencyPathResolver dependencyResolver;
private final TemporarySpace temporarySpace;

@Inject
public JvmPluginResolver(
HostSystem hostSystem,
MavenDependencyPathResolver dependencyPathResolver,
MavenDependencyPathResolver dependencyResolver,
TemporarySpace temporarySpace
) {
this.hostSystem = hostSystem;
this.dependencyPathResolver = dependencyPathResolver;
this.dependencyResolver = dependencyResolver;
this.temporarySpace = temporarySpace;
}

Expand Down Expand Up @@ -99,12 +98,8 @@ private List<String> resolveAndBuildArgLine(
) throws ResolutionException {

// Resolve dependencies first.
var dependencyIterator = dependencyPathResolver
.resolveDependencyTreePaths(
session,
DependencyResolutionDepth.TRANSITIVE,
plugin
)
var dependencyIterator = dependencyResolver
.resolveOne(session, plugin, DependencyResolutionDepth.TRANSITIVE)
.iterator();

// First dependency is always the thing we actually want to execute,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,20 @@
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.maven.model.Dependency;
import org.apache.maven.shared.transfer.artifact.ArtifactCoordinate;
import org.apache.maven.shared.transfer.artifact.DefaultArtifactCoordinate;
import org.apache.maven.shared.transfer.dependencies.DefaultDependableCoordinate;
import org.apache.maven.shared.transfer.dependencies.DependableCoordinate;
import org.jspecify.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
* Implementation independent deacriptor for an artifact or dependency that
* can be used in a Maven Plugin parameter.
* Implementation independent descriptor for an artifact or dependency that can be used in a Maven
* Plugin parameter.
*
* @author Ashley Scopes
* @since 1.2.0
*/
public final class MavenArtifact {

private static final Logger log = LoggerFactory.getLogger(MavenArtifact.class);

private @Nullable String groupId;
Expand Down Expand Up @@ -93,7 +89,7 @@ public void setType(@Nullable String type) {
public void setExtension(@Nullable String extension) {
log.warn("MavenArtifact.extension is deprecated for removal in v2.0.0. "
+ "Please use MavenArtifact.type instead for future compatibility.");
this.type = extension;
type = extension;
}

@Override
Expand Down Expand Up @@ -122,34 +118,4 @@ public String toString() {
.map(attr -> Objects.requireNonNullElse(attr, ""))
.collect(Collectors.joining(":"));
}

public ArtifactCoordinate toArtifactCoordinate() {
var coordinate = new DefaultArtifactCoordinate();
coordinate.setGroupId(groupId);
coordinate.setArtifactId(artifactId);
coordinate.setVersion(version);
coordinate.setClassifier(classifier);
coordinate.setExtension(type);
return coordinate;
}

public DependableCoordinate toDependableCoordinate() {
var coordinate = new DefaultDependableCoordinate();
coordinate.setGroupId(groupId);
coordinate.setArtifactId(artifactId);
coordinate.setVersion(version);
coordinate.setClassifier(classifier);
coordinate.setType(type);
return coordinate;
}

public static MavenArtifact fromDependency(Dependency dependency) {
var mavenArtifact = new MavenArtifact();
mavenArtifact.setGroupId(dependency.getGroupId());
mavenArtifact.setArtifactId(dependency.getArtifactId());
mavenArtifact.setVersion(dependency.getVersion());
mavenArtifact.setClassifier(dependency.getClassifier());
mavenArtifact.setType(dependency.getType());
return mavenArtifact;
}
}
Loading

0 comments on commit aa758c5

Please sign in to comment.