You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by rf...@apache.org on 2020/07/31 09:27:22 UTC

[maven-enforcer] branch master updated: [MENFORCER-358] requireUpperBounds deps should have includes

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

rfscholte 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 6599d49  [MENFORCER-358] requireUpperBounds deps should have includes
6599d49 is described below

commit 6599d49fbdddd41fe858c4553ab3fb724d3925b6
Author: Roy van Rijn <r....@portofrotterdam.com>
AuthorDate: Fri Jul 31 11:27:01 2020 +0200

    [MENFORCER-358] requireUpperBounds deps should have includes
    
    Signed-off-by: rfscholte <rf...@apache.org>
---
 .../plugins/enforcer/RequireUpperBoundDeps.java    | 51 +++++++++++---
 .../src/site/apt/requireUpperBoundDeps.apt.vm      |  7 +-
 .../it/mrm/repository/menforcer358_core-5.2.7.pom  | 26 +++++++
 .../it/mrm/repository/menforcer358_core-5.2.8.pom  | 26 +++++++
 .../mrm/repository/menforcer358_parent-5.2.7.pom   | 34 ++++++++++
 .../invoker.properties                             | 18 +++++
 .../module/pom.xml                                 | 30 ++++++++
 .../require-upper-bound-deps_includes/pom.xml      | 79 ++++++++++++++++++++++
 .../verify.groovy                                  | 32 +++++++++
 9 files changed, 291 insertions(+), 12 deletions(-)

diff --git a/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireUpperBoundDeps.java b/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireUpperBoundDeps.java
index 348d056..0a4ba38 100644
--- a/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireUpperBoundDeps.java
+++ b/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireUpperBoundDeps.java
@@ -47,7 +47,7 @@ import org.codehaus.plexus.component.repository.exception.ComponentLookupExcepti
 
 /**
  * Rule to enforce that the resolved dependency is also the most recent one of all transitive dependencies.
- * 
+ *
  * @author Geoffrey De Smet
  * @since 1.1
  */
@@ -69,9 +69,16 @@ public class RequireUpperBoundDeps
     private List<String> excludes = null;
 
     /**
+     * Dependencies to include.
+     *
+     * @since 3.0.0
+     */
+    private List<String> includes = null;
+
+    /**
      * Set to {@code true} if timestamped snapshots should be used.
-     * 
-     * @param uniqueVersions 
+     *
+     * @param uniqueVersions
      * @since 1.3
      */
     public void setUniqueVersions( boolean uniqueVersions )
@@ -88,13 +95,23 @@ public class RequireUpperBoundDeps
         this.excludes = excludes;
     }
 
