diff --git a/src/it/circular-exclusions/invoker.properties b/src/it/circular-exclusions/invoker.properties new file mode 100644 index 00000000..c48854c1 --- /dev/null +++ b/src/it/circular-exclusions/invoker.properties @@ -0,0 +1,3 @@ +invoker.goals=enforcer:enforce +invoker.buildResult = failure +invoker.maven.version = 3.0+ \ No newline at end of file diff --git a/src/it/circular-exclusions/pom.xml b/src/it/circular-exclusions/pom.xml new file mode 100644 index 00000000..8689e8ce --- /dev/null +++ b/src/it/circular-exclusions/pom.xml @@ -0,0 +1,49 @@ + + 4.0.0 + + org.slf4j + slf4j-api + 1.6.1 + CircularDependencies test + + + + + commons-logging + commons-logging + 1.1.1 + + + org.slf4j + slf4j-api + + + + + + + + + maven-enforcer-plugin + @enforcerPluginVersion@ + + + @project.groupId@ + @project.artifactId@ + @project.version@ + + + + + + true + + + + + + + + \ No newline at end of file diff --git a/src/main/java/org/apache/maven/plugins/enforcer/BanCircularDependencies.java b/src/main/java/org/apache/maven/plugins/enforcer/BanCircularDependencies.java index 1edf567a..e0717443 100644 --- a/src/main/java/org/apache/maven/plugins/enforcer/BanCircularDependencies.java +++ b/src/main/java/org/apache/maven/plugins/enforcer/BanCircularDependencies.java @@ -20,6 +20,7 @@ */ import java.util.HashSet; +import java.util.List; import java.util.Set; import org.apache.maven.artifact.Artifact; @@ -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; @@ -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} @@ -99,6 +108,24 @@ public void execute( EnforcerRuleHelper helper ) } } } + if( checkExclusions ) + { + List dependencies = project.getDependencies(); + for( Dependency dependency : dependencies ) + { + List 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 ) { diff --git a/src/site/apt/banCircularDependencies.apt.vm b/src/site/apt/banCircularDependencies.apt.vm index 7f776841..42498b0b 100644 --- a/src/site/apt/banCircularDependencies.apt.vm +++ b/src/site/apt/banCircularDependencies.apt.vm @@ -50,7 +50,9 @@ Ban Circular Dependencies - + + true + true