Slackbot
11/09/2023, 3:39 PMrobertjstroud
11/09/2023, 4:08 PMbuildProperties['build.artifact'] rather than buildProperties.artifact - in other words, the property names have a build prefix. But it's good enough for my purposes...tylervz
11/09/2023, 6:50 PMgrgit-gradle plugin (https://github.com/ajoberstar/grgit) in our Grails 5.3.2 app to get the current Git revision hash when the application is built.
You can use version 5.x of the plugin (and remove the ajoberstar-backup Maven repositories from build.gradle) if you're using Java 11+.
Here's the relevant parts of our `build.gradle`:
buildscript {
repositories {
maven { url "<https://plugins.gradle.org/m2/>" }
maven { url "<https://repo.grails.org/grails/core>" }
maven {
name = 'ajoberstar-backup'
url = '<https://ajoberstar.org/bintray-backup>'
}
}
dependencies {
classpath "org.grails:grails-gradle-plugin:$grailsGradlePluginVersion"
classpath "org.grails.plugins:hibernate5:7.3.0"
classpath "gradle.plugin.com.github.erdi.webdriver-binaries:webdriver-binaries-gradle-plugin:2.6"
classpath "com.bertramlabs.plugins:asset-pipeline-gradle:3.4.7"
classpath "org.grails.plugins:views-gradle:2.3.2"
classpath "org.ajoberstar.grgit:grgit-gradle:4.1.1"
}
}
version "6.8.1"
group "exampleapp"
apply plugin:"eclipse"
apply plugin:"idea"
apply plugin:"war"
apply plugin:"org.grails.grails-web"
apply plugin:"com.github.erdi.webdriver-binaries"
apply plugin:"com.bertramlabs.asset-pipeline"
apply plugin:"org.grails.grails-gsp"
apply plugin: "org.grails.plugins.views-json"
apply plugin: "org.ajoberstar.grgit"
repositories {
maven { url "<https://repo.grails.org/grails/core>" }
maven {
name = 'ajoberstar-backup'
url = '<https://ajoberstar.org/bintray-backup>'
}
mavenCentral()
}
...
/**
* Add build info to <http://grails.build.info|grails.build.info> file.
* Modified example from <https://objectcomputing.com/news/2017/04/02/add-build-info-your-project>
*/
buildProperties.doLast {
// Find the right file
File grailsBuildInfoFile = it.outputs.files.files.find { it.name == '<http://grails.build.info|grails.build.info>' } as File
if (!grailsBuildInfoFile) {
return // No need to continue if the file is not there
}
Properties properties = new Properties()
// Read properties from the file
grailsBuildInfoFile.withInputStream {
properties.load(it)
}
// 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('build.java.version', System.getProperty('java.version'))
// Get the host name where the build was created
properties.setProperty('build.host', InetAddress.localHost.hostName)
// Add property set by your CI (in this case CircleCI)
Map<String, String> env = System.getenv()
if (env.CIRCLE_BUILD_NUM) {
properties.setProperty('build.number', env.CIRCLE_BUILD_NUM)
properties.setProperty('build.git.revision', env.CIRCLE_SHA1)
} else {
// Get the commit id of HEAD
String gitCommitId = grgit.head().id
properties.setProperty('build.git.revision', gitCommitId)
}
// Write the properties back to the file
grailsBuildInfoFile.withOutputStream {
properties.store(it, null)
}
}
...
And then we have a Grails service named MetaInfoService which fetches and holds the info from the META-INF/grails.build.info file.
groovy
import grails.compiler.GrailsCompileStatic
import grails.config.Config
import grails.core.GrailsApplication
import grails.core.support.GrailsConfigurationAware
@GrailsCompileStatic
class MetaInfoService implements GrailsConfigurationAware {
GrailsApplication grailsApplication
/**
* The Grails environment currently being used i.e. "development", "test", or "production".
*/
String applicationEnvironment
/**
* The name of this Grails application.
*/
String applicationName
/**
* The version of this Grails application i.e. "6.4.5"; it is the version specified in build.gradle.
*/
String applicationVersion
/**
* The build number of the application. This is only set when the application is built by a
* CI (continuous integration) system.
*/
String applicationBuild
/**
* The Git revision of the source code when the application was built. This is only set when
* the application is built into a runnable war or jar and then deployed.
*/
String applicationBuildRevision
/**
* The time when the application was built, specified as a String in the format "yyyy-MM-dd HH:mm:ss".
* This is only set when the application is built into a runnable war or jar and then deployed.
*/
String applicationBuildTS
/**
* The Grails version used to build the application.
*/
String grailsVersion
/**
* The Java version used when the application was built i.e. "1.8.0_345". This is only set when
* the application is built into a runnable war or jar and then deployed.
*/
String buildJavaVersion
/**
* Have these properties set once when the service is created
*
* <http://grailsblog.objectcomputing.com/posts/2016/08/31/retrieving-config-values.html>
*/
@Override
void setConfiguration(Config co) {
applicationEnvironment = System.getProperty("grails.env")
applicationName = grailsApplication.metadata.getApplicationName()
applicationVersion = grailsApplication.metadata.getApplicationVersion()
applicationBuild = grailsApplication.metadata.getOrDefault("build.number", null)
applicationBuildRevision = grailsApplication.metadata.getOrDefault("build.git.revision", null)
applicationBuildTS = grailsApplication.metadata.getOrDefault("build.time", null)
grailsVersion = grailsApplication.metadata.getGrailsVersion()
buildJavaVersion = grailsApplication.metadata.getOrDefault("build.java.version", null)
}
}robertjstroud
11/09/2023, 6:57 PMMetaInfoService class.