Skip to content

Commit

Permalink
[MENFORCER-469] Fix banTransitiveDependencies and transitive dependen…
Browse files Browse the repository at this point in the history
…cies with another version than the resolved one

- rename ResolveUtil to ResolverUtil

- introduce methods:

 - resolveTransitiveDependenciesVerbose
   resolve with full tree contains all dependencies - also conflicted

 - resolveTransitiveDependencies
   resolve final tree contains dependencies after conflict resolved
  • Loading branch information
slawekjaranowski committed Mar 11, 2023
1 parent 6b62f03 commit 8ddf0c0
Show file tree
Hide file tree
Showing 12 changed files with 213 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ public final class BanTransitiveDependencies extends AbstractStandardEnforcerRul

private final MavenSession session;

private final ResolveUtil resolveUtil;
private final ResolverUtil resolverUtil;

@Inject
public BanTransitiveDependencies(MavenSession session, ResolveUtil resolveUtil) {
public BanTransitiveDependencies(MavenSession session, ResolverUtil resolverUtil) {
this.session = Objects.requireNonNull(session);
this.resolveUtil = Objects.requireNonNull(resolveUtil);
this.resolverUtil = Objects.requireNonNull(resolverUtil);
}

/**
Expand Down Expand Up @@ -147,7 +147,7 @@ public void execute() throws EnforcerRuleException {
.map(d -> RepositoryUtils.toDependency(d, artifactTypeRegistry))
.collect(Collectors.toSet());

DependencyNode rootNode = resolveUtil.resolveTransitiveDependencies();
DependencyNode rootNode = resolverUtil.resolveTransitiveDependencies();
StringBuilder generatedMessage = new StringBuilder();
if (searchTree(rootNode, 0, exclusions, directDependencies, generatedMessage)) {
throw new EnforcerRuleException(ofNullable(getMessage()).orElse(generatedMessage.toString()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
public final class BannedDependencies extends BannedDependenciesBase {

@Inject
BannedDependencies(MavenSession session, ResolveUtil resolveUtil) {
super(session, resolveUtil);
BannedDependencies(MavenSession session, ResolverUtil resolverUtil) {
super(session, resolverUtil);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ abstract class BannedDependenciesBase extends AbstractStandardEnforcerRule {

private final MavenSession session;

private final ResolveUtil resolveUtil;
private final ResolverUtil resolverUtil;

BannedDependenciesBase(MavenSession session, ResolveUtil resolveUtil) {
BannedDependenciesBase(MavenSession session, ResolverUtil resolverUtil) {
this.session = Objects.requireNonNull(session);
this.resolveUtil = Objects.requireNonNull(resolveUtil);
this.resolverUtil = Objects.requireNonNull(resolverUtil);
}

protected MavenSession getSession() {
Expand Down Expand Up @@ -102,7 +102,7 @@ public void execute() throws EnforcerRuleException {
}
} else {
StringBuilder messageBuilder = new StringBuilder();
DependencyNode rootNode = resolveUtil.resolveTransitiveDependencies();
DependencyNode rootNode = resolverUtil.resolveTransitiveDependenciesVerbose();
if (!validate(rootNode, 0, messageBuilder)) {
String message = "";
if (getMessage() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,17 @@ public final class DependencyConvergence extends AbstractStandardEnforcerRule {

private DependencyVersionMap dependencyVersionMap;

private final ResolveUtil resolveUtil;
private final ResolverUtil resolverUtil;

@Inject
public DependencyConvergence(ResolveUtil resolveUtil) {
this.resolveUtil = Objects.requireNonNull(resolveUtil);
public DependencyConvergence(ResolverUtil resolverUtil) {
this.resolverUtil = Objects.requireNonNull(resolverUtil);
}

@Override
public void execute() throws EnforcerRuleException {

DependencyNode node = resolveUtil.resolveTransitiveDependencies(
DependencyNode node = resolverUtil.resolveTransitiveDependenciesVerbose(
// TODO: use a modified version of ExclusionDependencySelector to process excludes and includes
new DependencySelector() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public final class RequireReleaseDeps extends BannedDependenciesBase {
private boolean failWhenParentIsSnapshot = true;

@Inject
public RequireReleaseDeps(MavenSession session, ResolveUtil resolveUtil) {
super(session, resolveUtil);
public RequireReleaseDeps(MavenSession session, ResolverUtil resolverUtil) {
super(session, resolverUtil);
}

// Override parent to allow optional ignore of this rule.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ public final class RequireUpperBoundDeps extends AbstractStandardEnforcerRule {

private RequireUpperBoundDepsVisitor upperBoundDepsVisitor;

private final ResolveUtil resolveUtil;
private final ResolverUtil resolverUtil;

@Inject
public RequireUpperBoundDeps(ResolveUtil resolveUtil) {
this.resolveUtil = Objects.requireNonNull(resolveUtil);
public RequireUpperBoundDeps(ResolverUtil resolverUtil) {
this.resolverUtil = Objects.requireNonNull(resolverUtil);
}

/**
Expand All @@ -97,7 +97,7 @@ public void setIncludes(List<String> includes) {

@Override
public void execute() throws EnforcerRuleException {
DependencyNode node = resolveUtil.resolveTransitiveDependencies();
DependencyNode node = resolverUtil.resolveTransitiveDependenciesVerbose();
upperBoundDepsVisitor = new RequireUpperBoundDepsVisitor()
.setUniqueVersions(uniqueVersions)
.setIncludes(includes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,33 +39,47 @@
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.selector.ExclusionDependencySelector;
import org.eclipse.aether.util.graph.selector.OptionalDependencySelector;
import org.eclipse.aether.util.graph.selector.ScopeDependencySelector;
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.
*/
@Named
class ResolveUtil {
class ResolverUtil {

private final RepositorySystem repositorySystem;

private final MavenSession session;

/**
* Default constructor
*/
@Inject
ResolveUtil(RepositorySystem repositorySystem, MavenSession session) {
ResolverUtil(RepositorySystem repositorySystem, MavenSession session) {
this.repositorySystem = Objects.requireNonNull(repositorySystem);
this.session = Objects.requireNonNull(session);
}

/**
* Retrieves the {@link DependencyNode} instance containing the result of the transitive dependency
* for the current {@link MavenProject} in verbose mode.
* <p>
* In verbose mode all nodes participating in a conflict are retained.
* </p>
* <p>
* Please consult {@link ConflictResolver} and {@link DependencyManagerUtils}>
* /p>
*
* @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 resolveTransitiveDependenciesVerbose(DependencySelector... selectors) throws EnforcerRuleException {
return resolveTransitiveDependencies(true, selectors);
}

/**
* Retrieves the {@link DependencyNode} instance containing the result of the transitive dependency
* for the current {@link MavenProject}.
Expand All @@ -75,23 +89,28 @@ class ResolveUtil {
* @throws EnforcerRuleException thrown if the lookup fails
*/
DependencyNode resolveTransitiveDependencies(DependencySelector... selectors) throws EnforcerRuleException {
if (selectors.length == 0) {
selectors = new DependencySelector[] {
new ScopeDependencySelector(SCOPE_TEST, SCOPE_PROVIDED),
new OptionalDependencySelector(),
new ExclusionDependencySelector()
};
}
return resolveTransitiveDependencies(false, selectors);
}

private DependencyNode resolveTransitiveDependencies(boolean verbose, DependencySelector... selectors)
throws EnforcerRuleException {

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

DefaultRepositorySystemSession repositorySystemSession =
new DefaultRepositorySystemSession(session.getRepositorySession());
repositorySystemSession.setConfigProperty(ConflictResolver.CONFIG_PROP_VERBOSE, true);
repositorySystemSession.setConfigProperty(DependencyManagerUtils.CONFIG_PROP_VERBOSE, true);
repositorySystemSession.setDependencySelector(new AndDependencySelector(selectors));

if (selectors.length > 0) {
repositorySystemSession.setDependencySelector(new AndDependencySelector(selectors));
}

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

CollectRequest collectRequest = new CollectRequest(
project.getDependencies().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class BannedDependenciesTest {
private MavenSession session;

@Mock
private ResolveUtil resolveUtil;
private ResolverUtil resolverUtil;

@InjectMocks
private BannedDependencies rule;
Expand Down Expand Up @@ -94,7 +94,7 @@ void excludesAndIncludesDoNotUseTransitiveDependencies() throws Exception {
@Test
void excludesUseTransitiveDependencies() throws Exception {

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

when(resolveUtil.resolveTransitiveDependencies())
when(resolverUtil.resolveTransitiveDependenciesVerbose())
.thenReturn(new DependencyNodeBuilder()
.withType(DependencyNodeBuilder.Type.POM)
.withChildNode(new DependencyNodeBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class RequireReleaseDepsTest {
private MavenSession session;

@Mock
private ResolveUtil resolveUtil;
private ResolverUtil resolverUtil;

@InjectMocks
private RequireReleaseDeps rule;
Expand All @@ -75,12 +75,12 @@ void testSearchNonTransitive() throws IOException {

assertThatCode(rule::execute).doesNotThrowAnyException();

verifyNoInteractions(resolveUtil);
verifyNoInteractions(resolverUtil);
}

@Test
void testSearchTransitiveMultipleFailures() throws Exception {
when(resolveUtil.resolveTransitiveDependencies()).thenReturn(getDependencyNodeWithMultipleSnapshots());
when(resolverUtil.resolveTransitiveDependenciesVerbose()).thenReturn(getDependencyNodeWithMultipleSnapshots());
rule.setSearchTransitive(true);

assertThatCode(rule::execute)
Expand All @@ -94,7 +94,7 @@ void testSearchTransitiveMultipleFailures() throws Exception {
@Test
void testSearchTransitiveNoFailures() throws Exception {
when(session.getCurrentProject()).thenReturn(project);
when(resolveUtil.resolveTransitiveDependencies()).thenReturn(new DependencyNodeBuilder().build());
when(resolverUtil.resolveTransitiveDependenciesVerbose()).thenReturn(new DependencyNodeBuilder().build());

rule.setSearchTransitive(true);
assertThatCode(rule::execute).doesNotThrowAnyException();
Expand All @@ -108,13 +108,14 @@ void testShouldFailOnlyWhenRelease() throws Exception {

assertThatCode(rule::execute).doesNotThrowAnyException();

verifyNoInteractions(resolveUtil);
verifyNoInteractions(resolverUtil);
}

@Test
void testWildcardExcludeTests() throws Exception {
when(session.getCurrentProject()).thenReturn(project);
when(resolveUtil.resolveTransitiveDependencies()).thenReturn(getDependencyNodeWithMultipleTestSnapshots());
when(resolverUtil.resolveTransitiveDependenciesVerbose())
.thenReturn(getDependencyNodeWithMultipleTestSnapshots());

rule.setExcludes(Collections.singletonList("*:*:*:*:test"));
rule.setSearchTransitive(true);
Expand All @@ -125,7 +126,8 @@ void testWildcardExcludeTests() throws Exception {
@Test
void testWildcardExcludeAll() throws Exception {
when(session.getCurrentProject()).thenReturn(project);
when(resolveUtil.resolveTransitiveDependencies()).thenReturn(getDependencyNodeWithMultipleTestSnapshots());
when(resolverUtil.resolveTransitiveDependenciesVerbose())
.thenReturn(getDependencyNodeWithMultipleTestSnapshots());

rule.setExcludes(Collections.singletonList("*"));
rule.setSearchTransitive(true);
Expand All @@ -135,7 +137,8 @@ void testWildcardExcludeAll() throws Exception {

@Test
void testExcludesAndIncludes() throws Exception {
when(resolveUtil.resolveTransitiveDependencies()).thenReturn(getDependencyNodeWithMultipleTestSnapshots());
when(resolverUtil.resolveTransitiveDependenciesVerbose())
.thenReturn(getDependencyNodeWithMultipleTestSnapshots());

rule.setExcludes(Collections.singletonList("*"));
rule.setIncludes(Collections.singletonList("*:*:*:*:test"));
Expand All @@ -161,7 +164,7 @@ void testId() {
void testFailWhenParentIsSnapshot() throws Exception {
when(session.getCurrentProject()).thenReturn(project);
when(project.getParentArtifact()).thenReturn(ARTIFACT_STUB_FACTORY.getSnapshotArtifact());
when(resolveUtil.resolveTransitiveDependencies()).thenReturn(new DependencyNodeBuilder().build());
when(resolverUtil.resolveTransitiveDependenciesVerbose()).thenReturn(new DependencyNodeBuilder().build());

rule.setFailWhenParentIsSnapshot(true);

Expand All @@ -174,7 +177,7 @@ void testFailWhenParentIsSnapshot() throws Exception {
void parentShouldBeExcluded() throws Exception {
when(session.getCurrentProject()).thenReturn(project);
when(project.getParentArtifact()).thenReturn(ARTIFACT_STUB_FACTORY.getSnapshotArtifact());
when(resolveUtil.resolveTransitiveDependencies()).thenReturn(new DependencyNodeBuilder().build());
when(resolverUtil.resolveTransitiveDependenciesVerbose()).thenReturn(new DependencyNodeBuilder().build());

rule.setFailWhenParentIsSnapshot(true);
rule.setExcludes(Collections.singletonList("testGroupId:*"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@
class RequireUpperBoundDepsTest {

@Mock
private ResolveUtil resolveUtil;
private ResolverUtil resolverUtil;

@InjectMocks
private RequireUpperBoundDeps rule;

@Test
void testRule() throws Exception {

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

0 comments on commit 8ddf0c0

Please sign in to comment.