diff --git a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/dependency/BannedDependenciesBase.java b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/dependency/BannedDependenciesBase.java index 90691c29..bed32d11 100644 --- a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/dependency/BannedDependenciesBase.java +++ b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/dependency/BannedDependenciesBase.java @@ -18,6 +18,7 @@ */ package org.apache.maven.enforcer.rules.dependency; +import java.util.Collections; import java.util.List; import java.util.Objects; @@ -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) { diff --git a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/dependency/DependencyConvergence.java b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/dependency/DependencyConvergence.java index 13e22c6c..0c36df7f 100644 --- a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/dependency/DependencyConvergence.java +++ b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/dependency/DependencyConvergence.java @@ -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; @@ -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); diff --git a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/dependency/RequireUpperBoundDeps.java b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/dependency/RequireUpperBoundDeps.java index 50e054b5..04e35f06 100644 --- a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/dependency/RequireUpperBoundDeps.java +++ b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/dependency/RequireUpperBoundDeps.java @@ -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; @@ -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; @@ -103,10 +101,8 @@ public void setIncludes(List 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); diff --git a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/dependency/ResolveUtil.java b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/dependency/ResolveUtil.java index 19d4a204..bb0179f0 100644 --- a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/dependency/ResolveUtil.java +++ b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/dependency/ResolveUtil.java @@ -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. @@ -72,59 +75,59 @@ class ResolveUtil { * Please consult {@link ConflictResolver} and {@link DependencyManagerUtils}> *

* - * @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 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 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 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 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) diff --git a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/dependency/selector/AllLevelsOptionalDependencySelector.java b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/dependency/selector/AllLevelsOptionalDependencySelector.java deleted file mode 100644 index 4c9e2ad6..00000000 --- a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/dependency/selector/AllLevelsOptionalDependencySelector.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.maven.enforcer.rules.dependency.selector; - -import org.eclipse.aether.collection.DependencyCollectionContext; -import org.eclipse.aether.collection.DependencySelector; -import org.eclipse.aether.graph.Dependency; - -/** - * Dependency selector discarding {@code optional} dependencies on all levels. - * The standard {@link org.eclipse.aether.util.graph.selector.OptionalDependencySelector} - * does not discard direct dependencies. - */ -public class AllLevelsOptionalDependencySelector implements DependencySelector { - @Override - public boolean selectDependency(Dependency dependency) { - return !dependency.isOptional(); - } - - @Override - public DependencySelector deriveChildSelector(DependencyCollectionContext context) { - return this; - } -} diff --git a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/dependency/selector/AllLevelsScopeDependencySelector.java b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/dependency/selector/AllLevelsScopeDependencySelector.java deleted file mode 100644 index ab59eac7..00000000 --- a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/dependency/selector/AllLevelsScopeDependencySelector.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.maven.enforcer.rules.dependency.selector; - -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.List; - -import org.eclipse.aether.collection.DependencyCollectionContext; -import org.eclipse.aether.collection.DependencySelector; -import org.eclipse.aether.graph.Dependency; - -/** - * Dependency selector discarding dependencies with the given scope on all levels. - * The standard {@link org.eclipse.aether.util.graph.selector.ScopeDependencySelector} - * does not discard direct dependencies. - */ -public class AllLevelsScopeDependencySelector implements DependencySelector { - private final Collection excluded; - - /** - * A constructor for selector - * @param excluded list of excluded scopes - */ - public AllLevelsScopeDependencySelector(String... excluded) { - this.excluded = excluded != null ? Arrays.asList(excluded) : Collections.emptyList(); - } - - /** - * A constructor for selector - * @param excluded list of excluded scopes - */ - public AllLevelsScopeDependencySelector(List excluded) { - this.excluded = excluded; - } - - @Override - public boolean selectDependency(Dependency dependency) { - return !excluded.contains(dependency.getScope()); - } - - @Override - public DependencySelector deriveChildSelector(DependencyCollectionContext context) { - return this; - } -} diff --git a/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/dependency/BannedDependenciesTest.java b/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/dependency/BannedDependenciesTest.java index 18883573..b5d7c814 100644 --- a/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/dependency/BannedDependenciesTest.java +++ b/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/dependency/BannedDependenciesTest.java @@ -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; /** @@ -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() @@ -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() diff --git a/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/dependency/RequireReleaseDepsTest.java b/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/dependency/RequireReleaseDepsTest.java index 5a0b76f8..b3377ed3 100644 --- a/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/dependency/RequireReleaseDepsTest.java +++ b/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/dependency/RequireReleaseDepsTest.java @@ -37,6 +37,7 @@ import static org.apache.maven.enforcer.rules.EnforcerTestUtils.getDependencyNodeWithMultipleTestSnapshots; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatCode; +import static org.mockito.ArgumentMatchers.anyList; import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.when; @@ -80,7 +81,8 @@ void testSearchNonTransitive() throws IOException { @Test void testSearchTransitiveMultipleFailures() throws Exception { - when(resolveUtil.resolveTransitiveDependenciesVerbose()).thenReturn(getDependencyNodeWithMultipleSnapshots()); + when(resolveUtil.resolveTransitiveDependenciesVerbose(anyList())) + .thenReturn(getDependencyNodeWithMultipleSnapshots()); rule.setSearchTransitive(true); assertThatCode(rule::execute) @@ -94,7 +96,8 @@ void testSearchTransitiveMultipleFailures() throws Exception { @Test void testSearchTransitiveNoFailures() throws Exception { when(session.getCurrentProject()).thenReturn(project); - when(resolveUtil.resolveTransitiveDependenciesVerbose()).thenReturn(new DependencyNodeBuilder().build()); + when(resolveUtil.resolveTransitiveDependenciesVerbose(anyList())) + .thenReturn(new DependencyNodeBuilder().build()); rule.setSearchTransitive(true); assertThatCode(rule::execute).doesNotThrowAnyException(); @@ -114,7 +117,7 @@ void testShouldFailOnlyWhenRelease() throws Exception { @Test void testWildcardExcludeTests() throws Exception { when(session.getCurrentProject()).thenReturn(project); - when(resolveUtil.resolveTransitiveDependenciesVerbose()) + when(resolveUtil.resolveTransitiveDependenciesVerbose(anyList())) .thenReturn(getDependencyNodeWithMultipleTestSnapshots()); rule.setExcludes(Collections.singletonList("*:*:*:*:test")); @@ -126,7 +129,7 @@ void testWildcardExcludeTests() throws Exception { @Test void testWildcardExcludeAll() throws Exception { when(session.getCurrentProject()).thenReturn(project); - when(resolveUtil.resolveTransitiveDependenciesVerbose()) + when(resolveUtil.resolveTransitiveDependenciesVerbose(anyList())) .thenReturn(getDependencyNodeWithMultipleTestSnapshots()); rule.setExcludes(Collections.singletonList("*")); @@ -137,7 +140,7 @@ void testWildcardExcludeAll() throws Exception { @Test void testExcludesAndIncludes() throws Exception { - when(resolveUtil.resolveTransitiveDependenciesVerbose()) + when(resolveUtil.resolveTransitiveDependenciesVerbose(anyList())) .thenReturn(getDependencyNodeWithMultipleTestSnapshots()); rule.setExcludes(Collections.singletonList("*")); @@ -164,7 +167,8 @@ void testId() { void testFailWhenParentIsSnapshot() throws Exception { when(session.getCurrentProject()).thenReturn(project); when(project.getParentArtifact()).thenReturn(ARTIFACT_STUB_FACTORY.getSnapshotArtifact()); - when(resolveUtil.resolveTransitiveDependenciesVerbose()).thenReturn(new DependencyNodeBuilder().build()); + when(resolveUtil.resolveTransitiveDependenciesVerbose(anyList())) + .thenReturn(new DependencyNodeBuilder().build()); rule.setFailWhenParentIsSnapshot(true); @@ -177,7 +181,8 @@ void testFailWhenParentIsSnapshot() throws Exception { void parentShouldBeExcluded() throws Exception { when(session.getCurrentProject()).thenReturn(project); when(project.getParentArtifact()).thenReturn(ARTIFACT_STUB_FACTORY.getSnapshotArtifact()); - when(resolveUtil.resolveTransitiveDependenciesVerbose()).thenReturn(new DependencyNodeBuilder().build()); + when(resolveUtil.resolveTransitiveDependenciesVerbose(anyList())) + .thenReturn(new DependencyNodeBuilder().build()); rule.setFailWhenParentIsSnapshot(true); rule.setExcludes(Collections.singletonList("testGroupId:*")); diff --git a/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/dependency/RequireUpperBoundDepsTest.java b/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/dependency/RequireUpperBoundDepsTest.java index ac66f0d0..07a56df1 100644 --- a/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/dependency/RequireUpperBoundDepsTest.java +++ b/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/dependency/RequireUpperBoundDepsTest.java @@ -27,7 +27,7 @@ import org.mockito.junit.jupiter.MockitoExtension; import static org.assertj.core.api.Assertions.assertThatCode; -import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyList; import static org.mockito.Mockito.when; @ExtendWith(MockitoExtension.class) @@ -42,7 +42,7 @@ class RequireUpperBoundDepsTest { @Test void testRule() throws Exception { - when(resolveUtil.resolveTransitiveDependenciesVerbose(any(), any(), any())) + when(resolveUtil.resolveTransitiveDependenciesVerbose(anyList())) .thenReturn(new DependencyNodeBuilder() .withType(DependencyNodeBuilder.Type.POM) .withChildNode(new DependencyNodeBuilder()