Verifying Application Module Structure

We can verify whether our code arrangement adheres to the intended constraints by calling the ….verify() method on our ApplicationModules instance:

  • Java

  • Kotlin

ApplicationModules.of(Application.class).verify();
ApplicationModules.of(Application::class.java).verify()

The verification includes the following rules:

  • No cycles on the application module level — the dependencies between modules have to form a directed acyclic graph.

  • Efferent module access via API packages only — all references to types that reside in application module internal packages are rejected. See Advanced Application Modules for details. Dependencies into internals of Open Application Modules are allowed.

  • Explicitly allowed application module dependencies only (optional) — an application module can optionally define allowed dependencies via @ApplicationModule(allowedDependencies = …). If those are configured, dependencies to other application modules are rejected. See Explicit Application Module Dependencies and Named Interfaces for details.

Spring Modulith optionally integrates with the jMolecules ArchUnit library and, if present, automatically triggers its Domain-Driven Design and architectural verification rules described here.

Handling Detected Violations

ApplicationModules.verify() throws an exception in case of any architectural violation being detected. You can access the violations for further processing, such as ignoring certain violations, by instead calling ApplicationModules.detectViolations().

ApplicationModules.of(…)
  .detectViolations()
  .filter(violation -> …)
  .throwIfPresent();

Customizing the Verifcation

As described above, by default, both the ApplicationModules.verify(…) and ….detectViolations(…) automatically perform additional verifications depending on the classpath configuration.

To customize these, disable them or register additional verifications, both verify(…) and detectVolations(…) take a VerificationOptions instance.

var hexagonal = JMoleculesArchitectureRules.ensureHexagonal(VerificationDepth.LENIENT); (1)
var options = VerificationOptions.defaults().withAdditionalVerifications(hexagonal); (2)

ApplicationModules.of(…).verify(options); (3)
1 Set up the jMolecules Architecture verification for Hexagonal Architecture in lenient mode.
2 Create a VerificationOptions instance replacing the default verification with the one just set up.
3 Execute the verification using the just configured options.