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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With