Skip to content

Potential NoClassDefFoundError when jMolecules DDD artifact is missing from the classpath  #1333

@StefanPenndorf

Description

@StefanPenndorf

I'm trying to enforce a simple onion architecture on my project. Therefore I've written a test as inspired by the docs:

// imports reduced for clarity
import org.jmolecules.archunit.JMoleculesArchitectureRules;
[...]

public class ArchitectureTests {
    @Test
    void verifyModularity() {
        final ArchRule onionRule = JMoleculesArchitectureRules.ensureOnionSimple();
        final VerificationOptions options = VerificationOptions.defaults().withAdditionalVerifications(onionRule);

        final ApplicationModules modules = ApplicationModules.of(Serveruerbersicht.class).verify(options);
    }
}

and integrated the necessary dependencies in my pom.xml:

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.modulith</groupId>
                <artifactId>spring-modulith-bom</artifactId>
                <version>1.4.2</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
            <dependency>
                <groupId>org.jmolecules</groupId>
                <artifactId>jmolecules-bom</artifactId>
                <version>2023.3.2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
[....]
    <dependencies>
       <!-- Production Scope Dependencies -->
        <dependency>
            <groupId>org.springframework.modulith</groupId>
            <artifactId>spring-modulith-starter-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jmolecules</groupId>
            <artifactId>jmolecules-onion-architecture</artifactId>
        </dependency>

       <!-- Test Scope Dependencies -->
        <dependency>
            <groupId>org.springframework.modulith</groupId>
            <artifactId>spring-modulith-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jmolecules.integrations</groupId>
            <artifactId>jmolecules-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

Unfortunately the test fails because it's missing AggregateRoot from jmolecules-ddd although I don't want to apply DDD at all:

java.lang.NoClassDefFoundError: org/jmolecules/ddd/annotation/AggregateRoot
	at org.jmolecules.archunit.JMoleculesDddRules.<clinit>(JMoleculesDddRules.java:72)
	at org.springframework.modulith.core.Types$JMoleculesTypes.getRules(Types.java:132)
	at org.springframework.modulith.core.VerificationOptions.defaults(VerificationOptions.java:57)
	at my.package.ArchitectureTests.verifyModularity(ArchitectureTests.java:21)
	at java.base/java.lang.reflect.Method.invoke(Method.java:580)
	at java.base/java.util.concurrent.ForkJoinTask.doExec$$$capture(ForkJoinTask.java:387)
	at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java)
	at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1312)
	at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1843)
	at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1808)
	at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:188)
Caused by: java.lang.ClassNotFoundException: org.jmolecules.ddd.annotation.AggregateRoot
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526)
	... 11 more

The code in org.springframework.modulith.core.Types seems to be aware that jmolecules-ddd might be missing because it checks the presence of the class via if (ClassUtils.isPresent(DDD_RULES, classLoader)) {....} before using JMoleculesDddRules. Unfortunately that is not sufficient.

Workaround

For the time beeing I can add jmolecules-ddd as a test dependency to my pom.xml but I hope for a better solution:

        <dependency>
            <groupId>org.jmolecules</groupId>
            <artifactId>jmolecules-ddd</artifactId>
            <scope>test</scope>
        </dependency>

Metadata

Metadata

Assignees

Labels

in: coreCore module meta modeltype: bugSomething isn't working

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions