Skip to content

Commit

Permalink
[MENFORCER-470] Add scopes to DependencyConvergence rule
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-anufriev authored and slawekjaranowski committed Mar 14, 2023
1 parent e6c1714 commit da1351a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
Expand Up @@ -22,10 +22,12 @@
import javax.inject.Named;

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

import org.apache.maven.artifact.Artifact;
import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
import org.apache.maven.enforcer.rules.AbstractStandardEnforcerRule;
import org.apache.maven.enforcer.rules.utils.ArtifactUtils;
Expand All @@ -35,9 +37,6 @@
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;

/**
* @author <a href="mailto:rex@e-hoffman.org">Rex Hoffman</a>
*/
Expand All @@ -50,6 +49,8 @@ public final class DependencyConvergence extends AbstractStandardEnforcerRule {

private List<String> excludes;

private List<String> scopes = Arrays.asList(Artifact.SCOPE_COMPILE, Artifact.SCOPE_RUNTIME, Artifact.SCOPE_SYSTEM);

private DependencyVersionMap dependencyVersionMap;

private final ResolveUtil resolveUtil;
Expand All @@ -71,8 +72,7 @@ public boolean selectDependency(Dependency dependency) {
return !dependency.isOptional()
// regular scope selectors only discard transitive dependencies
// and always allow direct dependencies
&& !dependency.getScope().equals(SCOPE_TEST)
&& !dependency.getScope().equals(SCOPE_PROVIDED);
&& scopes.contains(dependency.getScope());
}

@Override
Expand Down Expand Up @@ -142,7 +142,7 @@ private String buildConvergenceErrorMsg(List<DependencyNode> nodeList) {
@Override
public String toString() {
return String.format(
"DependencyConvergence[includes=%s, excludes=%s, uniqueVersions=%b]",
includes, excludes, uniqueVersions);
"DependencyConvergence[includes=%s, excludes=%s, uniqueVersions=%b, scopes=%s]",
includes, excludes, uniqueVersions, String.join(",", scopes));
}
}
7 changes: 7 additions & 0 deletions enforcer-rules/src/site/apt/dependencyConvergence.apt.vm
Expand Up @@ -139,6 +139,9 @@ and
* <<excludes>> - A list of artifacts for which dependency convergence should not be enforced. These are exceptions
to the includes.

* <<scopes>> - A list of scopes of artifacts for which dependency convergence should be enforced. Not specifying
any scopes is interpreted as having the following scopes activated: compile, runtime, system.

[]

The format for artifacts is groupId[:artifactId][:version][:type][:scope][:classifier] where artifactId, version,
Expand All @@ -147,6 +150,10 @@ and

+---------------------------------------------
<dependencyConvergence>
<scopes>
<scope>compile</scope>
<scope>runtime</scope>
</scopes>
<includes>
<include>org.slf4j</include>
<include>org.apache.commons</include>
Expand Down

0 comments on commit da1351a

Please sign in to comment.