Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute a command during war creation?

Tags:

grails

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

}
like image 889
John Gordon Avatar asked Sep 25 '12 14:09

John Gordon


People also ask

How do I run a war file executable?

Run the WAR fileOpen up a terminal/command prompt window to the download directory. Run the command java -jar jenkins. war . Browse to http://localhost:8080 and wait until the Unlock Jenkins page appears.

How do you manually start a WAR?

You need to use -c switch of jar, to create the war file. Go inside the project directory of your project (outside the WEB-INF), then write the following command: jar -cvf projectname. war *


1 Answers

You can put an eventCreateWarStart in your scripts/_Events.groovy. This event receives two parameters, the name of the WAR and the stagingDir

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

You have access to the ant variable giving you an AntBuilder so you can do stuff like

ant.exec(executable:'sencha') {
  arg(value:'app')
  arg(value:'build')
  // ...
}
like image 199
Ian Roberts Avatar answered Oct 04 '22 00:10

Ian Roberts