3

Is there way to use the jdepend plugin in maven to fail a build when package cycles exist? I know you can do it fairly easily with ant, but I haven't figured out how to do it with maven.

thanks, Jeff

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
Jeff Storey
  • 56,312
  • 72
  • 233
  • 406

3 Answers3

5

Based on the accepted answer, I improved the performance and log output and released it on Maven Central:

https://github.com/andrena/no-package-cycles-enforcer-rule

(I'd comment on the accepted answer, but don't have enough rep yet.)

Ben Romberg
  • 705
  • 8
  • 8
  • 2
    Please include any relevant code within your answer. That way, if the link should stop working, your answer may remain useful. Also, you should mention what you improved. – Ren May 13 '13 at 08:43
4

You could write your own rule for the maven-enforcer plugin as described in

http://maven.apache.org/enforcer/enforcer-api/writing-a-custom-rule.html

That's how I did it.

NoPackageCyclesRule.java

package org.apache.maven.enforcer.rule;

import java.io.File;
import java.io.IOException;

import jdepend.framework.JDepend;

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.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;

public class NoPackageCyclesRule implements EnforcerRule
{

    public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException
    {
        Log log = helper.getLog();

        try
        {
            MavenProject project = (MavenProject) helper.evaluate("${project}");
            File targetDir = new File((String) helper.evaluate("${project.build.directory}"));
            File classesDir = new File(targetDir, "classes");

            if (project.getPackaging().equalsIgnoreCase("jar") && classesDir.exists())
            {
                JDepend jdepend = new JDepend();
                jdepend.addDirectory(classesDir.getAbsolutePath());
                jdepend.analyze();

                if (jdepend.containsCycles())
                {
                    throw new EnforcerRuleException("There are package cycles");
                }
            }
            else
            {
                log.warn("Skipping jdepend analysis as " + classesDir + " does not exist.");
            }
        }
        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);
        }
    }

    public String getCacheId()
    {
        return "";
    }

    public boolean isCacheable()
    {
        return false;
    }

    public boolean isResultValid(EnforcerRule arg0)
    {
        return false;
    }
}

pom.xml for enforcer rule:

<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>custom-rule</groupId>
  <artifactId>no-package-cycles-rule</artifactId>
  <version>1.0</version>

  <properties>
    <api.version>1.0</api.version>
    <maven.version>2.2.1</maven.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.apache.maven.enforcer</groupId>
      <artifactId>enforcer-api</artifactId>
      <version>${api.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-project</artifactId>
      <version>${maven.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-core</artifactId>
      <version>${maven.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-artifact</artifactId>
      <version>${maven.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-plugin-api</artifactId>
      <version>${maven.version}</version>
    </dependency>
    <dependency>
      <groupId>org.codehaus.plexus</groupId>
      <artifactId>plexus-container-default</artifactId>
      <version>1.5.5</version>
    </dependency>
    <dependency>
      <groupId>jdepend</groupId>
      <artifactId>jdepend</artifactId>
      <version>2.9.1</version>
    </dependency>
  </dependencies>

</project>

Then you can add it to your build:

  <plugin>
    <artifactId>maven-enforcer-plugin</artifactId>
    <dependencies>
      <dependency>
        <groupId>custom-rule</groupId>
        <artifactId>no-package-cycles-rule</artifactId>
        <version>1.0</version>
      </dependency>
    </dependencies>
    <executions>
      <execution>
        <id>enforce-no-package-cycles</id>
        <goals>
          <goal>enforce</goal>
        </goals>
        <phase>verify</phase> <!-- use a phase after compile! -->
        <configuration>
          <rules>
            <NoPackageCyclesRule
              implementation="org.apache.maven.enforcer.rule.NoPackageCyclesRule" />
          </rules>
        </configuration>
      </execution>
    </executions>
  </plugin>
Daniel Seidewitz
  • 720
  • 8
  • 23
  • I did it using the post build plugin with Jenkins but your way is much simpler. Thanks. – Jeff Storey May 14 '11 at 19:44
  • What about assertFalse(jdepend.containsCycles()) in a junit test? Is there a reason to use the maven enforcer over failing a unit test? – Jason Jul 17 '12 at 01:28
  • The JUnit-Test approach is not as practical as the enforcer rule if you have a multi-module project. You would have to add the test to every single module. – Daniel Seidewitz Jul 23 '12 at 09:40
1

From what I can see, the JDepend Maven Plugin is supposed to be used to generate a report, it doesn't allow to fail the build on particular rules violations.

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124