Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect build time in grails? Also, specify commit revision while building war

I have to display few deployment level parameters from the running app without using external database. First one is the war build date time. Is there any way that my tomcat deployed grails application know when the build was made?

Also, Is there any way through which we can specify commit revision for the war which can then be read by application? The script for reading revision number is external task but what I am concerned about is passing this value while building war automatically and application reading it when deployed.

like image 924
Sumit Shrestha Avatar asked Oct 02 '22 13:10

Sumit Shrestha


1 Answers

You can do this through scripts/_Events.groovy by adding an eventCreateWarStart. This event fires after the final webapp's contents have been assembled in the staging directory but before they are zipped up to make a WAR. You can create new files in the staging directory to have them included in the WAR. For example:

eventCreateWarStart = { warName, stagingDir ->
  ant.exec(executable:'read-revision-number',
           output:"${stagingDir}/WEB-INF/classes/revision.txt") {
    arg(value:'any-argument-the-script-needs')
  }
}

This would run the script and put its output into the WAR where you could read it from code using AnyClassInYourApplication.getResource("/revision.txt").

You can use exactly the same principle for the build time, by putting something like

new File(stagingDir, "WEB-INF/classes/build.timestamp").text = "${new Date()}"

within the eventCreateWarStart.

If you want to be really clever, have your script output the details in a Java-compatible "properties" format file, e.g.

my.app.revision=123456
my.app.build.timestamp=Fri Nov 01 14:25:30 GMT 2013

and then in your app's Config.groovy add classpath:build-details.properties to grails.config.locations, and then the details will be available through the grailsApplication.config.

like image 185
Ian Roberts Avatar answered Oct 13 '22 12:10

Ian Roberts