spring-modulith
spring-modulith copied to clipboard
Verification that no classes exist outside Application‘s package
Hi,
in ArchUnit, there is the possibility to check that there are no classes outside of the packages, for which an architecture is defined. Wouldn’t this be a nice additional verification for spring-modulith too ? It would ensure, that there is no other root package besides the application‘s main package.
Maybe this is a non-issue, if all root packages are verified by modulith anyway (?)
So I have some questions to be clear about modulith's conecepts:
1) what are valid root packages ?
└─ □ src/main/java
├─ □ com.mycompany
| └─ Application.java
├─ □ com.mycompany.inventory
| └─ …
└─ □ com.mycompany.order
└─ …
Is "com.mycompany" considered a root package ? Or would that just be "com" ?
2) many root packages ? all packages considered by modulith ?
└─ □ src/main/java
├─ □ example1
| └─ Application.java
├─ □ example1.inventory
| └─ …
└─ □ example1.order
└─ …
├─ □ example2
| └─ MySneakyServices.java
Is "example2" also regarded as a second root package by modulith and is it allowed to access anything public from example 2 ?
These are my findings, when I experimented with the provided example: "com.mycompany"

...and this also fails to verify dependencies when renaming "com.mycompany" to "example2"
So, my wish would be:
-
to have the optional 'strict' mode, in which all packages are considered, not just the one where "Application" (main) resides in
-
to optionally be able to specify root packages like "com.mycompany", so that it is made explicit, and that there is no convention of just considering the first package path "com"
Your second point can already be addressed using the @Modulithic annotation like this:
@Modulithic(additionalPackages = "com.mycompany")
@SpringBootApplication
public class Application { … }
I don't think we will be able to achieve the former, as we only scan the package containing the main application class. Even if we scanned potential parent packages, there's no way for us to differentiate between application classes and library ones.
Regarding your general questions, the docs are actually pretty precise on this.
Is "com.mycompany" considered a root package ? Or would that just be "com" ?
com.mycompany as that is the package, the main application class resides in.
Is "example2" also regarded as a second root package by modulith and is it allowed to access anything public from example 2 ?
example2 is not considered by Spring Modulith at all (for the reasons described above) and code residing in there would not be verified for potential violations.