Showing posts with label Groovy. Show all posts
Showing posts with label Groovy. Show all posts
I took inspiration from this post, but I wanted to use Groovy. I know there are plugins that do the same, but this was intended to be a learning exercise.
Whilst the example in the link is quite simple - I also need to address authentication of the user. Once available this would be used for displaying build status via a wall mounted monitor.
Requires "commons-codec-1.8.jar" and "commons-httpclient-3.1.jar" on the classpath

Whilst the example in the link is quite simple - I also need to address authentication of the user. Once available this would be used for displaying build status via a wall mounted monitor.
Requires "commons-codec-1.8.jar" and "commons-httpclient-3.1.jar" on the classpath
This simple Gist adds 2 additional output listeners; Standard Out and Standard Error and pipes their output to a build log.
There's small changes you make when developing that you often disregard as too simple to matter. One such example is shown below.
Dates should be ordered: YEAR, MONTH, DAY. (e.g. YYYYMMDD, YYMMDD, YYYYMM). Time should be ordered: HOUR, MINUTES, SECONDS (HHMMSS). The reason for this is that files will always be sorted in correct chronological order.
As a build manager I use this most in my Gradle scripts for ordering the build output logs.
Dates should be ordered: YEAR, MONTH, DAY. (e.g. YYYYMMDD, YYMMDD, YYYYMM). Time should be ordered: HOUR, MINUTES, SECONDS (HHMMSS). The reason for this is that files will always be sorted in correct chronological order.
As a build manager I use this most in my Gradle scripts for ordering the build output logs.
When restarting my Grails application in development mode, it was becoming a pain to re-enter the test data to show people how the application worked.
Groovy provides great support for XML, and so using that for loading data was a no brainer.
First step, create a service:
This service is called from within BootStrap.groovy:
This service imports XML data into domain objects. If a domain object refers to another domain object, we user the power of GORM findBy. The implementation of load() is shown below:
And finally an example dataset:
Unfortunately, because of the format of XML conflicting with HTML I've had to provide a screenshot.
Inspiration taken from Sanjay Mysoremutt's Blog
Groovy provides great support for XML, and so using that for loading data was a no brainer.
First step, create a service:
class LoadDbService {
def fileName
LoadDbService(def fileName) {
this.fileName = fileName
}
def load (){
... detailed below ...
}
}
This service is called from within BootStrap.groovy:
class BootStrap {
def init = { servletContext ->
environments {
development {
def dbs = new LoadDbService('grails-app/resources/test_data/test_records.xml')
dbs.load()
}
}
}
This service imports XML data into domain objects. If a domain object refers to another domain object, we user the power of GORM findBy. The implementation of load() is shown below:
def data = new XmlSlurper().parse(fileName)
// These do not have dependencies and can therefore be generated easily
data.Release.each {
new Release(name: "${it.'@name'}", status: "${it.'@status'}", createdDate: new Date().parseToStringDate("${it.'@createdDate'}")).save(failOnError: true)
}
data.Stack.each {
new Stack(number: "${it.@'number'}").save(failOnError: true)
}
// for these imports to work correctly they need to search for the items that they relate to using .findByName
data.Version.each {
new Version(appName: "${it.@'appName'}", tag: "${it.@'tag'}", createdDate: new Date().parseToStringDate("${it.'@createdDate'}"), partOf: Release.findByName("${it.@'partOf'}")).save(failOnError: true)
}
data.Environment.each {
new Environment(hostname: "${it.@'hostname'}", testTier: "${it.@'testTier'}", lastRefresh: new Date().parseToStringDate("${it.'@lastRefresh'}"), ownedBy: Release.findByName("${it.@'ownedBy'}"), partOf: Stack.findByNumber("${it.@'partOf'}")).save(failOnError: true)
}
And finally an example dataset:
Unfortunately, because of the format of XML conflicting with HTML I've had to provide a screenshot.
Inspiration taken from Sanjay Mysoremutt's Blog
When creating a release package its nice to maintain a copy of all the metadata. I like to do this using the power of Groovy from within my Gradle build scripts.
import groovy.xml.MarkupBuilder
task xmlGen << {
def tag = "git describe --abbrev=0 --tags".execute().text
def sha = "git rev-parse HEAD".execute().text
def fw = new FileWriter("build_info.xml" )
def xml = new groovy.xml.MarkupBuilder(fw)
xml.build(id:tag){
ProjectName("ORCA")
SHA1(sha)
Date(new Date())
Components("component1, component2, component3")
}
}
And the output is:
<build id="v1.3.0">
<ProjectName>MyProject</ProjectName>
<SHA1>1f661e69797dc23281d2955d6ca2dda3cdd81dc0</SHA1>
<Date>Thu May 02 16:14:33 BST 2013</Date>
<Components>component1, component2, component3</Components>
</build>
Subscribe to:
Posts (Atom)
