如何在战争创造期间执行命令?

Joh*_*don 5 grails

我的环境:Grails v2.1.1

我需要在战争过程中运行一个小型实用程序应用程序.这个应用程序生成我想要包含在我的war文件中的文件.我已经尝试将代码放在BuildConfig.groovy的grails.war.resources中,但我没有看到错误,或者我希望创建的文件.

有谁知道如何执行这个实用程序的应用程序,以便它的输出在我的战争?

这是在终端实例中运行的命令:

sencha app build -e production -d $stagingDir/production
Run Code Online (Sandbox Code Playgroud)

这是我尝试通过运行它grails.war.resourcesBuildConfig.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"
    }
}

}
Run Code Online (Sandbox Code Playgroud)

grails.war.resources要走的路?

更新

对于后代,这是我的一些复杂的例子,使用下面的答案.

来自_Events.groovy档案

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

}
Run Code Online (Sandbox Code Playgroud)

Ian*_*rts 9

你可以放入eventCreateWarStart你的scripts/_Events.groovy.此事件接收两个参数,WAR的名称和stagingDir

eventCreateWarStart = { warName, stagingDir ->
  // ..
}
Run Code Online (Sandbox Code Playgroud)

你可以访问ant变量给你一个AntBuilder所以你可以做的事情

ant.exec(executable:'sencha') {
  arg(value:'app')
  arg(value:'build')
  // ...
}
Run Code Online (Sandbox Code Playgroud)