5

我的环境:Grails v2.1.1

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

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

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

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

这是我尝试通过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"
    }
}

}

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

}
4

1 回答 1

9

你可以eventCreateWarStart在你的scripts/_Events.groovy. 这个事件接收两个参数,WAR的名字和stagingDir

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

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

ant.exec(executable:'sencha') {
  arg(value:'app')
  arg(value:'build')
  // ...
}
于 2012-09-25T14:52:26.290 回答