My environment: Grails v2.1.1
I need to run a small utility app during the war process. This app generates files that I want included in my war file. I’ve tried putting code in BuildConfig.groovy’s grails.war.resources, but I’m not seeing an error, or the files I am expecting to be created.
Does anyone know how I can execute this utility app so that its output is in my war?
This is the command as run inside a terminal instance:
sencha app build -e production -d $stagingDir/production
Here’s my attempt to run it via grails.war.resources in BuildConfig.groovy:
grails.war.resources = { stagingDir ->
//calling echo() does nothing. I don't see the comment in the build output
echo(message:'executing grails.war.resources')
def outputDir = new File("${stagingDir.getParentFile().getPath()}/target/ranForReal")
def command = """sencha app build -e testing -d ${outputDir.getPath()}"""
def executionDir = new File("${stagingDir.getParentFile().getPath()}/web-app")
def proc = command.execute(null,executionDir)
proc.waitFor()
//my desperate attempt to see if anything is happening. I'd expect an error here
def x = 1/0
// Obtain status and output
println "return code: ${ proc.exitValue()}"
println "stderr: ${proc.err.text}"
println "stdout: ${proc.in.text}" // *out* from the external program is *in* for groovy
//this for loop does work and does remove servlet jars, so I know this closure is called.
for (name in ['servlet']) {
delete {
fileset dir: "$stagingDir/WEB-INF/lib/",
includes: "$name*.jar"
}
}
}
Is grails.war.resources the way to go?
Update
For posterity, here is my somewhat complex example, using the answer below.
from _Events.groovy file
/**
* Generate an optimized version of the sencha app.
*/
eventCreateWarStart = {warName, stagingDir ->
//argsMap contains params from command line, e.g 'war --sencha.env=production'
def senchaEnvironment = argsMap["sencha.env"] ?: 'testing'
//println is the only way I've found to write to the console.
println "running sencha optimizer code for $senchaEnvironment environment..."
ant.exec(outputproperty: "cmdOut", executable:'sencha',
dir:"$stagingDir",failOnError:true){
arg(value:'app')
arg(value:'build')
arg(value:"-e $senchaEnvironment" )
}
println "${ant.project.properties.cmdOut}"
println'completed sencha optimization process.'
}
You can put an
eventCreateWarStartin yourscripts/_Events.groovy. This event receives two parameters, the name of the WAR and the stagingDirYou have access to the
antvariable giving you anAntBuilderso you can do stuff like