Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MENFORCER-466] Apply the all levels scope and optional selectors on RequireUpperBoundDeps #254

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:[email protected]">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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a new parameter according to: MENFORCER-470

But ok - I will restore it as exludesScopes


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.resolveTransitiveDependenciesVerbose(
// 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.resolveTransitiveDependenciesVerbose();
DependencyNode node = resolveUtil.resolveTransitiveDependenciesVerbose(
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,50 @@
/*
* 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 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<String> excluded;

public AllLevelsScopeDependencySelector(String... excluded) {
this.excluded = excluded != null ? Arrays.asList(excluded) : Collections.emptyList();
}

@Override
public boolean selectDependency(Dependency dependency) {
return !excluded.contains(dependency.getScope());
}

@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.resolveTransitiveDependenciesVerbose())
when(resolveUtil.resolveTransitiveDependenciesVerbose(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,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>
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,10 @@
-->
<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>menforcer128</artifactId>
<groupId>org.apache.maven.enforcer.its</groupId>
<artifactId>require-upper-bound-dependencies-provided</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.apache.maven.plugins.enforcer.its</groupId>
<artifactId>menforcer128_api</artifactId>
<version>1.4.0</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
Expand All @@ -41,17 +33,45 @@
<executions>
<execution>
<id>enforce</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<RequireUpperBoundDeps/>
<requireUpperBoundDeps/>
</rules>
</configuration>
<goals>
<goal>enforce</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.apache.maven.plugins.enforcer.its</groupId>
<artifactId>menforcer128_api</artifactId>
<version>1.4.0</version>
<scope>provided</scope>
</dependency>

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

<dependency>
<groupId>org.apache.maven.plugins.enforcer.its</groupId>
<artifactId>menforcer466_requires_api150</artifactId>
<version>1.0</version>
</dependency>

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

This file was deleted.