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 08:55:19 UTC
[maven-enforcer] 01/02: Added includes to RequireUpperBoundDeps rule
This is an automated email from the ASF dual-hosted git repository.
rfscholte pushed a commit to branch MENFORCER-358
in repository https://gitbox.apache.org/repos/asf/maven-enforcer.git
commit 6992339a647347c57574bfe3e23782efd40b5466
Author: Roy van Rijn <r....@portofrotterdam.com>
AuthorDate: Wed Jul 29 13:35:22 2020 +0200
Added includes to RequireUpperBoundDeps rule
---
.../plugins/enforcer/RequireUpperBoundDeps.java | 84 ++++++++++++++--------
.../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, 305 insertions(+), 31 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..f3a2f42 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,12 +47,11 @@ 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
*/
-public class RequireUpperBoundDeps
- extends AbstractNonCacheableEnforcerRule
+public class RequireUpperBoundDeps extends AbstractNonCacheableEnforcerRule
{
private static Log log;
@@ -69,9 +68,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 )
@@ -81,6 +87,7 @@ public class RequireUpperBoundDeps
/**
* Sets dependencies to exclude.
+ *
* @param excludes a list of {@code groupId:artifactId} names
*/
public void setExcludes( List<String> excludes )
@@ -88,20 +95,29 @@ 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.
- *
+ * 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
*/
// CHECKSTYLE_ON: LineLength
- private DependencyNode getNode( EnforcerRuleHelper helper )
- throws EnforcerRuleException
+ private DependencyNode getNode( EnforcerRuleHelper helper ) throws EnforcerRuleException
{
try
{
@@ -112,9 +128,8 @@ public class RequireUpperBoundDeps
ArtifactMetadataSource metadataSource = helper.getComponent( ArtifactMetadataSource.class );
ArtifactCollector collector = helper.getComponent( ArtifactCollector.class );
ArtifactFilter filter = null; // we need to evaluate all scopes
- DependencyNode node =
- dependencyTreeBuilder.buildDependencyTree( project, repository, factory, metadataSource, filter,
- collector );
+ DependencyNode node = dependencyTreeBuilder.buildDependencyTree( project, repository, factory,
+ metadataSource, filter, collector );
return node;
}
catch ( ExpressionEvaluationException e )
@@ -132,8 +147,7 @@ public class RequireUpperBoundDeps
}
@Override
- public void execute( EnforcerRuleHelper helper )
- throws EnforcerRuleException
+ public void execute( EnforcerRuleHelper helper ) throws EnforcerRuleException
{
if ( log == null )
{
@@ -144,12 +158,13 @@ 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 )
{
- throw new EnforcerRuleException( "Failed while enforcing RequireUpperBoundDeps. The error(s) are "
- + errorMessages );
+ throw new EnforcerRuleException(
+ "Failed while enforcing RequireUpperBoundDeps. The error(s) are " + errorMessages );
}
}
catch ( Exception e )
@@ -180,8 +195,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 +217,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();
}
@@ -237,24 +253,35 @@ public class RequireUpperBoundDeps
return artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + version;
}
- private static class RequireUpperBoundDepsVisitor
- implements DependencyNodeVisitor
+ private static class RequireUpperBoundDepsVisitor implements DependencyNodeVisitor
{
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 )
{
@@ -318,8 +345,7 @@ public class RequireUpperBoundDeps
}
- private static class DependencyNodeHopCountPair
- implements Comparable<DependencyNodeHopCountPair>
+ private static class DependencyNodeHopCountPair implements Comparable<DependencyNodeHopCountPair>
{
private DependencyNode node;
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 )
+