Skip to content

Commit

Permalink
Plugins transitive dependencies should be filtered
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed May 31, 2024
1 parent fa91320 commit 236e2c2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/main/java/org/simplify4u/plugins/ArtifactResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.collection.CollectRequest;
import org.eclipse.aether.graph.Dependency;
import org.eclipse.aether.graph.DependencyNode;
import org.eclipse.aether.graph.DependencyVisitor;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.repository.RepositoryPolicy;
import org.eclipse.aether.resolution.ArtifactRequest;
Expand Down Expand Up @@ -223,7 +225,7 @@ private List<Artifact> resolvePlugin(Plugin plugin, Configuration config) {

if (config.verifyPluginDependencies) {
// we need resolve all transitive dependencies
result = resolvePluginArtifactsTransitive(pArtifact, pluginDependencies, config.verifyPomFiles);
result = resolvePluginArtifactsTransitive(pArtifact, pluginDependencies, config.dependencyFilter, config.verifyPomFiles);
} else {
// only resolve plugin artifact
List<org.eclipse.aether.artifact.Artifact> aeArtifacts = new ArrayList<>();
Expand All @@ -240,7 +242,7 @@ private List<Artifact> resolvePlugin(Plugin plugin, Configuration config) {

private List<org.eclipse.aether.artifact.Artifact> resolvePluginArtifactsTransitive(
org.eclipse.aether.artifact.Artifact artifact,
List<Dependency> dependencies, boolean verifyPomFiles) {
List<Dependency> dependencies, SkipFilter dependencyFilter, boolean verifyPomFiles) {

CollectRequest collectRequest = new CollectRequest(new Dependency(artifact, "runtime"),
remotePluginRepositories);
Expand All @@ -252,12 +254,21 @@ private List<org.eclipse.aether.artifact.Artifact> resolvePluginArtifactsTransit
.recover(DependencyResolutionException.class, DependencyResolutionException::getResult)
.get();

List<org.eclipse.aether.artifact.Artifact> result = new ArrayList<>(
dependencyResult.getArtifactResults().stream()
.map(aResult -> aResult.isResolved() ?
aResult.getArtifact() :
aResult.getRequest().getArtifact())
.collect(Collectors.toList()));
List<org.eclipse.aether.artifact.Artifact> result = new ArrayList<>();
dependencyResult.getRoot().accept(new DependencyVisitor() {
@Override
public boolean visitEnter(DependencyNode node) {
if (node.getArtifact() != null && !dependencyFilter.shouldSkipDependency(node.getDependency())) {
result.add(node.getArtifact());
}
return true;
}

@Override
public boolean visitLeave(DependencyNode node) {
return true;
}
});

if (verifyPomFiles) {
resolvePoms(result, remotePluginRepositories);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.simplify4u.plugins.skipfilters;

import org.apache.maven.RepositoryUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DefaultArtifact;
import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
Expand Down Expand Up @@ -53,4 +54,12 @@ default boolean shouldSkipDependency(Dependency dependency, ArtifactHandlerManag
artifactHandlerManager.getArtifactHandler(dependency.getType()));
return shouldSkipArtifact(artifact);
}

default boolean shouldSkipDependency(org.eclipse.aether.graph.Dependency dependency) {

Artifact artifact = RepositoryUtils.toArtifact(dependency.getArtifact());
artifact.setScope(dependency.getScope());
artifact.setOptional(dependency.getOptional());
return shouldSkipArtifact(artifact);
}
}

0 comments on commit 236e2c2

Please sign in to comment.