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 a Groovy Micronaut app using the Micronaut Command Line Interface.

mn create-app example.micronaut.complete --features=groovy

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

Due to the --features groovy flag, it generates a Groovy Micronaut app and it uses Gradle build system. However, you could use other build tool such as Maven or other programming languages such as Java or Kotlin.

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 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 Spock feature method 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.RxHttpClient
import io.micronaut.runtime.server.EmbeddedServer
import spock.lang.AutoCleanup
import spock.lang.Shared
import spock.lang.Specification

class InfoSpec extends Specification {

    @AutoCleanup (1)
    EmbeddedServer embeddedServer =  (2)

    RxHttpClient client = embeddedServer.applicationContext.createBean(RxHttpClient, embeddedServer.getURL()) (3)

    def "test git commit info appears in JSON"() {
        HttpRequest request = HttpRequest.GET('/info') (4)

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

        rsp.status().code == 200

        Map json = rsp.body() (5)

1 The AutoCleanup extension makes sure the close() method of an object (e.g. EmbeddedServer) is called each time a feature method is finished
2 To run the application from a unit test you can use the EmbeddedServer interface
3 Register a RxClient 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.
4 Creating HTTP Requests is easy thanks to Micronaut’s fluid API.
5 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.