Prevent module misconfiguration from additional packages that enclose the root package or another additional package
#1247 filters an additionalPackages entry that is nested in the root package. Two similar entries are still registered as root packages, and both give unexpected modules:
- an entry that encloses the root package, for example
"com"for an application incom.example.app; - an entry that is nested in another additional package, for example
"com.other.billing"next to"com.other".
Reproducer
A minimal Maven project, attached as spring-modulith-additional-packages-repro.zip. Run mvn test.
- The application is in
com.example.app, with the modulecom.example.app.order. - A second root has
com.other.billing,com.other.billing.invoiceandcom.other.billing.payment. Theinvoicepackage has@ApplicationModulewithout anid. Thepaymentpackage has no annotation. - Five
@Modulithictypes incom.example.appdeclare the configurations: the intended one, the two cases, and the two cases withuseFullyQualifiedModuleNames = true. AdditionalPackagesReproTestprintsApplicationModules.of(type)for each one.AdditionalPackagesVerifyTestcallsApplicationModules.of(type).verify()for each one.
Actual result
The intended configuration, additionalPackages = "com.other":
billing -> com.other.billing
billing.invoice -> com.other.billing.invoice
order -> com.example.app.order
1. additionalPackages = "com": 11 modules
apple -> com.apple
example -> com.example
fasterxml -> com.fasterxml
jayway -> com.jayway
jetbrains -> com.jetbrains
order -> com.example.app.order
other -> com.other
other.billing.invoice -> com.other.billing.invoice
structurizr -> com.structurizr
sun -> com.sun
tngtech -> com.tngtech
- The packages of libraries and of the JDK become application modules:
com.fasterxml,com.tngtech,com.sun,com.appleand others. The defaultImportOptionexcludes only the tests, soimportPackages("com")imports the classes of every JAR on the classpath. com.examplebecomes the moduleexample. It holds the application packagecom.example.appand every type there that is not in another module.
verify() fails with 146 violations between two libraries, which the user cannot fix. They name 6 distinct types:
org.springframework.modulith.core.Violations:
- Module 'structurizr' depends on non-exposed type com.fasterxml.jackson.annotation.JsonGetter within module 'fasterxml'!
- Module 'structurizr' depends on non-exposed type com.fasterxml.jackson.annotation.JsonIgnore within module 'fasterxml'!
- Module 'structurizr' depends on non-exposed type com.fasterxml.jackson.annotation.JsonInclude within module 'fasterxml'!
- Module 'structurizr' depends on non-exposed type com.fasterxml.jackson.annotation.JsonInclude$Include within module 'fasterxml'!
- Module 'structurizr' depends on non-exposed type com.fasterxml.jackson.annotation.JsonSetter within module 'fasterxml'!
- Module 'structurizr' depends on non-exposed type com.fasterxml.jackson.annotation.Nulls within module 'fasterxml'!
With "com", each test takes 7 to 11 seconds, because it imports all classes under com on the classpath. With the other configurations, each test takes less than a second.
2. additionalPackages = { "com.other", "com.other.billing" }: 5 modules
billing -> com.other.billing
billing.invoice -> com.other.billing.invoice
invoice -> com.other.billing.invoice
order -> com.example.app.order
payment -> com.other.billing.payment
- Two modules on one package:
com.other.billing.invoiceis found from both roots, with an identifier relative to each root.ApplicationModuleSource.equalscompares the identifier and the package, so both sources stay. The types ofinvoicethen belong to two modules. This is the situation from #1247, wheregetModuleForPackagepicked one of them by iteration order. - A split module:
com.other.billing.paymentbecomes its own module. With the intended configuration, it is a part ofbilling.
verify() passes, so nothing tells the user about either effect.
useFullyQualifiedModuleNames = true helps only with the duplicate
With "com", the result has the same 11 modules, with fully qualified identifiers such as com.fasterxml. verify() fails with the same 146 violations.
With { "com.other", "com.other.billing" }, the result has 4 modules:
com.example.app.order -> com.example.app.order
com.other.billing -> com.other.billing
com.other.billing.invoice -> com.other.billing.invoice
com.other.billing.payment -> com.other.billing.payment
Both roots now give invoice the same identifier, so the two sources are equal and only one module stays. payment is still a module of its own.
Expected result
Spring Modulith rejects or ignores both entries, as #1247 does for an entry in the root package:
- An entry that encloses the root package fails at startup with a clear message. If an enclosing entry has a valid use, Spring could ignore it and log a warning instead.
- An entry that is nested in another additional package is ignored, because the enclosing entry already scans it.
Related
- #1247: the same filter, for an entry that is nested in the root package.
- #1964: duplicate module identifiers from two roots. A broad entry such as
"com"makes this more likely. - #613: modules from other JARs through
additionalPackages. This is why the import of JARs is correct, and only a too broad entry is the problem.