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

时间:2012-09-25 14:44:43

标签: grails

我的环境:Grails v2.1.1

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

有谁知道如何执行此实用程序应用程序以使其输出在我的战争中?

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

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

我尝试通过grails.war.resources中的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"
    }
}

}

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

}

1 个答案:

答案 0 :(得分:9)

您可以在eventCreateWarStart中添加scripts/_Events.groovy。此事件接收两个参数,WAR的名称和stagingDir

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

您可以访问ant变量,为您提供AntBuilder,以便您可以执行以下操作

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