Skip to content

Project Structure

Somkiat Puisungnoen edited this page Sep 2, 2026 · 2 revisions

Project Structure

1. Verify Structure with ArchUnit

File pom.xml

<dependency>
	<groupId>com.tngtech.archunit</groupId>
	<!-- <artifactId>archunit-junit5</artifactId> --> <!-- for JUnit 5 -->
	<artifactId>archunit-junit6</artifactId>
	<version>1.5.0</version>
	<scope>test</scope>
</dependency>

Create Test case

@AnalyzeClasses(packages = "com.example.demo01")
public class ArchitectureTest {

    @ArchTest
    static final ArchRule layer_dependencies = layeredArchitecture()
            .consideringAllDependencies()
            .layer("Controller").definedBy("..controller..")
            .layer("Service").definedBy("..service..")
            .layer("Repository").definedBy("..repository..")

            // Define allowed dependencies
            .whereLayer("Controller").mayNotBeAccessedByAnyLayer()
            .whereLayer("Service").mayOnlyBeAccessedByLayers("Controller")
            .whereLayer("Repository").mayOnlyBeAccessedByLayers("Service");

    @ArchTest
    static final ArchRule controller_naming = classes()
            .that().resideInAPackage("..controller..")
            .should().haveSimpleNameEndingWith("Controller")
            .andShould().beAnnotatedWith(RestController.class)
            .andShould().onlyDependOnClassesThat().resideInAnyPackage("java..", "org..", "..service..");

    @ArchTest
    static final ArchRule service_classes = classes()
            .that().resideInAPackage("..service..")
            .should().haveSimpleNameEndingWith("Service")
            .andShould().beAnnotatedWith(Service.class)
            .andShould().onlyDependOnClassesThat().resideInAnyPackage("java..", "org..", "..repository..");


}

2. Spring Modulith

  • Add dependencies to project

Create test cases

public class ModulithStructureTest {
    ApplicationModules modules = ApplicationModules.of(Demo02Application.class);

    @Test
    void verifyArchitecture() {
        // Automatically checks for cyclic dependencies and illegal package imports
        modules.verify();
    }

    @Test
    void writeDocumentation() {
        // Automatically creates Asciidoc/PlantUML diagrams of your system inside target/modulith-docs
        new Documenter(modules)
                .writeModulesAsPlantUml()
                .writeDocumentation()
                .writeModuleCanvases()
                .writeIndividualModulesAsPlantUml();
    }
}

Run

$mvnw clean package

Clone this wiki locally