Skip to content

Commit

Permalink
Plugins dependencies should be filtered
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed May 30, 2024
1 parent 996d5ee commit 082b2ee
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 20 deletions.
25 changes: 16 additions & 9 deletions src/main/java/org/simplify4u/plugins/ArtifactResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import io.vavr.control.Try;
import org.apache.maven.RepositoryUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.execution.MavenSession;
Expand Down Expand Up @@ -87,6 +88,8 @@ public class ArtifactResolver {

private final List<RemoteRepository> remotePluginRepositories;

private final ArtifactHandlerManager artifactHandlerManager;

/**
* Copy of remote repositories with check sum policy set to ignore, we need it for pgp signature resolving.
* <p>
Expand All @@ -95,13 +98,15 @@ public class ArtifactResolver {
private final List<RemoteRepository> remoteRepositoriesIgnoreCheckSum;

@Inject
ArtifactResolver(MavenSession session, RepositorySystem repositorySystem) {
ArtifactResolver(MavenSession session, RepositorySystem repositorySystem,
ArtifactHandlerManager artifactHandlerManager) {
this.remoteProjectRepositories = requireNonNull(session.getCurrentProject().getRemoteProjectRepositories());
this.remotePluginRepositories = requireNonNull(session.getCurrentProject().getRemotePluginRepositories());
this.repositorySystem = requireNonNull(repositorySystem);
this.repositorySession = requireNonNull(session.getRepositorySession());
this.remoteRepositoriesIgnoreCheckSum = repositoriesIgnoreCheckSum(remoteProjectRepositories,
remotePluginRepositories);
this.artifactHandlerManager = artifactHandlerManager;
}

/**
Expand Down Expand Up @@ -210,15 +215,20 @@ private List<Artifact> resolvePlugin(Plugin plugin, Configuration config) {
}

List<org.eclipse.aether.artifact.Artifact> result;

List<Dependency> pluginDependencies = plugin.getDependencies().stream()
.filter(d -> !config.dependencyFilter.shouldSkipDependency(d, artifactHandlerManager))
.map(d -> RepositoryUtils.toDependency(d, repositorySession.getArtifactTypeRegistry()))
.collect(Collectors.toList());

if (config.verifyPluginDependencies) {
// we need resolve all transitive dependencies
result = resolvePluginArtifactsTransitive(pArtifact, plugin.getDependencies(), config.verifyPomFiles);
result = resolvePluginArtifactsTransitive(pArtifact, pluginDependencies, config.verifyPomFiles);
} else {
// only resolve plugin artifact
List<org.eclipse.aether.artifact.Artifact> aeArtifacts = new ArrayList<>();
aeArtifacts.add(pArtifact);
aeArtifacts.addAll(plugin.getDependencies().stream().map(
d -> RepositoryUtils.toDependency(d, repositorySession.getArtifactTypeRegistry()))
aeArtifacts.addAll(pluginDependencies.stream()
.map(Dependency::getArtifact)
.collect(Collectors.toList()));

Expand All @@ -230,14 +240,11 @@ private List<Artifact> resolvePlugin(Plugin plugin, Configuration config) {

private List<org.eclipse.aether.artifact.Artifact> resolvePluginArtifactsTransitive(
org.eclipse.aether.artifact.Artifact artifact,
List<org.apache.maven.model.Dependency> dependencies, boolean verifyPomFiles) {
List<Dependency> dependencies, boolean verifyPomFiles) {

CollectRequest collectRequest = new CollectRequest(new Dependency(artifact, "runtime"),
remotePluginRepositories);

dependencies.stream().map(d -> RepositoryUtils.toDependency(d, repositorySession.getArtifactTypeRegistry()))
.forEach(collectRequest::addDependency);

collectRequest.setDependencies(dependencies);
DependencyRequest request = new DependencyRequest(collectRequest, null);

DependencyResult dependencyResult =
Expand Down
31 changes: 25 additions & 6 deletions src/main/java/org/simplify4u/plugins/skipfilters/SkipFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,40 @@
package org.simplify4u.plugins.skipfilters;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DefaultArtifact;
import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
import org.apache.maven.model.Dependency;

/**
* An interface for a filter that determines whether or not a particular artifact should be
* processed or skipped, based on the mojo configuration.
*/
public interface SkipFilter {

/**
* Indicates whether or not an artifact should be skipped, based on the configuration of this
* Indicates whether an artifact should be skipped, based on the configuration of this
* filter.
*
* @param artifact
* The artifact being considered for verification.
*
* @return {@code true} if the artifact should be skipped; {@code false} if it should be
* processed.
* @param artifact The artifact being considered for verification.
* @return {@code true} if the artifact should be skipped; {@code false} if it should be
* processed.
*/
boolean shouldSkipArtifact(final Artifact artifact);

/**
* Indicates whether a dependency should be skipped, based on the configuration of this
* filter.
*
* @param dependency The artifact being considered for verification.
* @param artifactHandlerManager a artifactHandlerManager
* @return {@code true} if the artifact should be skipped; {@code false} if it should be
* processed.
*/
default boolean shouldSkipDependency(Dependency dependency, ArtifactHandlerManager artifactHandlerManager) {
DefaultArtifact artifact =
new DefaultArtifact(dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion(),
dependency.getScope(), dependency.getType(), dependency.getClassifier(),
artifactHandlerManager.getArtifactHandler(dependency.getType()));
return shouldSkipArtifact(artifact);
}
}
14 changes: 9 additions & 5 deletions src/test/java/org/simplify4u/plugins/ArtifactResolverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DefaultArtifact;
import org.apache.maven.artifact.handler.DefaultArtifactHandler;
import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.project.MavenProject;
import org.assertj.core.api.Condition;
Expand Down Expand Up @@ -70,6 +71,9 @@ class ArtifactResolverTest {
@Mock
private RepositorySystemSession repositorySession;

@Mock
private ArtifactHandlerManager artifactHandlerManager;

@Mock
private MavenSession session;

Expand All @@ -82,25 +86,25 @@ class ArtifactResolverTest {
void setup() {
when(session.getCurrentProject()).thenReturn(project);
when(session.getRepositorySession()).thenReturn(repositorySession);
resolver = new ArtifactResolver(session, aetherRepositorySystem);
resolver = new ArtifactResolver(session, aetherRepositorySystem, artifactHandlerManager);
}

@Test
void testConstructArtifactResolverWithNull() {

reset(session, project);

assertThatCode(() -> new ArtifactResolver(null, null))
assertThatCode(() -> new ArtifactResolver(null, null, null))
.isExactlyInstanceOf(NullPointerException.class);

assertThatCode(() -> new ArtifactResolver(session, null))
assertThatCode(() -> new ArtifactResolver(session, null, null))
.isExactlyInstanceOf(NullPointerException.class);

assertThatCode(() -> new ArtifactResolver(session, null))
assertThatCode(() -> new ArtifactResolver(session, null, null))
.isExactlyInstanceOf(NullPointerException.class);

doThrow(new NullPointerException()).when(session).getCurrentProject();
assertThatCode(() -> new ArtifactResolver(session, null))
assertThatCode(() -> new ArtifactResolver(session, null, null))
.isExactlyInstanceOf(NullPointerException.class);
}

Expand Down

0 comments on commit 082b2ee

Please sign in to comment.