Adding Commit Info to your Micronaut Application

Expose the exact version of code that your application is running.

Authors: Sergio del Amo

Micronaut Version: 1.1.0

1 Getting Started

In this guide we are going to add git commit info to your Micronaut build artifacts and running application. There are many benefits of keeping your commit info handy:

  • Commit info is encapsulated within the built artifacts

  • Fast authoritative means of identifying what specific code is running in an environment

  • This solution doesn’t rely on external tracking mechanisms

  • Transparency and reproducibility when investigating issues

1.1 What you will need

To complete this guide, you will need the following:

  • Some time on your hands

  • A decent text editor or IDE

  • JDK 1.8 or greater installed with JAVA_HOME configured appropriately

1.2 Solution

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


Then, cd into the complete folder which you will find in the root project of the downloaded/cloned project.

2 Writing the App

Create an app using the Micronaut Command Line Interface.

mn create-app example.micronaut.complete

The previous command creates a micronaut app with the default package example.micronaut in a folder named complete.

By default, create-app generates a Java Micronaut app and it uses Gradle build system. However, you could use other build tool such as Maven or other programming languages such as Groovy or Kotlin.

If you are using Java or Kotlin and IntelliJ IDEA make sure you have enabled annotation processing.


3 Management

Inspired by Spring Boot and Grails, the Micronaut management dependency adds support for monitoring of your application via endpoints: special URIs that return details about the health and state of your application.

To use the management features described in this section, add the dependency on your classpath. For example, in build.gradle

dependencies {
    compile "io.micronaut:micronaut-management"

4 Info endpoint

The info endpoint returns static information from the state of the application. The info exposed can be provided by any number of "info sources".

Enable the info endpoint:

        enabled: true
        sensitive: false

5 Gradle Git Properties Plugin

If a file is available on the classpath, the GitInfoSource will expose the values in that file under the git key. Generating of a file will need to be configured as part of your build; for example, you may choose to use the Gradle Git Properties plugin. The plugin provides a task named generateGitProperties responsible for the file generation. It is automatically invoked upon the execution of the classes task. You can find the generated file in the directory build/resources/main.

Modify build.gradle file to add the Gradle plugin:

plugins {
    id "com.gorylenko.gradle-git-properties" version "2.0.0"

6 Test Info endpoint

Create a Junit test which verifies that when you do a GET request to /info you get a payload such as:

  "git": {
    "dirty": "true",
    "commit": {
      "id": "7368906193527fbf2b45f1ed5b08c56631f5b155",
      "describe": "7368906-dirty",
      "time": "1527429126",
      "message": {
        "short": "Initial version",
        "full": "Initial version"
      "user": {
        "name": "sdelamo",
        "email": ""
    "branch": "master"

Create a JUnit test to verify the behaviour:

package example.micronaut;

import io.micronaut.context.ApplicationContext;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.client.HttpClient;
import io.micronaut.runtime.server.EmbeddedServer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import java.util.Map;

import static junit.framework.TestCase.assertNotNull;
import static org.junit.Assert.assertEquals;

public class InfoTest {
    private static EmbeddedServer server;
    private static HttpClient client;

    public static void setupServer() {
        server =; (1)
        client = server
                .createBean(HttpClient.class, server.getURL()); (2)

    public static void stopServer() {
        if(server != null) {
        if(client != null) {

    public void testGitComitInfoAppearsInJson() {

        HttpRequest request = HttpRequest.GET("/info"); (3)

        HttpResponse<Map> rsp = client.toBlocking().exchange(request, Map.class);

        assertEquals(rsp.status().getCode(), 200);

        Map json = rsp.body(); (4)

        assertNotNull(((Map) json.get("git")).get("commit"));
        assertNotNull(((Map) ((Map) json.get("git")).get("commit")).get("message"));
        assertNotNull(((Map) ((Map) json.get("git")).get("commit")).get("time"));
        assertNotNull(((Map) ((Map) json.get("git")).get("commit")).get("id"));
        assertNotNull(((Map) ((Map) json.get("git")).get("commit")).get("user"));
        assertNotNull(((Map) json.get("git")).get("branch"));
1 To run the application from a unit test you can use the EmbeddedServer interface
2 Register an HttpClient bean in the application context and point it to the embedded server URL. The EmbeddedServer interface provides the URL of the server under test which runs on a random port.
3 Creating HTTP Requests is easy thanks to Micronaut’s fluid API.
4 Use .body() to retrieve the parsed payload.

7 Testing the Application

To run the tests:

$ ./gradlew test
$ open build/reports/tests/test/index.html

8 Running the Application

To run the application use the ./gradlew run command which will start the application on port 8080.