This is an automated email from the ASF dual-hosted git repository.

sjaranowski pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-enforcer.git


The following commit(s) were added to refs/heads/master by this push:
     new c96a6fa  [MENFORCER-470] Configurable scopes for DependencyConvergence 
rule - refactor
c96a6fa is described below

commit c96a6fac0283e343567adec0ff2f6f6c1c13142e
Author: Slawomir Jaranowski <s.jaranow...@gmail.com>
AuthorDate: Sat Mar 18 12:14:09 2023 +0100

    [MENFORCER-470] Configurable scopes for DependencyConvergence rule - 
refactor
---
 .../rules/dependency/DependencyConvergence.java    | 23 ++++---
 .../selector/AllLevelsScopeDependencySelector.java | 13 ++++
 .../src/site/apt/dependencyConvergence.apt.vm      |  8 +--
 .../invoker.properties                             | 18 +++++
 .../dependency-convergence_excludedScopes/pom.xml  | 77 ++++++++++++++++++++++
 .../verify.groovy                                  | 27 ++++++++
 6 files changed, 153 insertions(+), 13 deletions(-)

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 21cac73..13e22c6 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
@@ -22,6 +22,7 @@ import javax.inject.Inject;
 import javax.inject.Named;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
 import java.util.Objects;
@@ -43,12 +44,18 @@ import static org.apache.maven.artifact.Artifact.SCOPE_TEST;
 @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;
@@ -63,19 +70,17 @@ public final class DependencyConvergence extends 
AbstractStandardEnforcerRule {
 
         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));
         }
     }
 
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
index 7490edf..ab59eac 100644
--- 
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
@@ -21,6 +21,7 @@ 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;
@@ -34,10 +35,22 @@ import org.eclipse.aether.graph.Dependency;
 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());
diff --git a/enforcer-rules/src/site/apt/dependencyConvergence.apt.vm 
b/enforcer-rules/src/site/apt/dependencyConvergence.apt.vm
index 805c7df..99f473d 100644
--- a/enforcer-rules/src/site/apt/dependencyConvergence.apt.vm
+++ b/enforcer-rules/src/site/apt/dependencyConvergence.apt.vm
@@ -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>>>.
 
     []
 
@@ -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>
diff --git 
a/maven-enforcer-plugin/src/it/projects/dependency-convergence_excludedScopes/invoker.properties
 
b/maven-enforcer-plugin/src/it/projects/dependency-convergence_excludedScopes/invoker.properties
new file mode 100644
index 0000000..66b78c0
--- /dev/null
+++ 
b/maven-enforcer-plugin/src/it/projects/dependency-convergence_excludedScopes/invoker.properties
@@ -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
diff --git 
a/maven-enforcer-plugin/src/it/projects/dependency-convergence_excludedScopes/pom.xml
 
b/maven-enforcer-plugin/src/it/projects/dependency-convergence_excludedScopes/pom.xml
new file mode 100644
index 0000000..e09ea48
--- /dev/null
+++ 
b/maven-enforcer-plugin/src/it/projects/dependency-convergence_excludedScopes/pom.xml
@@ -0,0 +1,77 @@
+<?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>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-nop</artifactId>
+            <version>1.6.0</version>
+            <scope>test</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>
diff --git 
a/maven-enforcer-plugin/src/it/projects/dependency-convergence_excludedScopes/verify.groovy
 
b/maven-enforcer-plugin/src/it/projects/dependency-convergence_excludedScopes/verify.groovy
new file mode 100644
index 0000000..d0e7395
--- /dev/null
+++ 
b/maven-enforcer-plugin/src/it/projects/dependency-convergence_excludedScopes/verify.groovy
@@ -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:provided' )
+assert !buildLog.contains( 'org.slf4j:slf4j-api:jar:1.6.0' )
+

Reply via email to