mn create-app example.micronaut.micronautguide --build=gradle --lang=groovy
Table of Contents
Adding Commit Info to your Micronaut Application
Expose the exact version of code that your application is running.
Authors: Sergio del Amo
Micronaut Version: 3.9.2
In this guide, we will 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. Getting Started
In this guide, we will create a Micronaut application written in Groovy.
2. 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 11 or greater installed with
JAVA_HOME
configured appropriately
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.
-
Download and unzip the source
Before running the downloaded project, follow the steps described in the Initialize Git Repository section below. |
4. Writing the Application
Create an application using the Micronaut Command Line Interface or with Micronaut Launch.
If you don’t specify the --build argument, Gradle is used as the build tool. If you don’t specify the --lang argument, Java is used as the language.
|
The previous command creates a Micronaut application with the default package example.micronaut
in a directory named micronautguide
.
5. Initialize Git Repository
The project aims to demonstrate how to provide Git commit information to the
/info
endpoint and in order for that to work the project needs to be in a Git repository. After creating the project, initialize a Git repository from the root of the newly created project:
cd micronautguide
git init
git add .
git commit -a "Initial project"
6. 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.
implementation("io.micronaut:micronaut-management")
7. 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:
endpoints:
info:
enabled: true
sensitive: false
8. Gradle Git Properties Plugin
If a git.properties
file is available on the classpath, the GitInfoSource will expose the values in that file under the git
key. Generation of a git.properties
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 git.properties
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 plugin:
plugins {
id "com.gorylenko.gradle-git-properties" version "@gradle-git-propertiesVersion@"
}
9. Test
Create a JUnit test to verify that when you make 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": "sergio.delamo@softamo.com"
}
},
"branch": "master"
}
}
Create a JUnit test to verify the behaviour:
package example.micronaut
import io.micronaut.http.HttpRequest
import io.micronaut.http.HttpResponse
import io.micronaut.http.client.HttpClient
import io.micronaut.http.client.annotation.Client
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import spock.lang.Shared
import spock.lang.Specification
import jakarta.inject.Inject
@MicronautTest (1)
class InfoSpec extends Specification {
@Shared
@Client("/")
@Inject
HttpClient client (2)
void 'test git commit info appears in JSON'() {
given:
HttpRequest request = HttpRequest.GET('/info') (3)
when:
HttpResponse<Map> rsp = client.toBlocking().exchange(request, Map)
then:
rsp.status().code == 200
when:
Map json = rsp.body() (4)
then:
json.git
json.git.commit
json.git.commit.message
json.git.commit.time
json.git.commit.id
json.git.commit.user
json.git.branch
}
}
1 | Annotate the class with @MicronautTest so the Micronaut framework will initialize the application context and the embedded server. More info. |
2 | Inject the HttpClient bean and point it to the embedded server. |
3 | Creating HTTP Requests is easy thanks to the Micronaut framework fluid API. |
4 | Use .body() to retrieve the parsed payload. |
10. Testing the Application
To run the tests:
./gradlew test
Then open build/reports/tests/test/index.html
in a browser to see the results.
11. Running the Application
To run the application, use the ./gradlew run
command, which starts the application on port 8080.
12. Next steps
Explore more features with Micronaut Guides.
13. Help with the Micronaut Framework
The Micronaut Foundation sponsored the creation of this Guide. A variety of consulting and support services are available.