Search code examples
javaspringgradleonion-architecture

Fail compilation if imports from Package B exist in a class in Package A


I am working on a Spring Boot project and am following the Onion Architecture, where each component is a different package. My build tool is gradle.

https://www.codeguru.com/csharp/understanding-onion-architecture/

Example: I have packages named controller, infrastructure, application, domain and core as per Onion Architecture.

My question is: I want to disallow imports from "infrastructure" package in "domain" package and fail the build automatically. Is there a way to achieve this either in Java or Gradle?


Solution

  • I think you should look at ArchUnit https://www.archunit.org/ it allows you to implement this kind of constraint in a unit test form, You can then hook that into your build/CI to block changes breaking your rules.