Show Navigation

How to Add Build Info to Your Project

By Søren Berg Glasius & Colin Harrington

April 2, 2017


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 the Grails framework, we were able to add arbitrary properties to the file. The Grails framework provides this information through the grails.util.Metadata class, and for views this is easily made available through the tag.

Since Grails 3 is built with Gradle, the old is no longer in use. Instead we'll add properties to

The Basics

When you build/run your Grails applications, one of tasks that Gradle always executes is 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/ The contents of this file looks like:

#Thu, 02 Feb 2017 15:30:37 +0100

This info primarily comes from build.gradle and (the curious can check out the source code in grails-core). This file will be part of your final application when you build your .jar or .war file.

In a .gsp, this information is available by using the meta tag like this: (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, and then we want to add additional properties to the file.

To accomplish this, add the following snippet to build.gradle in your project:

buildProperties.doLast {
    // Find the right file
    File grailsBuildInfoFile = it.outputs.files.files.find { == '' }
    if(!grailsBuildInfoFile) return // No need to continue if the file is not there
    Properties properties = new Properties()
    // Read properties from the file
    grailsBuildInfoFile.withInputStream {
    // Add new properties from various sources
    properties.setProperty('build.time', new Date().format("yyyy-MM-dd HH:mm:ss"))
    // Get a System property
    properties.setProperty('', System.getProperty('java.version'))
    // Get the host name where the build was created
    properties.setProperty('', InetAddress.localHost.hostName)
    // Add property set by your CI (in this case Bamboo)
    Map<String, String> env = System.getenv()
    if(env.bamboo_buildNumber) {
        properties.setProperty('build.number', env.bamboo_buildNumber)
        properties.setProperty('build.git.revision', env.bamboo_planRepository_revision)
        properties.setProperty('build.git.branch', env.bamboo_planRepository_branch)
    // Write the properties back to the file
    grailsBuildInfoFile.withOutputStream {,null)

This example shows:

  • How to set build.time from the time that the build was executed
  • How to set values from
  • How to get your build host name
  • 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, we can show it in our application in a .gsp page.

For example:

        <tr><td>Build time</td><td><g:meta name="build.time"></g:meta>></td></tr>
        <tr><td>Build java version</td><td><g:meta name=""></g:meta>></td></tr>
        <tr><td>Build host</td><td><g:meta name=""></g:meta>></td></tr>

... and so on.

NOTE: If you use the Spring Boot Actuator endpoints, you can write your own custom InfoContributor that uses Grails Metadata!

Alternatively, you can leverage the Auto-configured InfoContributors to:


This blog-post shows how you can easily 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.

You might also like ...