+    /**
+     * Sets dependencies to include.
+     *
+     * @param includes a list of {@code groupId:artifactId} names
+     */
+    public void setIncludes( List<String> includes )
+    {
+        this.includes = includes;
+    }
+
     // CHECKSTYLE_OFF: LineLength
     /**
      * Uses the {@link EnforcerRuleHelper} to populate the values of the
      * {@link DependencyTreeBuilder#buildDependencyTree(MavenProject, ArtifactRepository, ArtifactFactory, ArtifactMetadataSource, ArtifactFilter, ArtifactCollector)}
      * factory method. <br/>
      * This method simply exists to hide all the ugly lookup that the {@link EnforcerRuleHelper} has to do.
-     * 
+     *
      * @param helper
      * @return a Dependency Node which is the root of the project's dependency tree
      * @throws EnforcerRuleException when the build should fail
@@ -144,6 +161,7 @@ public class RequireUpperBoundDeps
             DependencyNode node = getNode( helper );
             RequireUpperBoundDepsVisitor visitor = new RequireUpperBoundDepsVisitor();
             visitor.setUniqueVersions( uniqueVersions );
+            visitor.setIncludes( includes );
             node.accept( visitor );
             List<String> errorMessages = buildErrorMessages( visitor.getConflicts() );
             if ( errorMessages.size() > 0 )
@@ -180,8 +198,9 @@ public class RequireUpperBoundDeps
     private String buildErrorMessage( List<DependencyNode> conflict )
     {
         StringBuilder errorMessage = new StringBuilder();
-        errorMessage.append( System.lineSeparator() + "Require upper bound dependencies error for "
-            + getFullArtifactName( conflict.get( 0 ), false ) + " paths to dependency are:" + System.lineSeparator() );
+        errorMessage.append(
+                System.lineSeparator() + "Require upper bound dependencies error for " + getFullArtifactName(
+                        conflict.get( 0 ), false ) + " paths to dependency are:" + System.lineSeparator() );
         if ( conflict.size() > 0 )
         {
             errorMessage.append( buildTreeString( conflict.get( 0 ) ) );
@@ -201,13 +220,13 @@ public class RequireUpperBoundDeps
         while ( currentNode != null )
         {
             StringBuilder line = new StringBuilder( getFullArtifactName( currentNode, false ) );
-            
+
             if ( currentNode.getPremanagedVersion() != null )
             {
                 line.append( " (managed) <-- " );
                 line.append( getFullArtifactName( currentNode, true ) );
             }
-            
+
             loc.add( line.toString() );
             currentNode = currentNode.getParent();
         }
@@ -243,18 +262,30 @@ public class RequireUpperBoundDeps
 
         private boolean uniqueVersions;
 
+        private List<String> includes = null;
+
         public void setUniqueVersions( boolean uniqueVersions )
         {
             this.uniqueVersions = uniqueVersions;
         }
 
-        private Map<String, List<DependencyNodeHopCountPair>> keyToPairsMap =
-            new LinkedHashMap<>();
+        public void setIncludes( List<String> includes )
+        {
+            this.includes = includes;
+        }
+
+        private Map<String, List<DependencyNodeHopCountPair>> keyToPairsMap = new LinkedHashMap<>();
 
         public boolean visit( DependencyNode node )
         {
             DependencyNodeHopCountPair pair = new DependencyNodeHopCountPair( node );
             String key = pair.constructKey();
+
+            if ( includes != null && !includes.isEmpty() && !includes.contains( key ) )
+            {
+                return true;
+            }
+
             List<DependencyNodeHopCountPair> pairs = keyToPairsMap.get( key );
             if ( pairs == null )
             {
diff --git a/enforcer-rules/src/site/apt/requireUpperBoundDeps.apt.vm b/enforcer-rules/src/site/apt/requireUpperBoundDeps.apt.vm
index b34a6f4..cf53856 100644
--- a/enforcer-rules/src/site/apt/requireUpperBoundDeps.apt.vm
+++ b/enforcer-rules/src/site/apt/requireUpperBoundDeps.apt.vm
@@ -109,6 +109,11 @@ and
                     <exclude>com.google.guava:guava</exclude>
                   </excludes>
                   -->
+                  <!-- If you include specific cases only these will be checked: (when omitted everything is included)
+                  <includes>
+                    <include>com.google.guava:guava</include>
+                  </includes>
+                  -->
                 </requireUpperBoundDeps>
               </rules>
             </configuration>
@@ -124,5 +129,3 @@ and
   ...
 </project>
 -----------------------------------------------------------------------------------
-
-  
\ No newline at end of file
diff --git a/maven-enforcer-plugin/src/it/mrm/repository/menforcer358_core-5.2.7.pom b/maven-enforcer-plugin/src/it/mrm/repository/menforcer358_core-5.2.7.pom
new file mode 100644
index 0000000..baf8fa5
--- /dev/null
+++ b/maven-enforcer-plugin/src/it/mrm/repository/menforcer358_core-5.2.7.pom
@@ -0,0 +1,26 @@
+<?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>menforcer358_core</artifactId>
+  <version>5.2.7</version>
+</project>
diff --git a/maven-enforcer-plugin/src/it/mrm/repository/menforcer358_core-5.2.8.pom b/maven-enforcer-plugin/src/it/mrm/repository/menforcer358_core-5.2.8.pom
new file mode 100644
index 0000000..b176096
--- /dev/null
+++ b/maven-enforcer-plugin/src/it/mrm/repository/menforcer358_core-5.2.8.pom
@@ -0,0 +1,26 @@
+<?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>menforcer358_core</artifactId>
+  <version>5.2.8</version>
+</project>
diff --git a/maven-enforcer-plugin/src/it/mrm/repository/menforcer358_parent-5.2.7.pom b/maven-enforcer-plugin/src/it/mrm/repository/menforcer358_parent-5.2.7.pom
new file mode 100644
index 0000000..dce4e1e
--- /dev/null
+++ b/maven-enforcer-plugin/src/it/mrm/repository/menforcer358_parent-5.2.7.pom
@@ -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>menforcer358_parent</artifactId>
+  <version>5.2.7</version>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.maven.plugins.enforcer.its</groupId>
+      <artifactId>menforcer358_core</artifactId>
+      <version>5.2.7</version>
+    </dependency>
+  </dependencies>
+</project>
diff --git a/maven-enforcer-plugin/src/it/projects/require-upper-bound-deps_includes/invoker.properties b/maven-enforcer-plugin/src/it/projects/require-upper-bound-deps_includes/invoker.properties
new file mode 100644
index 0000000..c98ac4c
--- /dev/null
+++ b/maven-enforcer-plugin/src/it/projects/require-upper-bound-deps_includes/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
\ No newline at end of file
diff --git a/maven-enforcer-plugin/src/it/projects/require-upper-bound-deps_includes/module/pom.xml b/maven-enforcer-plugin/src/it/projects/require-upper-bound-deps_includes/module/pom.xml
new file mode 100644
index 0000000..6d2c1bb
--- /dev/null
+++ b/maven-enforcer-plugin/src/it/projects/require-upper-bound-deps_includes/module/pom.xml
@@ -0,0 +1,30 @@
+<?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>
+  <parent>
+    <groupId>test</groupId>
+    <artifactId>TestParent</artifactId>
+    <version>1.0-SNAPSHOT</version>
+  </parent>
+  <artifactId>TestModule</artifactId>
+  <version>1.1-SNAPSHOT</version>
+</project>
diff --git a/maven-enforcer-plugin/src/it/projects/require-upper-bound-deps_includes/pom.xml b/maven-enforcer-plugin/src/it/projects/require-upper-bound-deps_includes/pom.xml
new file mode 100644
index 0000000..0e6d073
--- /dev/null
+++ b/maven-enforcer-plugin/src/it/projects/require-upper-bound-deps_includes/pom.xml
@@ -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.plugins.enforcer.its</groupId>
+  <artifactId>menforcer128</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>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.maven.plugins.enforcer.its</groupId>
+      <artifactId>menforcer128_classic</artifactId>
+      <version>0.9.9</version>
+      <!-- Depends on org.apache.maven.plugins.enforcer.its:menforcer128_api:1.5.0 and should fail -->
+    </dependency>
+
+    <dependency>
+      <groupId>org.apache.maven.plugins.enforcer.its</groupId>
+      <artifactId>menforcer358_core</artifactId>
+      <version>5.2.8</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.maven.plugins.enforcer.its</groupId>
+      <artifactId>menforcer358_parent</artifactId>
+      <version>5.2.7</version>
+      <!-- Depends on org.apache.maven.plugins.enforcer.its:menforcer358_core:5.2.7 but is not included -->
+    </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>
+                <RequireUpperBoundDeps>
+                  <includes>
+                    <!-- Only include menforcer128_api -->
+                    <include>org.apache.maven.plugins.enforcer.its:menforcer128_api</include>
+                  </includes>
+                </RequireUpperBoundDeps>
+              </rules>
+            </configuration>
+            <goals>
+              <goal>enforce</goal>
+            </goals>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+</project>
diff --git a/maven-enforcer-plugin/src/it/projects/require-upper-bound-deps_includes/verify.groovy b/maven-enforcer-plugin/src/it/projects/require-upper-bound-deps_includes/verify.groovy
new file mode 100644
index 0000000..db22ccf
--- /dev/null
+++ b/maven-enforcer-plugin/src/it/projects/require-upper-bound-deps_includes/verify.groovy
@@ -0,0 +1,32 @@
+/*
+ * 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 LS = System.getProperty( "line.separator" )
+File buildLog = new File( basedir, 'build.log' )
+
+assert buildLog.text.contains( 'Rule 0: org.apache.maven.plugins.enforcer.RequireUpperBoundDeps failed with message:' )
+def message =
+        'Require upper bound dependencies error for org.apache.maven.plugins.enforcer.its:menforcer128_api:1.4.0 paths to dependency are:'+LS+
+                '+-org.apache.maven.plugins.enforcer.its:menforcer128:1.0-SNAPSHOT'+LS+
+                '  +-org.apache.maven.plugins.enforcer.its:menforcer128_api:1.4.0'+LS+
+                'and'+LS+
+                '+-org.apache.maven.plugins.enforcer.its:menforcer128:1.0-SNAPSHOT'+LS+
+                '  +-org.apache.maven.plugins.enforcer.its:menforcer128_classic:0.9.9'+LS+
+                '    +-org.apache.maven.plugins.enforcer.its:menforcer128_api:1.5.0'+LS
+assert buildLog.text.contains( message )
+