-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from davidburkhart/master
Cleanup & add target/test-classes to package cycle check
- Loading branch information
Showing
1,724 changed files
with
22,274 additions
and
22,227 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,38 @@ | ||
[![Build Status](https://buildhive.cloudbees.com/job/andrena/job/no-package-cycles-enforcer-rule/badge/icon)](https://buildhive.cloudbees.com/job/andrena/job/no-package-cycles-enforcer-rule/) | ||
|
||
This Maven Enforcer Rule checks your project for package cycles. It fails the build if any package cycle is found, showing you the packages and classes involved in the cycle. | ||
|
||
Usage: Add the following plugin to your POM: | ||
|
||
``` | ||
<plugin> | ||
<artifactId>maven-enforcer-plugin</artifactId> | ||
<version>1.2</version> | ||
<dependencies> | ||
<dependency> | ||
<groupId>de.andrena.tools.nopackagecycles</groupId> | ||
<artifactId>no-package-cycles-enforcer-rule</artifactId> | ||
<version>1.0.4</version> | ||
</dependency> | ||
</dependencies> | ||
<executions> | ||
<execution> | ||
<id>enforce-no-package-cycles</id> | ||
<goals> | ||
<goal>enforce</goal> | ||
</goals> | ||
<phase>compile</phase> | ||
<configuration> | ||
<rules> | ||
<NoPackageCyclesRule implementation="de.andrena.tools.nopackagecycles.NoPackageCyclesRule" /> | ||
</rules> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
``` | ||
|
||
See also: | ||
* The original version by Daniel Seidewitz on [Stackoverflow](http://stackoverflow.com/questions/3416547/maven-jdepend-fail-build-with-cycles). Improved by showing all packages afflicted with cycles and the corresponding classes importing the conflicting packages. | ||
* [JDepend](https://github.com/clarkware/jdepend), the library being used to detect package cycles. | ||
* For more information about package cycles, see ["The Acyclic Dependencies Principle" by Robert C. Martin (Page 6)](http://www.objectmentor.com/resources/articles/granularity.pdf). | ||
[![Build Status](https://buildhive.cloudbees.com/job/andrena/job/no-package-cycles-enforcer-rule/badge/icon)](https://buildhive.cloudbees.com/job/andrena/job/no-package-cycles-enforcer-rule/) | ||
|
||
This Maven Enforcer Rule checks your project for package cycles. It fails the build if any package cycle is found, showing you the packages and classes involved in the cycle. | ||
|
||
Usage: Add the following plugin to your POM: | ||
|
||
``` | ||
<plugin> | ||
<artifactId>maven-enforcer-plugin</artifactId> | ||
<version>1.2</version> | ||
<dependencies> | ||
<dependency> | ||
<groupId>de.andrena.tools.nopackagecycles</groupId> | ||
<artifactId>no-package-cycles-enforcer-rule</artifactId> | ||
<version>1.0.4</version> | ||
</dependency> | ||
</dependencies> | ||
<executions> | ||
<execution> | ||
<id>enforce-no-package-cycles</id> | ||
<goals> | ||
<goal>enforce</goal> | ||
</goals> | ||
<phase>test-compile</phase> | ||
<configuration> | ||
<rules> | ||
<NoPackageCyclesRule implementation="de.andrena.tools.nopackagecycles.NoPackageCyclesRule" /> | ||
</rules> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
``` | ||
|
||
See also: | ||
* The original version by Daniel Seidewitz on [Stackoverflow](http://stackoverflow.com/questions/3416547/maven-jdepend-fail-build-with-cycles). Improved by showing all packages afflicted with cycles and the corresponding classes importing the conflicting packages. | ||
* [JDepend](https://github.com/clarkware/jdepend), the library being used to detect package cycles. | ||
* For more information about package cycles, see ["The Acyclic Dependencies Principle" by Robert C. Martin (Page 6)](http://www.objectmentor.com/resources/articles/granularity.pdf). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/main/java/de/andrena/tools/nopackagecycles/DirectoriesWithClasses.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package de.andrena.tools.nopackagecycles; | ||
|
||
import static java.util.Collections.unmodifiableList; | ||
|
||
import java.io.File; | ||
import java.util.Iterator; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper; | ||
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException; | ||
|
||
public class DirectoriesWithClasses implements Iterable<File>{ | ||
|
||
public static final String MAVEN_PROJECT_BUILD_OUTPUT_DIRECTORY_VAR = "${project.build.outputDirectory}"; | ||
public static final String MAVEN_PROJECT_BUILD_TEST_OUTPUT_DIRECTORY_VAR = "${project.build.testOutputDirectory}"; | ||
|
||
private final List<File> directories = new LinkedList<File>(); | ||
|
||
public DirectoriesWithClasses(EnforcerRuleHelper helper) throws ExpressionEvaluationException { | ||
addDirectoryIfExists(helper, MAVEN_PROJECT_BUILD_OUTPUT_DIRECTORY_VAR); | ||
addDirectoryIfExists(helper, MAVEN_PROJECT_BUILD_TEST_OUTPUT_DIRECTORY_VAR); | ||
} | ||
|
||
private void addDirectoryIfExists(EnforcerRuleHelper helper, String variable) | ||
throws ExpressionEvaluationException { | ||
File directory = new File((String) helper.evaluate(variable)); | ||
if(directory.exists()) { | ||
helper.getLog().info("Adding directory " + directory.getAbsolutePath() + " for package cycles search."); | ||
directories.add(directory); | ||
} else { | ||
helper.getLog().info("Directory " + directory.getAbsolutePath() + " could not be found."); | ||
} | ||
} | ||
|
||
public boolean directoriesWithClassesFound() { | ||
return !directories.isEmpty(); | ||
} | ||
|
||
public Iterator<File> iterator() { | ||
return unmodifiableList(directories).iterator(); | ||
} | ||
} |
145 changes: 69 additions & 76 deletions
145
src/main/java/de/andrena/tools/nopackagecycles/NoPackageCyclesRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,70 @@ | ||
package de.andrena.tools.nopackagecycles; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
|
||
import jdepend.framework.JDepend; | ||
import jdepend.framework.JavaPackage; | ||
|
||
import org.apache.maven.enforcer.rule.api.EnforcerRule; | ||
import org.apache.maven.enforcer.rule.api.EnforcerRuleException; | ||
import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper; | ||
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException; | ||
|
||
public class NoPackageCyclesRule implements EnforcerRule { | ||
|
||
public static final String MAVEN_CLASSES_DIR = "classes"; | ||
public static final String MAVEN_PROJECT_BUILD_DIRECTORY_VAR = "${project.build.directory}"; | ||
|
||
public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException { | ||
try { | ||
executePackageCycleCheckIfNecessary(helper); | ||
} catch (ExpressionEvaluationException e) { | ||
throw new EnforcerRuleException("Unable to lookup an expression " + e.getLocalizedMessage(), e); | ||
} catch (IOException e) { | ||
throw new EnforcerRuleException("Unable to access target directory " + e.getLocalizedMessage(), e); | ||
} | ||
} | ||
|
||
private void executePackageCycleCheckIfNecessary(EnforcerRuleHelper helper) throws ExpressionEvaluationException, | ||
IOException, EnforcerRuleException { | ||
File targetDir = new File((String) helper.evaluate(MAVEN_PROJECT_BUILD_DIRECTORY_VAR)); | ||
File classesDir = new File(targetDir, MAVEN_CLASSES_DIR); | ||
helper.getLog().info("Searching directory " + classesDir.getAbsolutePath() + " for package cycles."); | ||
if (checkIsNecessary(classesDir)) { | ||
executePackageCycleCheck(classesDir); | ||
} else { | ||
helper.getLog().info("Directory " + classesDir.getAbsolutePath() + " could not be found."); | ||
} | ||
} | ||
|
||
private void executePackageCycleCheck(File classesDir) throws IOException, EnforcerRuleException { | ||
JDepend jdepend = createJDepend(); | ||
jdepend.addDirectory(classesDir.getAbsolutePath()); | ||
jdepend.analyze(); | ||
if (jdepend.containsCycles()) { | ||
throw new EnforcerRuleException("There are package cycles:" + getPackageCycles(jdepend)); | ||
} | ||
} | ||
|
||
protected JDepend createJDepend() { | ||
return new JDepend(); | ||
} | ||
|
||
private String getPackageCycles(JDepend jdepend) { | ||
@SuppressWarnings("unchecked") | ||
Collection<JavaPackage> packages = jdepend.getPackages(); | ||
return new PackageCycleOutput(new ArrayList<JavaPackage>(packages)).getOutput(); | ||
} | ||
|
||
private boolean checkIsNecessary(File classesDir) { | ||
return classesDir.exists(); | ||
} | ||
|
||
public String getCacheId() { | ||
return ""; | ||
} | ||
|
||
public boolean isCacheable() { | ||
return false; | ||
} | ||
|
||
public boolean isResultValid(EnforcerRule arg0) { | ||
return false; | ||
} | ||
package de.andrena.tools.nopackagecycles; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
|
||
import jdepend.framework.JDepend; | ||
import jdepend.framework.JavaPackage; | ||
|
||
import org.apache.maven.enforcer.rule.api.EnforcerRule; | ||
import org.apache.maven.enforcer.rule.api.EnforcerRuleException; | ||
import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper; | ||
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException; | ||
|
||
public class NoPackageCyclesRule implements EnforcerRule { | ||
|
||
public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException { | ||
try { | ||
executePackageCycleCheckIfNecessary(helper); | ||
} catch (ExpressionEvaluationException e) { | ||
throw new EnforcerRuleException("Unable to lookup an expression " + e.getLocalizedMessage(), e); | ||
} catch (IOException e) { | ||
throw new EnforcerRuleException("Unable to access target directory " + e.getLocalizedMessage(), e); | ||
} | ||
} | ||
|
||
private void executePackageCycleCheckIfNecessary(EnforcerRuleHelper helper) throws ExpressionEvaluationException, | ||
IOException, EnforcerRuleException { | ||
DirectoriesWithClasses directories = new DirectoriesWithClasses(helper); | ||
if (directories.directoriesWithClassesFound()) { | ||
executePackageCycleCheck(directories); | ||
} else { | ||
helper.getLog().info("No directories with classes to check for cycles found."); | ||
} | ||
} | ||
|
||
private void executePackageCycleCheck(Iterable<File> directories) throws IOException, EnforcerRuleException { | ||
JDepend jdepend = createJDepend(); | ||
for (File directory : directories) { | ||
jdepend.addDirectory(directory.getAbsolutePath()); | ||
} | ||
jdepend.analyze(); | ||
if (jdepend.containsCycles()) { | ||
throw new EnforcerRuleException("There are package cycles:" + getPackageCycles(jdepend)); | ||
} | ||
} | ||
|
||
protected JDepend createJDepend() { | ||
return new JDepend(); | ||
} | ||
|
||
private String getPackageCycles(JDepend jdepend) { | ||
@SuppressWarnings("unchecked") | ||
Collection<JavaPackage> packages = jdepend.getPackages(); | ||
return new PackageCycleOutput(new ArrayList<JavaPackage>(packages)).getOutput(); | ||
} | ||
|
||
public String getCacheId() { | ||
return ""; | ||
} | ||
|
||
public boolean isCacheable() { | ||
return false; | ||
} | ||
|
||
public boolean isResultValid(EnforcerRule arg0) { | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.