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

Add property 'checkExclusions' to BanCircularDependencies. #72

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/it/circular-exclusions/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
invoker.goals=enforcer:enforce
invoker.buildResult = failure
invoker.maven.version = 3.0+
49 changes: 49 additions & 0 deletions src/it/circular-exclusions/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.1</version>
<name>CircularDependencies test</name>

<dependencies>
<!-- if commons-logging had slf4j-api in the dependency tree,
it would have been able to get around the rule with this exclusion -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-enforcer-plugin</artifactId>
<version>@enforcerPluginVersion@</version>
<dependencies>
<dependency>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
</dependency>
</dependencies>
<configuration>
<rules>
<banCircularDependencies>
<checkExclusions>true</checkExclusions>
</banCircularDependencies>
</rules>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.maven.artifact.Artifact;
Expand All @@ -28,6 +29,8 @@
import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
import org.apache.maven.execution.RuntimeInformation;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Exclusion;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;
import org.apache.maven.shared.dependency.graph.DependencyGraphBuilder;
Expand All @@ -48,6 +51,12 @@ public class BanCircularDependencies
private transient DependencyGraphBuilder graphBuilder;

private String message;

/**
* If {@code false} then the rule will only check the dependency hierarchy.
* If {@code true} then the rule will also verify there are no exclusions used to get around the rule.
*/
private boolean checkExclusions;

/**
* {@inheritDoc}
Expand Down Expand Up @@ -99,6 +108,24 @@ public void execute( EnforcerRuleHelper helper )
}
}
}
if( checkExclusions )
{
List<Dependency> dependencies = project.getDependencies();
for( Dependency dependency : dependencies )
{
List<Exclusion> exclusions = dependency.getExclusions();
for(Exclusion exclusion: exclusions)
{
if(exclusion.getGroupId().equals(project.getGroupId()) &&
exclusion.getArtifactId().equals(project.getArtifactId()))
{
StringBuilder buf = new StringBuilder("You are not allowed to break the circular dependency by excluding yourself. Self exclusion found under dependency ");
buf.append( "\n " ).append( dependency.getGroupId() ).append( ":" ).append( dependency.getArtifactId() ).append( "\n " );
throw new EnforcerRuleException( buf.toString() );
}
}
}
}
}
catch ( ExpressionEvaluationException e )
{
Expand Down
4 changes: 3 additions & 1 deletion src/site/apt/banCircularDependencies.apt.vm
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ Ban Circular Dependencies
</goals>
<configuration>
<rules>
<banCircularDependencies/>
<banCircularDependencies>
<checkExclusions>true</checkExclusions>
</banCircularDependencies>
</rules>
<fail>true</fail>
</configuration>
Expand Down