When you build and run Grails 3 applications, often you may want to know about the build environment via the running application.
Since the early days of Grails, we were able to add arbitrary properties to the
application.properties file. Grails provides this information through the
grails.util.Metadata class, and for views this is easily made available through the
Since Grails 3 is built with Gradle, the old
application.properties is no longer in use, instead we’ll add properties to
When you build/run your Grails applications, one of tasks that Gradle always executes is the
buildProperties. This task takes a small set of build information from your project, and places it in a file, which the Grails
Metadata class later reads. This file is located in
META-INF and is named
At build time you can find this file at
build/resources/main/META-INF/grails.build.info. The contents of this file looks like:
This info primarily comes from
gradle.properties (the curious can check out the source code in grails-core). This file will be part of your final application when you build your
.gsp this information is available by using the meta tag like this:
<g:meta name='info.app.version'/> (see the tag documentation).
Adding more information to
With the Gradle build system it is possible to hook into the build process and execute our own code. In this case we want Gradle to complete writing
grails.build.info and then we want to add additional properties to the file.
To accomplish this, add the following snippet to
build.gradle in your project:
This example shows how to set
build.time from the time that the build was executed, how to set values from
System.properties, how to get your build host name, and finally how to read environment variables set by your build server (in the above example by Atlassian Bamboo)
Now that we have this build information captured in
grails.build.info, we can show it in our application in a
.gsp page, for example:
and so on.
NOTE: If you use the Spring Boot Actuator endpoints, you can write your own custom InfoContributor that uses Grails
Alternatively, you can leverage the Auto-configured InfoContributors
to add git commit info,
expose build info by copying
grails.build.info to a
build-info.properties file (BuildInfoContributor),
expose environment info by configuring properties starting with
'info.' such as
This blog-post shows how you easily can add build information to your Grails 3 application by leveraging the tasks already available within the Gradle build, and how you can show them in your application.