Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MENFORCER-411] DependencyConvergence takes include/exclude parameters to filter errors #136

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public class DependencyConvergence

private boolean uniqueVersions;

private List<String> includes;

private List<String> excludes;

public void setUniqueVersions( boolean uniqueVersions )
{
this.uniqueVersions = uniqueVersions;
Expand Down Expand Up @@ -113,8 +117,8 @@ public void execute( EnforcerRuleHelper helper )
DependencyVersionMap visitor = new DependencyVersionMap( log );
visitor.setUniqueVersions( uniqueVersions );
node.accept( visitor );
List<CharSequence> errorMsgs = new ArrayList<>();
errorMsgs.addAll( getConvergenceErrorMsgs( visitor.getConflictedVersionNumbers() ) );
List<List<DependencyNode>> errors = visitor.getConflictedVersionNumbers( includes, excludes );
List<CharSequence> errorMsgs = new ArrayList<>( getConvergenceErrorMsgs( errors ) );
for ( CharSequence errorMsg : errorMsgs )
{
log.warn( errorMsg );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public static Set<Artifact> checkDependencies( Set<Artifact> dependencies, List<
* @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 )
static boolean compareDependency( String pattern, Artifact artifact )
throws EnforcerRuleException
{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
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 @@ -120,16 +122,89 @@ 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 ( includes != null && !includes.isEmpty() )
slawekjaranowski marked this conversation as resolved.
Show resolved Hide resolved
{
List<DependencyNode> filteredNodes = new ArrayList<>();
for ( DependencyNode node : nodes )
{
if ( includeArtifact( node.getArtifact(), formattedIncludes, formattedExcludes ) )
{
filteredNodes.add( node );
}
}
nodes = filteredNodes;
slawekjaranowski marked this conversation as resolved.
Show resolved Hide resolved
}
if ( containsConflicts( nodes ) )
{
output.add( nodes );
}
}
return output;
}

private static boolean includeArtifact( Artifact artifact, List<String> includes, List<String> excludes )
throws EnforcerRuleException
{
boolean included = false;
if ( includes != null )
{
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;
}
}
33 changes: 32 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,34 @@ 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. If empty, dependency
convergence will be enforced for all artifacts.

* excludes - A list of artifacts for which dependency convergence should not be enforced. These are exceptions
to the includes. In other words, excludes only subtract from artifacts that matched an included artifact.
If you want to enforce dependency convergence for all except a few artifacts, set the includes to <<<*>>> and
exclude the artifacts you want.

[]

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
slawekjaranowski marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?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-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 there are no includes, return all errors -->
<dependencyConvergence>
<includes/>
<excludes>
<exclude>org.slf4j</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,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>