spring-projects / spring-projects/spring-modulith

Force allowed dependencies to be named interfaces instead of whole modules

Open
#1,334 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
1.2k
Forks
222
PR merge metrics
No merged PRs in 30d

Description

Currently in our project, we are migrating from a spaghetti ball to a modular structure. We find 2 rules very useful in our case:

  1. It really helps if the dependencies are explicitly referring to a specific named interface, never a whole module.
  2. Every module HAS to explicitly specify it's allowed dependencies, even if there are none

We use these three archunit tests to enforce this:

1. Enforce only named interfaces in allowed dependencies
    @Test
    void allowedDependenciesShouldAlwaysBeNamedInterfaces() {
        var rootPackage = importedClasses.getPackage("com.something");

        var violations = rootPackage
            .getSubpackages()
            .stream()
            .filter(pkg ->
                Arrays.stream(pkg.getAnnotationOfType(ApplicationModule.class).allowedDependencies()).anyMatch(
                    allowedDependency -> !allowedDependency.contains("::")
                )
            )
            .map(
                pkg ->
                    pkg.getDescription() +
                    " has a module name in the `allowedDependencies` instead of a named interface. Only use specific named interface dependencies. E.g. instead of \"core\" use \"core :: configuration\""
            );

        assertThat(violations)
            .as(
                "Because that way we can keep modulith as strict as possible to prevent accidental additional dependencies in the future"
            )
            .isEmpty();
    }
2. Enforce explicit allowed dependencies for every module
    @Test
    void applicationModulesShouldSpecifyTheirDependencies() {
        var defaultAllowedDependencies = "[%s]".formatted(ApplicationModule.OPEN_TOKEN);
        var rootPackage = importedClasses.getPackage("com.something");

        var violations = rootPackage
            .getSubpackages()
            .stream()
            .filter(pkg ->
                Arrays.toString(pkg.getAnnotationOfType(ApplicationModule.class).allowedDependencies()).equals(
                    defaultAllowedDependencies
                )
            )
            .map(
                pkg ->
                    pkg.getDescription() +
                    " does not set `allowedDependencies` in the @" +
                    ApplicationModule.class.getSimpleName() +
                    "annotation"
            );

        assertThat(violations)
            .as("Because a module should explicitly declare it's dependencies so that it is immediately visible")
            .isEmpty();
    }

Would it be of interest for spring-modulith to include these as optional configuration options in the library? I'd be open to try and create a PR for this if this is something that would be a welcome addition.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading ApplicationModule.allowedDependencies and the existing ArchUnit tests shown in the issue. Trace how module metadata is validated and how optional library configuration is exposed. Done means both named-interface enforcement and explicit dependency declarations can be enabled as optional configuration, with tests covering each rule.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring-boot
Domain
backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.