Skip to content

Commit

Permalink
MENFORCER-466: Apply the all levels (so, including direct) scope and …
Browse files Browse the repository at this point in the history
…optional selectors on RequireUpperBoundDeps
  • Loading branch information
jarmoniuk committed Mar 16, 2023
1 parent da1351a commit b9ea602
Show file tree
Hide file tree
Showing 12 changed files with 355 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@
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.dependency.selector.AllLevelsOptionalDependencySelector;
import org.apache.maven.enforcer.rules.dependency.selector.AllLevelsScopeDependencySelector;
import org.apache.maven.enforcer.rules.utils.ArtifactUtils;
import org.eclipse.aether.collection.DependencyCollectionContext;
import org.eclipse.aether.collection.DependencySelector;
import org.eclipse.aether.graph.Dependency;
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 @@ -49,8 +49,6 @@ 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 @@ -64,23 +62,8 @@ public DependencyConvergence(ResolveUtil resolveUtil) {
public void execute() throws EnforcerRuleException {

DependencyNode node = resolveUtil.resolveTransitiveDependencies(
// TODO: use a modified version of ExclusionDependencySelector to process excludes and includes
new DependencySelector() {
@Override
public boolean selectDependency(Dependency dependency) {
// regular OptionalDependencySelector only discriminates optional dependencies at level 2+
return !dependency.isOptional()
// regular scope selectors only discard transitive dependencies
// and always allow direct dependencies
&& scopes.contains(dependency.getScope());
}

@Override
public DependencySelector deriveChildSelector(DependencyCollectionContext context) {
return this;
}
},
// process dependency exclusions
new AllLevelsOptionalDependencySelector(),
new AllLevelsScopeDependencySelector(SCOPE_TEST, SCOPE_PROVIDED),
new ExclusionDependencySelector());
dependencyVersionMap = new DependencyVersionMap().setUniqueVersions(uniqueVersions);
node.accept(dependencyVersionMap);
Expand Down Expand Up @@ -142,7 +125,7 @@ private String buildConvergenceErrorMsg(List<DependencyNode> nodeList) {
@Override
public String toString() {
return String.format(
"DependencyConvergence[includes=%s, excludes=%s, uniqueVersions=%b, scopes=%s]",
includes, excludes, uniqueVersions, String.join(",", scopes));
"DependencyConvergence[includes=%s, excludes=%s, uniqueVersions=%b]",
includes, excludes, uniqueVersions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,18 @@
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;

/**
* Rule to enforce that the resolved dependency is also the most recent one of all transitive dependencies.
Expand Down Expand Up @@ -97,7 +103,10 @@ public void setIncludes(List<String> includes) {

@Override
public void execute() throws EnforcerRuleException {
DependencyNode node = resolveUtil.resolveTransitiveDependencies();
DependencyNode node = resolveUtil.resolveTransitiveDependencies(
new AllLevelsOptionalDependencySelector(),
new AllLevelsScopeDependencySelector(SCOPE_TEST, SCOPE_PROVIDED),
new ExclusionDependencySelector());
upperBoundDepsVisitor = new RequireUpperBoundDepsVisitor()
.setUniqueVersions(uniqueVersions)
.setIncludes(includes);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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 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 String[] excluded;

public AllLevelsScopeDependencySelector(String... excluded) {
this.excluded = excluded;
}

@Override
public boolean selectDependency(Dependency dependency) {
return Arrays.stream(excluded)
.map(scope -> !scope.equals(dependency.getScope()))
.reduce(Boolean::logicalAnd)
.orElse(true);
}

@Override
public DependencySelector deriveChildSelector(DependencyCollectionContext context) {
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +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.Mockito.when;

@ExtendWith(MockitoExtension.class)
Expand All @@ -41,7 +42,7 @@ class RequireUpperBoundDepsTest {
@Test
void testRule() throws Exception {

when(resolveUtil.resolveTransitiveDependencies())
when(resolveUtil.resolveTransitiveDependencies(any(), any(), any()))
.thenReturn(new DependencyNodeBuilder()
.withType(DependencyNodeBuilder.Type.POM)
.withChildNode(new DependencyNodeBuilder()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
* 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.
*
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.maven.plugins.enforcer.its</groupId>
<artifactId>menforcer466_requires_api150</artifactId>
<version>1.0</version>

<dependencies>
<dependency>
<groupId>org.apache.maven.plugins.enforcer.its</groupId>
<artifactId>menforcer128_api</artifactId>
<version>1.5.0</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
* 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.
*
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.maven.plugins.enforcer.its</groupId>
<artifactId>menforcer466_requires_api150_optional</artifactId>
<version>1.0</version>

<dependencies>
<dependency>
<groupId>org.apache.maven.plugins.enforcer.its</groupId>
<artifactId>menforcer128_api</artifactId>
<version>1.5.0</version>
<optional>true</optional>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
* 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.
*
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.maven.plugins.enforcer.its</groupId>
<artifactId>menforcer466_requires_api160_provided</artifactId>
<version>1.0</version>

<dependencies>
<dependency>
<groupId>org.apache.maven.plugins.enforcer.its</groupId>
<artifactId>menforcer128_api</artifactId>
<version>1.6.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
* 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.
*
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.maven.plugins.enforcer.its</groupId>
<artifactId>menforcer466_requires_utils30</artifactId>
<version>1.0</version>

<dependencies>
<dependency>
<groupId>org.apache.maven.plugins.enforcer.its</groupId>
<artifactId>menforcer138_utils</artifactId>
<version>3.0</version>
</dependency>
</dependencies>
</project>

0 comments on commit b9ea602

Please sign in to comment.