Skip to content

Commit

Permalink
[MENFORCER-470] Configurable scopes for DependencyConvergence rule - …
Browse files Browse the repository at this point in the history
…refactor
  • Loading branch information
slawekjaranowski committed Mar 18, 2023
1 parent fb7f749 commit e645711
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import javax.inject.Named;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
Expand All @@ -43,12 +44,18 @@
@Named("dependencyConvergence")
public final class DependencyConvergence extends AbstractStandardEnforcerRule {

// parameters

private boolean uniqueVersions;

private List<String> includes;

private List<String> excludes;

private List<String> excludedScopes = Arrays.asList(SCOPE_TEST, SCOPE_PROVIDED);

// parameters - end

private DependencyVersionMap dependencyVersionMap;

private final ResolveUtil resolveUtil;
Expand All @@ -63,19 +70,17 @@ public void execute() throws EnforcerRuleException {

DependencyNode node = resolveUtil.resolveTransitiveDependenciesVerbose(
new AllLevelsOptionalDependencySelector(),
new AllLevelsScopeDependencySelector(SCOPE_TEST, SCOPE_PROVIDED),
new AllLevelsScopeDependencySelector(excludedScopes),
new ExclusionDependencySelector());
dependencyVersionMap = new DependencyVersionMap().setUniqueVersions(uniqueVersions);
node.accept(dependencyVersionMap);

List<CharSequence> errorMsgs = new ArrayList<>(
getConvergenceErrorMsgs(dependencyVersionMap.getConflictedVersionNumbers(includes, excludes)));
for (CharSequence errorMsg : errorMsgs) {
getLog().warnOrError(errorMsg);
}
if (errorMsgs.size() > 0) {
throw new EnforcerRuleException(
"Failed while enforcing releasability. " + "See above detailed error message.");
List<String> errorMsgs =
getConvergenceErrorMsgs(dependencyVersionMap.getConflictedVersionNumbers(includes, excludes));

if (!errorMsgs.isEmpty()) {
throw new EnforcerRuleException("Failed while enforcing releasability." + System.lineSeparator()
+ String.join(System.lineSeparator(), errorMsgs));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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;
Expand All @@ -34,10 +35,22 @@
public class AllLevelsScopeDependencySelector implements DependencySelector {
private final Collection<String> 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<String> excluded) {
this.excluded = excluded;
}

@Override
public boolean selectDependency(Dependency dependency) {
return !excluded.contains(dependency.getScope());
Expand Down
8 changes: 4 additions & 4 deletions enforcer-rules/src/site/apt/dependencyConvergence.apt.vm
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ 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.
* <<excludedScopes>> - A list of scopes of artifacts for which dependency convergence should not be enforced.
Not specifying any scopes is interpreted as having the following scopes excluded: <<<provided>>>, <<<test>>>.

[]

Expand All @@ -150,10 +150,10 @@ and

+---------------------------------------------
<dependencyConvergence>
<scopes>
<excludedScopes>
<scope>compile</scope>
<scope>runtime</scope>
</scopes>
</excludedScopes>
<includes>
<include>org.slf4j</include>
<include>org.apache.commons</include>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# 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.

invoker.buildResult=failure
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?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.enforcer.its</groupId>
<artifactId>dependency-convergence</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.6.0</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<id>enforce</id>
<configuration>
<rules>
<dependencyConvergence>
<excludedScopes>
<!-- only test scope is excluded -->
<scope>test</scope>
</excludedScopes>
</dependencyConvergence>
</rules>
</configuration>
<goals>
<goal>enforce</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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.
*/

def buildLog = new File( basedir, 'build.log' ).text

assert buildLog.contains( '[ERROR] Rule 0: org.apache.maven.enforcer.rules.dependency.DependencyConvergence failed with message' )
assert buildLog.contains( 'Dependency convergence error for org.slf4j:slf4j-api:jar:1.6.2 paths to dependency are:' )
assert buildLog.contains( '+-org.slf4j:slf4j-api:jar:1.6.2:compile' )
assert buildLog.contains( '+-org.slf4j:slf4j-api:jar:1.6.1:compile' )
assert buildLog.contains( '+-org.slf4j:slf4j-api:jar:1.6.0:provided' )

0 comments on commit e645711

Please sign in to comment.