Creating your first Micronaut Graal application

Learn how to create a Hello World Micronaut GraalVM application.

Authors: Iván López, Sergio del Amo

Micronaut Version: 4.6.3

1. Getting Started

In this guide, we will create a Micronaut application written in Kotlin with GraalVM support.

2. What you will need

To complete this guide, you will need the following:

3. Solution

We recommend that you follow the instructions in the next sections and create the application step by step. However, you can go right to the completed example.

4. Writing the Application

Create an application using the Micronaut Command Line Interface or with Micronaut Launch.

mn create-app example.micronaut.micronautguide \
    --features=graalvm \
    --build=maven \
    --lang=kotlin \
    --test=junit
If you don’t specify the --build argument, Gradle with the Kotlin DSL is used as the build tool.
If you don’t specify the --lang argument, Java is used as the language.
If you don’t specify the --test argument, JUnit is used for Java and Kotlin, and Spock is used for Groovy.

The previous command creates a Micronaut application with the default package example.micronaut in a directory named micronautguide.

If you use Micronaut Launch, select Micronaut Application as application type and add graalvm features.

If you have an existing Micronaut application and want to add the functionality described here, you can view the dependency and configuration changes from the specified features, and apply those changes to your application.

4.1. Service

Create a POJO Conference:

src/main/kotlin/example/micronaut/Conference.kt
package example.micronaut

import io.micronaut.serde.annotation.Serdeable;

@Serdeable (1)
data class Conference(val name: String)
1 Declare the @Serdeable annotation at the type level in your source code to allow the type to be serialized or deserialized.

Create a Service:

src/main/kotlin/example/micronaut/ConferenceService.kt
package example.micronaut

import java.util.Random
import jakarta.inject.Singleton

@Singleton (1)
class ConferenceService {

    fun randomConf(): Conference = CONFERENCES[Random().nextInt(CONFERENCES.size)] (2)

    companion object {
        private val CONFERENCES = listOf(
                Conference("Greach"),
                Conference("GR8Conf EU"),
                Conference("Micronaut Summit"),
                Conference("Devoxx Belgium"),
                Conference("Oracle Code One"),
                Conference("CommitConf"),
                Conference("Codemotion Madrid")
        )
    }
}
1 Use jakarta.inject.Singleton to designate a class as a singleton.
2 Return a random conference.

4.2. Controller

Create a Controller with a method that returns a Conference. The Micronaut framework will convert it automatically to JSON in the response:

src/main/kotlin/example/micronaut/ConferenceController.kt
package example.micronaut

import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get

@Controller("/conferences") (1)
class ConferenceController(private val conferenceService: ConferenceService) { (2)

    @Get("/random") (3)
    fun randomConf(): Conference = conferenceService.randomConf() (4)
}
1 The class is defined as a controller with the @Controller annotation mapped to the path /conferences.
2 Constructor injection
3 The @Get annotation maps the index method to an HTTP GET request on /random.
4 Return a Conference.

5. Generate a Micronaut Application Native Executable with GraalVM

We will use GraalVM, an advanced JDK with ahead-of-time Native Image compilation, to generate a native executable of this Micronaut application.

Compiling Micronaut applications ahead of time with GraalVM significantly improves startup time and reduces the memory footprint of JVM-based applications.

Only Java and Kotlin projects support using GraalVM’s native-image tool. Groovy relies heavily on reflection, which is only partially supported by GraalVM.

5.1. GraalVM Installation

The easiest way to install GraalVM on Linux or Mac is to use SDKMan.io.

Java 21
sdk install java 21.0.5-graal

For installation on Windows, or for a manual installation on Linux or Mac, see the GraalVM Getting Started documentation.

The previous command installs Oracle GraalVM, which is free to use in production and free to redistribute, at no cost, under the GraalVM Free Terms and Conditions.

Alternatively, you can use the GraalVM Community Edition:

Java 21
sdk install java 21.0.2-graalce

5.2. Native Executable Generation

To generate a native executable using Maven, run:

./mvnw package -Dpackaging=native-image

The native executable is created in the target directory and can be run with target/micronautguide.

It is possible to customize the name of the native executable or pass additional build arguments using the Maven plugin for GraalVM Native Image building. Declare the plugin as following:

pom.xml
<plugin>
    <groupId>org.graalvm.buildtools</groupId>
    <artifactId>native-maven-plugin</artifactId>
    <version>0.10.3</version>
    <configuration>
        <!-- <1> -->
        <imageName>mn-graalvm-application</imageName> (1)
        <buildArgs>
              <!-- <2> -->
          <buildArg>-Ob</buildArg>
        </buildArgs>
    </configuration>
</plugin>
1 The native executable name will now be mn-graalvm-application.
2 It is possible to pass extra build arguments to native-image. For example, -Ob enables the quick build mode.

5.3. Creating a Native Executable Inside Docker

The output following this approach is a Docker image that runs the native executable of your application. You don’t need to install any additional dependencies.

Building a GraalVM native executable with Maven
./mvnw package -Dpackaging=docker-native

5.4. Running the Native Executable

Execute the application by either running the executable or starting the Docker container.

Executing the native executable
10:29:46.845 [main] INFO  io.micronaut.runtime.Micronaut - Startup completed in 12ms. Server Running: http://localhost:8080

We can see that the application starts in only 12ms.

5.5. Sending a Request

Start the application either using Docker or the native executable. You can run a few cURL requests to test the application:

time curl localhost:8080/conferences/random
{"name":"Greach"}
real    0m0.016s
user    0m0.005s
sys     0m0.004s
time curl localhost:8080/conferences/random
{"name":"GR8Conf EU"}
real    0m0.014s
user    0m0.005s
sys     0m0.004s

6. Next Steps

Take a look at the Micronaut Gradle plugin and Micronaut Maven Plugin documentation.

7. Help with the Micronaut Framework

The Micronaut Foundation sponsored the creation of this Guide. A variety of consulting and support services are available.

8. License

All guides are released with an Apache license 2.0 license for the code and a Creative Commons Attribution 4.0 license for the writing and media (images…​).