Featured image of post Set Up Multiple-Module Project

Set Up Multiple-Module Project

Learning Micronaut Journey - Part 3

Introduction

The expectation of this blog is to guide you through setting up a multiple-module project from the first micronaut application. The anticipated structure after following this blog will be:

1
2
3
4
5
melon-be/
β”œβ”€β”€ app/
β”‚   └── api/
β”œβ”€β”€ core/
└── testing/

In this blog post, we will explore the step-by-step process of setting up a multiple-module project with Micronaut. Let’s get started!

Why Multiple Modules?

Breaking down a monolithic application into smaller, more manageable modules brings several advantages. It promotes code reusability, makes the project more maintainable, and enables parallel development by allowing different teams to work on separate modules independently. Micronaut’s support for multiple modules makes it an excellent choice for projects of all sizes.

Setting Up the Micronaut Project

Creating app/api module

  1. Create the app module manually

  1. The structure after creation:

  1. Add configuration to the root build.gradle
1
2
3
4
subprojects {
    apply(plugin: "kotlin")
    apply(plugin: "java")
}
  1. Include app:api in the settings.gradle file
1
2
3
include(
        "app:api"
)
  1. Synchronize Gradle, and here are the results:

We can use ./gradlew projects to see the sub-modules of our Micronaut application.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
❯ ./gradlew projects

> Task :projects

------------------------------------------------------------
Root project 'melon-be'
------------------------------------------------------------

Root project 'melon-be'
\--- Project ':app'
     \--- Project ':app:api'

Moving code from src/ to app/api/

  1. Move all source code from src/ to app/api/

After relocating all source code, the application structure will be as follows:

Ignore errors for now; we’ll address them shortly.

  1. Update Configuration in root build.gradle file:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
plugins {
    id("org.jetbrains.kotlin.jvm") version "1.9.20"
}

repositories {
    mavenCentral()
}

subprojects {
    apply(plugin: "kotlin")
    apply(plugin: "java")

    repositories {
        mavenCentral()
        maven {
            url = "https://maven.google.com/"
        }
    }

    java {
        toolchain {
            languageVersion = JavaLanguageVersion.of(17)
        }
    }

    kotlin {
        jvmToolchain {
            languageVersion.set(JavaLanguageVersion.of(17))
        }
    }
}
  1. Update Configuration in the app/api/build.gradle file:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
plugins {
    id("com.google.devtools.ksp") version "1.9.20-1.0.14"
    id("io.micronaut.application") version "4.2.0"
    id("io.micronaut.aot") version "4.2.0"
}

version = "0.1"
group = "com.melon"

dependencies {
    ksp("io.micronaut:micronaut-http-validation")
    ksp("io.micronaut.serde:micronaut-serde-processor")
    implementation("io.micronaut.kotlin:micronaut-kotlin-runtime")
    implementation("io.micronaut.serde:micronaut-serde-jackson")
    implementation("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlinVersion}")
    compileOnly("io.micronaut:micronaut-http-client")
    runtimeOnly("ch.qos.logback:logback-classic")
    runtimeOnly("com.fasterxml.jackson.module:jackson-module-kotlin")
    testImplementation("io.micronaut:micronaut-http-client")
}

application {
    mainClass.set("com.melon.ApplicationKt")
}

graalvmNative.toolchainDetection = false
micronaut {
    runtime("netty")
    testRuntime("junit5")
    processing {
        incremental(true)
        annotations("com.melon.*")
    }
    aot {
        // Please review carefully the optimizations enabled below
        // Check https://micronaut-projects.github.io/micronaut-aot/latest/guide/ for more details
        optimizeServiceLoading = false
        convertYamlToJava = false
        precomputeOperations = true
        cacheEnvironment = true
        optimizeClassLoading = true
        deduceEnvironment = true
        optimizeNetty = true
    }
}

Attempt running ./gradlew run, and your Micronaut application should function as usual.

Conclusion

With the foundation in place, you can now easily add more modules, such as app/module-postgresql or core/module-logging, extending the modularity and scalability of your Micronaut project.

Happy coding!

comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy