Skip to content

Commit

Permalink
[MENFORCER-474] Filter dependency by scope on project level
Browse files Browse the repository at this point in the history
We should filter dependencies scopes on project level,
for transitive dependency tree we should not change defaults selectors.
  • Loading branch information
slawekjaranowski committed Mar 23, 2023
1 parent 57746a1 commit 5e0ca5a
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 163 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.apache.maven.enforcer.rules.dependency;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

Expand Down Expand Up @@ -102,7 +103,7 @@ public void execute() throws EnforcerRuleException {
}
} else {
StringBuilder messageBuilder = new StringBuilder();
DependencyNode rootNode = resolveUtil.resolveTransitiveDependenciesVerbose();
DependencyNode rootNode = resolveUtil.resolveTransitiveDependenciesVerbose(Collections.emptyList());
if (!validate(rootNode, 0, messageBuilder)) {
String message = "";
if (getMessage() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,8 @@

import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
import org.apache.maven.enforcer.rules.AbstractStandardEnforcerRule;
import org.apache.maven.enforcer.rules.dependency.selector.AllLevelsOptionalDependencySelector;
import org.apache.maven.enforcer.rules.dependency.selector.AllLevelsScopeDependencySelector;
import org.apache.maven.enforcer.rules.utils.ArtifactUtils;
import org.eclipse.aether.graph.DependencyNode;
import org.eclipse.aether.util.graph.selector.ExclusionDependencySelector;

import static org.apache.maven.artifact.Artifact.SCOPE_PROVIDED;
import static org.apache.maven.artifact.Artifact.SCOPE_TEST;
Expand Down Expand Up @@ -68,10 +65,7 @@ public DependencyConvergence(ResolveUtil resolveUtil) {
@Override
public void execute() throws EnforcerRuleException {

DependencyNode node = resolveUtil.resolveTransitiveDependenciesVerbose(
new AllLevelsOptionalDependencySelector(),
new AllLevelsScopeDependencySelector(excludedScopes),
new ExclusionDependencySelector());
DependencyNode node = resolveUtil.resolveTransitiveDependenciesVerbose(excludedScopes);
dependencyVersionMap = new DependencyVersionMap().setUniqueVersions(uniqueVersions);
node.accept(dependencyVersionMap);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import javax.inject.Named;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand All @@ -34,15 +35,12 @@
import org.apache.maven.artifact.versioning.OverConstrainedVersionException;
import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
import org.apache.maven.enforcer.rules.AbstractStandardEnforcerRule;
import org.apache.maven.enforcer.rules.dependency.selector.AllLevelsOptionalDependencySelector;
import org.apache.maven.enforcer.rules.dependency.selector.AllLevelsScopeDependencySelector;
import org.apache.maven.enforcer.rules.utils.ArtifactUtils;
import org.apache.maven.enforcer.rules.utils.ParentNodeProvider;
import org.apache.maven.enforcer.rules.utils.ParentsVisitor;
import org.eclipse.aether.graph.DependencyNode;
import org.eclipse.aether.graph.DependencyVisitor;
import org.eclipse.aether.util.graph.manager.DependencyManagerUtils;
import org.eclipse.aether.util.graph.selector.ExclusionDependencySelector;

import static org.apache.maven.artifact.Artifact.SCOPE_PROVIDED;
import static org.apache.maven.artifact.Artifact.SCOPE_TEST;
Expand Down Expand Up @@ -103,10 +101,8 @@ public void setIncludes(List<String> includes) {

@Override
public void execute() throws EnforcerRuleException {
DependencyNode node = resolveUtil.resolveTransitiveDependenciesVerbose(
new AllLevelsOptionalDependencySelector(),
new AllLevelsScopeDependencySelector(SCOPE_TEST, SCOPE_PROVIDED),
new ExclusionDependencySelector());
DependencyNode node =
resolveUtil.resolveTransitiveDependenciesVerbose(Arrays.asList(SCOPE_TEST, SCOPE_PROVIDED));
upperBoundDepsVisitor = new RequireUpperBoundDepsVisitor()
.setUniqueVersions(uniqueVersions)
.setIncludes(includes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,30 @@
import javax.inject.Inject;
import javax.inject.Named;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import org.apache.maven.RepositoryUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.DependencyManagement;
import org.apache.maven.project.MavenProject;
import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.artifact.ArtifactTypeRegistry;
import org.eclipse.aether.collection.CollectRequest;
import org.eclipse.aether.collection.DependencyCollectionException;
import org.eclipse.aether.collection.DependencySelector;
import org.eclipse.aether.graph.Dependency;
import org.eclipse.aether.graph.DependencyNode;
import org.eclipse.aether.util.graph.manager.DependencyManagerUtils;
import org.eclipse.aether.util.graph.selector.AndDependencySelector;
import org.eclipse.aether.util.graph.transformer.ConflictResolver;

import static java.util.Optional.ofNullable;
import static org.apache.maven.artifact.Artifact.SCOPE_PROVIDED;
import static org.apache.maven.artifact.Artifact.SCOPE_TEST;

/**
* Resolver helper class.
Expand Down Expand Up @@ -72,59 +75,59 @@ class ResolveUtil {
* Please consult {@link ConflictResolver} and {@link DependencyManagerUtils}>
* </p>
*
* @param selectors zero or more {@link DependencySelector} instances
* @param excludedScopes a project dependency scope to excluded
* @return a Dependency Node which is the root of the project's dependency tree
* @throws EnforcerRuleException thrown if the lookup fails
*/
DependencyNode resolveTransitiveDependenciesVerbose(DependencySelector... selectors) throws EnforcerRuleException {
return resolveTransitiveDependencies(true, selectors);
DependencyNode resolveTransitiveDependenciesVerbose(List<String> excludedScopes) throws EnforcerRuleException {
return resolveTransitiveDependencies(true, true, excludedScopes);
}

/**
* Retrieves the {@link DependencyNode} instance containing the result of the transitive dependency
* for the current {@link MavenProject}.
*
* @param selectors zero or more {@link DependencySelector} instances
* @return a Dependency Node which is the root of the project's dependency tree
* @throws EnforcerRuleException thrown if the lookup fails
*/
DependencyNode resolveTransitiveDependencies(DependencySelector... selectors) throws EnforcerRuleException {
return resolveTransitiveDependencies(false, selectors);
DependencyNode resolveTransitiveDependencies() throws EnforcerRuleException {
return resolveTransitiveDependencies(false, true, Arrays.asList(SCOPE_TEST, SCOPE_PROVIDED));
}

private DependencyNode resolveTransitiveDependencies(boolean verbose, DependencySelector... selectors)
throws EnforcerRuleException {
private DependencyNode resolveTransitiveDependencies(
boolean verbose, boolean excludeOptional, List<String> excludedScopes) throws EnforcerRuleException {

try {
RepositorySystemSession repositorySystemSession = session.getRepositorySession();

if (verbose) {
DefaultRepositorySystemSession defaultRepositorySystemSession =
new DefaultRepositorySystemSession(repositorySystemSession);
defaultRepositorySystemSession.setConfigProperty(ConflictResolver.CONFIG_PROP_VERBOSE, true);
defaultRepositorySystemSession.setConfigProperty(DependencyManagerUtils.CONFIG_PROP_VERBOSE, true);
repositorySystemSession = defaultRepositorySystemSession;
}

MavenProject project = session.getCurrentProject();
ArtifactTypeRegistry artifactTypeRegistry =
session.getRepositorySession().getArtifactTypeRegistry();

DefaultRepositorySystemSession repositorySystemSession =
new DefaultRepositorySystemSession(session.getRepositorySession());

if (selectors.length > 0) {
repositorySystemSession.setDependencySelector(new AndDependencySelector(selectors));
}
List<Dependency> dependencies = project.getDependencies().stream()
.filter(d -> !(excludeOptional && d.isOptional()))
.filter(d -> !excludedScopes.contains(d.getScope()))
.map(d -> RepositoryUtils.toDependency(d, artifactTypeRegistry))
.collect(Collectors.toList());

if (verbose) {
repositorySystemSession.setConfigProperty(ConflictResolver.CONFIG_PROP_VERBOSE, true);
repositorySystemSession.setConfigProperty(DependencyManagerUtils.CONFIG_PROP_VERBOSE, true);
}

CollectRequest collectRequest = new CollectRequest(
project.getDependencies().stream()
List<Dependency> managedDependencies = ofNullable(project.getDependencyManagement())
.map(DependencyManagement::getDependencies)
.map(list -> list.stream()
.map(d -> RepositoryUtils.toDependency(d, artifactTypeRegistry))
.collect(Collectors.toList()),
ofNullable(project.getDependencyManagement())
.map(DependencyManagement::getDependencies)
.map(list -> list.stream()
.map(d -> RepositoryUtils.toDependency(d, artifactTypeRegistry))
.collect(Collectors.toList()))
.orElse(null),
project.getRemoteProjectRepositories());
Artifact artifact = project.getArtifact();
collectRequest.setRootArtifact(RepositoryUtils.toArtifact(artifact));
.collect(Collectors.toList()))
.orElse(null);

CollectRequest collectRequest =
new CollectRequest(dependencies, managedDependencies, project.getRemoteProjectRepositories());
collectRequest.setRootArtifact(RepositoryUtils.toArtifact(project.getArtifact()));

return repositorySystem
.collectDependencies(repositorySystemSession, collectRequest)
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.mockito.junit.jupiter.MockitoExtension;

import static org.assertj.core.api.Assertions.assertThatCode;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.Mockito.when;

/**
Expand Down Expand Up @@ -94,7 +95,7 @@ void excludesAndIncludesDoNotUseTransitiveDependencies() throws Exception {
@Test
void excludesUseTransitiveDependencies() throws Exception {

when(resolveUtil.resolveTransitiveDependenciesVerbose())
when(resolveUtil.resolveTransitiveDependenciesVerbose(anyList()))
.thenReturn(new DependencyNodeBuilder()
.withType(DependencyNodeBuilder.Type.POM)
.withChildNode(new DependencyNodeBuilder()
Expand Down Expand Up @@ -127,7 +128,7 @@ void excludesUseTransitiveDependencies() throws Exception {
@Test
void excludesAndIncludesUseTransitiveDependencies() throws Exception {

when(resolveUtil.resolveTransitiveDependenciesVerbose())
when(resolveUtil.resolveTransitiveDependenciesVerbose(anyList()))
.thenReturn(new DependencyNodeBuilder()
.withType(DependencyNodeBuilder.Type.POM)
.withChildNode(new DependencyNodeBuilder()
Expand Down
Loading

0 comments on commit 5e0ca5a

Please sign in to comment.