August 17, 2018

Micronaut Mastery: Add Build Info To Info Endpoint

Micronaut has some built-in management endpoints to get information, a list of beans, health checks and more. To enable the endpoints we must add the dependency io.micronaut:management to our application. Then we can add configuration properties to enable the different endpoints. The /info endpoint gathers information from several sources with properties. If we want to add build information we must create a file build-info.properties with information and Micronaut will automatically add the properties from the file to the /info endpoint.

We can choose how we want to create the build-info.properties file. The location is configurable via Micronaut application configuration properties, but the default location is on the classpath at META-INF/build-info.properties. To make life easy for us we reuse the BuildInfo Gradle task from the Spring Boot Gradle plugin to create the build-info.properties file.

In the following example build file we add a build script classpath dependency on the Spring Boot Gradle plugin. This will add the BuildInfo class to our Gradle build file. Next we create a new task buildInfo using the BuildInfo type and we set the destination directory to the resources/main/META-INF directory in the project build directory. This is the default location that Micronaut uses to read the properties for the /info endpoint.

// File: build.gradle
buildscript {
   ...
   dependencies {
       ...
       classpath "org.springframework.boot:spring-boot-gradle-plugin:2.0.4.RELEASE"
       ...
    }
}
...
dependencies {
    ...
    // Add management endpoint support.
    runtime "io.micronaut:management"
    ...
}
...
import org.springframework.boot.gradle.tasks.buildinfo.BuildInfo

task buildInfo(type: BuildInfo) {
    description = 'Generates build-info.properties file.'

    group = BasePlugin.BUILD_GROUP

    destinationDir = new File(sourceSets.main.output.resourcesDir, 'META-INF')

    properties {
        time = null // Otherwise task is never up-to-date
        artifact = shadowJar.baseName
        
        // Extra properties that will be added to build-info.properties.
        additionalProperties = [
                by: System.properties['user.name'],
                operatingSystem: "${System.properties['os.name']} (${System.properties['os.version']})",
                continuousIntegration: System.getenv('CI') ? true: false,
                machine: InetAddress.localHost.hostName,
        ]
    }
}
classes.dependsOn buildInfo
...

In our application configuration we enable the /info endpoint:

# File: src/main/resources/application.yml
...
endpoints:
  info:
    enabled: true
    sensitive: false
...

We start our Micronaut application and invoke the /info endpoint and we get the following data:

{
    "build": {
        "artifact": "micronaut-sample",
        "by": "mrhaki",
        "continuous-integration": "false",
        "group": "mrhaki.micronaut",
        "machine": "mrhaki.fritz.box",
        "name": "micronaut-sample",
        "operating-system": "Mac OS X (10.13.6)",
        "version": "1.0.1"
    }
}

Written with Micronaut 1.0.0.M4.