How to execute a command during the creation of a war?

My environment: Grails v2.1.1

I need to run a small utility application during the war process. This application generates files that I want to include in my war file. I tried to put the code in BuildConfig.groovy grails.war.resources, but I do not see the errors or files that I expect to create.

Does anyone know how I can run this utility application so that its output is in my war?

This is the command executed inside the terminal instance:

sencha app build -e production -d $stagingDir/production

Here is my attempt to run it through grails.war.resourcesin 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 there a grails.war.resourcestransition method?

Update

For posterity, here is my somewhat complicated example using the answer below.

from _Events.groovyfile

/**
 * 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.'

}
+5
source share
1

eventCreateWarStart scripts/_Events.groovy. : WAR stagingDir

eventCreateWarStart = { warName, stagingDir ->
  // ..
}

ant, AntBuilder, ,

ant.exec(executable:'sencha') {
  arg(value:'app')
  arg(value:'build')
  // ...
}
+9

All Articles