Java: Finding Package Cycles

JDepend is a tool for detecting cycles between your Java packages. It is often used from a Maven plugin to generate reports for the project’s Maven site.
In most teams, however, people only look at these reports from time to time. So when a cycle has been introduced, it takes a while until someone notices. The later the cycle is detected, the harder it is to fix. Fortunately, there’s an easy solution.

Using JDepend’s API, you can add a cycle check to your unit tests. This way, cycles will be detected much earlier, when developers (or your continuous integration server) run the unit tests.

All you have to do is to add the following test (JUnit 4 syntax):

import static;
import static org.junit.Assert.assertThat;
import jdepend.framework.JDepend;
import org.junit.Test;

public class DependenciesTest {
    public void testCycles() throws IOException {
        JDepend jdepend = new JDepend();


        assertThat(jdepend.containsCycles(), is(false));

If your project uses Maven, you have to add the following dependency:


When the test fails you can use a JDepend-based tool like the Eclipse plugin to pinpoint the exact problem.

If you want to detect cycles only on an application-specific level (for example between layers or modules) instead of the package level, you can define components as described in JDepend’s API documentation.

This entry was posted in java and tagged , , . Bookmark the permalink.

2 Responses to Java: Finding Package Cycles

  1. Ian Lim says:

    Great idea, still applies at 2015.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s