Skip to content

Commit

Permalink
[MENFORCER-411] DependencyConvergence takes include/exclude parameter…
Browse files Browse the repository at this point in the history
…s to filter errors (#136)
  • Loading branch information
fdfea committed Dec 4, 2022
1 parent 40637c6 commit 34b923f
Show file tree
Hide file tree
Showing 15 changed files with 699 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ public class DependencyConvergence implements EnforcerRule {

private boolean uniqueVersions;

private List<String> includes;

private List<String> excludes;

public void setUniqueVersions(boolean uniqueVersions) {
this.uniqueVersions = uniqueVersions;
}
Expand Down Expand Up @@ -117,7 +121,7 @@ public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException {
visitor.setUniqueVersions(uniqueVersions);
node.accept(visitor);
List<CharSequence> errorMsgs = new ArrayList<>();
errorMsgs.addAll(getConvergenceErrorMsgs(visitor.getConflictedVersionNumbers()));
errorMsgs.addAll(getConvergenceErrorMsgs(visitor.getConflictedVersionNumbers(includes, excludes)));
for (CharSequence errorMsg : errorMsgs) {
log.warn(errorMsg);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public static Set<Artifact> checkDependencies(Set<Artifact> dependencies, List<S
* @return <code>true</code> if the artifact matches one of the patterns
* @throws EnforcerRuleException the enforcer rule exception
*/
private static boolean compareDependency(String pattern, Artifact artifact) throws EnforcerRuleException {
static boolean compareDependency(String pattern, Artifact artifact) throws EnforcerRuleException {

ArtifactMatcher.Pattern am = new Pattern(pattern);
boolean result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.shared.dependency.graph.DependencyNode;
import org.apache.maven.shared.dependency.graph.traversal.DependencyNodeVisitor;
Expand Down Expand Up @@ -118,13 +120,65 @@ private boolean containsConflicts(List<DependencyNode> nodes) {
return false;
}

public List<List<DependencyNode>> getConflictedVersionNumbers() {
public List<List<DependencyNode>> getConflictedVersionNumbers(List<String> includes, List<String> excludes)
throws EnforcerRuleException {
List<String> formattedIncludes = formatPatterns(includes);
List<String> formattedExcludes = formatPatterns(excludes);
List<List<DependencyNode>> output = new ArrayList<>();
for (List<DependencyNode> nodes : idsToNode.values()) {
if (containsConflicts(nodes)) {
output.add(nodes);
List<DependencyNode> filteredNodes = nodes;
if (formattedIncludes != null || formattedExcludes != null) {
filteredNodes = new ArrayList<>();
for (DependencyNode node : nodes) {
if (includeArtifact(node.getArtifact(), formattedIncludes, formattedExcludes)) {
filteredNodes.add(node);
}
}
}
if (containsConflicts(filteredNodes)) {
output.add(filteredNodes);
}
}
return output;
}

private static boolean includeArtifact(Artifact artifact, List<String> includes, List<String> excludes)
throws EnforcerRuleException {
boolean included = includes == null || includes.isEmpty();
if (!included) {
for (String pattern : includes) {
if (ArtifactUtils.compareDependency(pattern, artifact)) {
included = true;
break;
}
}
}
if (!included) {
return false;
}
boolean excluded = false;
if (excludes != null) {
for (String pattern : excludes) {
if (ArtifactUtils.compareDependency(pattern, artifact)) {
excluded = true;
break;
}
}
}
return !excluded;
}

private static List<String> formatPatterns(List<String> patterns) {
if (patterns == null) {
return null;
}
List<String> formattedPatterns = new ArrayList<>();
for (String pattern : patterns) {
String[] subStrings = pattern.split(":");
subStrings = StringUtils.stripAll(subStrings);
String formattedPattern = StringUtils.join(subStrings, ":");
formattedPatterns.add(formattedPattern);
}
return formattedPatterns;
}
}
31 changes: 30 additions & 1 deletion enforcer-rules/src/site/apt/dependencyConvergence.apt.vm
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ and
</project>
+-----------------------------------------------------------------------------------

* Timestamped version
* Timestamped Version

By default the non-unique versions are matched, which means the <<<X.Y-SNAPSHOT>>> instead of the timestamped versions.
If you want to use the unique versions of the dependencies, you can set its property to <<<true>>>.
Expand All @@ -128,3 +128,32 @@ and
</dependencyConvergence>
+---------------------------------------------

* Filtering Dependency Errors

By default, all dependency convergence errors are reported, and any single error will fail the build. If you want
to tune which dependency errors are reported and fail the build, you can add the following optional parameters:

* includes - A list of artifacts for which dependency convergence should be enforced. Not specifying any includes
is interpreted the same as including all artifacts.

* excludes - A list of artifacts for which dependency convergence should not be enforced. These are exceptions
to the includes.

[]

The format for artifacts is groupId[:artifactId][:version][:type][:scope][:classifier] where artifactId, version,
type, scope and classifier are optional. Wildcards may be used to replace an entire section or just parts of a
section. This rule uses the {{{./versionRanges.html}Enforcer version range syntax}} to define allowed versions.

+---------------------------------------------
<dependencyConvergence>
<includes>
<include>org.slf4j</include>
<include>org.apache.commons</include>
</includes>
<excludes>
<exclude>org.slf4j:slf4j-jdk14</exclude>
<exclude>org.apache.commons:*:[3.4]</exclude>
</excludes>
</dependencyConvergence>
+---------------------------------------------
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,79 @@
<?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>
</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>
<!-- if everything is included, return any errors that are not excluded -->
<dependencyConvergence>
<includes>
<include>*</include>
</includes>
<excludes>
<exclude>org.apache.commons</exclude>
<exclude>org.slf4j:slf4j-api:[1.6.2]</exclude>
</excludes>
</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,78 @@
<?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>
</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>
<!-- if everything is included, but all conflicts are excluded, return no errors -->
<dependencyConvergence>
<includes>
<include>*</include>
</includes>
<excludes>
<excludes>org.slf4j:slf4j-api:[1.6.1,1.6.2]</excludes>
</excludes>
</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,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

0 comments on commit 34b923f

Please sign in to comment.