Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to enforce package dependency hierarchy in Java?

Basically I have packages...

com.me.application
com.me.application.thing
com.me.library

I want to enforce the rule that nothing in com.me.application can include com.me.library, except things in com.me.application.thing.

Is this possible at the level of Java code, Maven, classpath, or any other layer of Java that would be roughly equivalent to linking in C?

like image 282
djechlin Avatar asked Oct 21 '25 05:10

djechlin


2 Answers

Instead of trying to force the compiler to do it, why not use source code analysis instead?

I would recommend using the architecture rules engine that is now embedded in Sonar. Combine this with the build breaker plugin and you can trigger a build failure, if developers breach your hierarchy rules.

like image 156
Mark O'Connor Avatar answered Oct 22 '25 18:10

Mark O'Connor


You can enforce the checks with Checkstyle's import control rule. Configure the Maven Checkstyle plugin into your pom.xml, and any violations can be set to cause the build to fail.

like image 38
Saish Avatar answered Oct 22 '25 18:10

Saish



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!