1

I was thinking that it would be nice with a maven plugin that bans circular package dependencies. Looked around and didn't find anything.

Let me try to define the problem:

Package dependency: There exists code in package A that references code from package B. Package A depends on package B.

A -> B

Circular package dependency: There exists dependencies between packages so that the dependency graph of packages within a project forms a cyclic graph.

A -> B -> A

What I want the plugin to do, in a best effort manner, is to enforce that the package dependency graph is acyclic (it has to be a directed acyclic graph). It's fine if it misses reflective code and such that is hard to analyze.

Anyone know of such a plugin, or maybe library.. that does this?

Thanks!

Sason Ohanian
  • 795
  • 5
  • 16
  • I use different multiple maven modules to make the layering explicit while still allow some circular dependencies within a module. – Peter Lawrey Sep 25 '18 at 14:59
  • I would expect if you really have such things that you will get build failures already based on circular dependencies not necessarily. – khmarbaise Sep 25 '18 at 17:10
  • @khmarbaise - not sure what you mean, but there is nothing keeping you from having circular dependencies between package as I defined. For instance if package A contains class A1 och A2, package B contains B1 and B2. A1 could depend on B1 while B2 depends on A2, i.e. A -> B -> A – Sason Ohanian Sep 25 '18 at 21:57
  • 1
    @PeterLawrey - Agree, that works, but it's a slightly different concept. I think enforcing acyclic packages would, especially in a codebase with lots of devs, help improve code structure, and make devs think twice on where classes should belong. – Sason Ohanian Sep 25 '18 at 22:08
  • @SasonOhanian from experience of this sort of thing, developers will add to the package everything work, ie the highest, and you might find you have to restructure the code later where it could have been lower in the dependencies. – Peter Lawrey Sep 26 '18 at 07:29
  • @PeterLawrey - good point, people always find their ways around rules. Hopefully the rule will make people wonder about the reasons behind, and we can have a discussion on why it's there. – Sason Ohanian Sep 26 '18 at 15:20

3 Answers3

2

While not specifically targeted at your problem, it should be solvable with https://jqassistant.org/.

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
1
  1. for package-cycles maybe you can try: https://github.com/andrena/no-package-cycles-enforcer-rule maven jdepend fail build with cycles
<plugin>
    <artifactId>maven-enforcer-plugin</artifactId>
    <version>3.0.0-M2</version>
    <dependencies>
        <dependency>
            <groupId>de.andrena.tools.nopackagecycles</groupId>
            <artifactId>no-package-cycles-enforcer-rule</artifactId>
            <version>1.0.9</version>
        </dependency>
    </dependencies>
    <executions>
        <execution>
            <id>enforce-no-package-cycles</id>
            <goals>
                <goal>enforce</goal>
            </goals>
            <phase>test</phase>
            <configuration>
                <rules>
                    <NoPackageCyclesRule implementation="de.andrena.tools.nopackagecycles.NoPackageCyclesRule" />
                </rules>
            </configuration>
        </execution>
    </executions>
</plugin>
  1. for dependencies-cycles, as khmarbaise said you can try: https://www.mojohaus.org/extra-enforcer-rules/banCircularDependencies.html
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-enforcer-plugin</artifactId>
        <version>3.0.0-M3</version> <!-- find the latest version at http://maven.apache.org/plugins/maven-enforcer-plugin/ -->
        <executions>
          <execution>
            <id>enforce-ban-circular-dependencies</id>
            <goals>
              <goal>enforce</goal>
            </goals>
            <configuration>
              <rules>
                <banCircularDependencies/>
              </rules>
              <fail>true</fail>
            </configuration>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>extra-enforcer-rules</artifactId>
            <version>1.3</version>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>

rhinoceros.xn
  • 803
  • 9
  • 12
0

You should check for extra-enforcer-rules which you can find here.

https://www.mojohaus.org/extra-enforcer-rules/banCircularDependencies.html which has to be used in relationship with maven-enforcer-plugin

khmarbaise
  • 92,914
  • 28
  • 189
  • 235