我已阅读文档here,它提供了结构,但没有提供有关如何创建实际逻辑的任何帮助。
我想构建一个非常简单的脚本,它执行干净,创建战争,创建源的zip(没有目标目录,没有任何svn dirs),并创建一个tar(理想gzip)的迁移dir所以这可以和liquibase一起使用。这三个工件中的每一个都应该在其名称中包含应用程序版本(就像现有的“grails war”一样。
项目结构如下:
svn
main-app
grails-app
migrations
target
:
exploded-plugin-1
grails-app
:
exploded-plugin-2
grails-app
:
这是我有多远:
includeTargets << grailsScript("_GrailsInit")
target(packageUs: "Creates war, zips source, tars liquibase scripts") {
depends(clean, war-us, zip-source, tar-liquibase)
}
setDefaultTarget(packageUs)
target ("war-us" : "creates war") {
ant.war() // this was a guess at calling the existing war - it doesnt work
}
target ("zip-source" : "zips sources") {
// cd to top dir of project, i.e. above the app.
}
target ("tar-liquibase":"produces tar of migrations dir") {
// tar up the migrations dir
// name it with the app-version
// put it in the target dir along side the war etc.
}
target ("clean") {
// call the default clean some how, or cd to target dir, and delete everything
}
上述脚本最初是使用“grails create-script package-us”创建的
可悲的是,即使这不起作用,也会产生以下错误:
| Error Error executing script PackageUs: No signature of method: org.codehaus.gant.GantBinding$_initializeGantBinding_closure5.doCall() is applicable
for argument types: (java.lang.String, PackageUs$_run_closure5) values: [clean, PackageUs$_run_closure5@5ed0b4e3]
除了链接中的基本概述之外,我找不到任何示例脚本或文档。
我甚至无法使ant.echo()工作 - intellij说只有一个ant.echo函数接受LinkedHashmap,String和Closure,但是ant文档说echo只接受一个“message”字符串。 Linkedhashmap,字符串和闭包应该是什么?我已经尝试了至少30种不同的变体,没有效果。
includeTargets << grailsScript("_GrailsInit")
target(packageUs: "Creates war, zips source, tars liquibase scripts") {
depends(clean, "war-us", "zip-source")
}
setDefaultTarget(packageUs)
target ("war-us" : "creates war") {
ant.echo(message:"hello") // this compiles and runs, but does nothing.
println "hello there" // this does work.
// ant.war(?)
}
// puts in it wrong dir, and doen't have app version in name, but at least it zips the right content!
target ("zip-source" : "zips sources") {
ant.delete(file: 'sources.zip') // dont know how to add this to the clean cycle
ant.zip(destfile: 'sources.zip', basedir: '..', excludes: "**/*.zip **/target/** **.idea **/.classpath **/.iml")
}
我没想到的是:
在Ashraf Purno的帮助下,我们有一个脚本(下面)创建一个战争,拉链源和tar.gz liquibase文件并生成我们的包。但是,它有一个主要缺陷,创建的战争始终是“dev”版本,因为当你将它部署到tomcat时,它会厌倦使用dev数据源和dev环境。似乎没有办法在构建scirpt中改变它,并且在调用脚本的命令行上设置环境(例如“grails prod myscript”)也没有影响 - 它也产生了一个dev版本的战争(没有用)
includeTargets << grailsScript("_GrailsInit")
includeTargets << grailsScript("_GrailsClean")
includeTargets << grailsScript("War")
target(warpack: "Creates war, zips source, tars liquibase scripts") {
depends(cleanAll, cleanRealyAll, war, "zip-source", "tar-liquibase", "package-all")
}
setDefaultTarget(warpack)
target ("cleanRealyAll" : "Cleans stuff that clean-all wont touch") {
println "wiping the target dir"
ant.delete(dir: "target")
ant.mkdir(dir: "target")
}
target ("zip-source" : "zips sources") {
println "zipping sources for ${metadata.'app.version'}"
String zipFile = "target/sources-${metadata.'app.version'}.zip"
ant.delete(file: zipFile)
ant.zip(destfile: zipFile, basedir: '..', excludes: "**/*.zip **/target/** **.idea **/.classpath **/.iml")
}
target ("tar-liquibase":"produces tar of migrations dir") {
println "tarring liquibase for ${metadata.'app.version'}"
String tarFile = "target/migrations-${metadata.'app.version'}.tar"
String gzipfile = "target/migrations-${metadata.'app.version'}.tar.gz"
ant.tar(destfile:tarFile, basedir: "grails-app/migrations")
ant.gzip(src: tarFile, destfile : gzipfile )
ant.delete(file: tarFile)
}
target ("package-all":"puts it all together in one file, relies on externally running 'grails war' first") {
println "creating package for ${metadata.'app.version'}"
String packageFile = "target/ourpackage-${metadata.'app.version'}.tar"
ant.delete(file: packageFile)
ant.tar (destfile: packageFile, basedir:"target", includes: "*.war *.zip *.gz" )
}
答案 0 :(得分:2)
经过一些谷歌搜索和窥视grails核心脚本后,我设法创建了一个脚本,它可以完成你在问题中提到的事情。这是
import grails.util.Metadata
includeTargets << grailsScript("_GrailsInit")
includeTargets << grailsScript("_GrailsClean")
includeTargets << grailsScript("_GrailsWar")
target(pack: "Creates war, zips source, tars liquibase scripts") {
depends(cleanAll, war)
String appVersion = Metadata.current[Metadata.APPLICATION_VERSION],
zipFileName = "${basedir}/target/sources-${appVersion}.zip",
tarFileName = "${basedir}/target/migrations-${appVersion}.tar.gz"
println "Creating Sources Zip"
ant.delete(file: zipFileName)
ant.zip(destfile: zipFileName, basedir: basedir, excludes: "**/target/** **/.idea/** **/.classpath/** **/.iml/**")
println "Creating Migrations Tar Ball"
ant.delete(file: tarFileName)
ant.tar(destfile: tarFileName, basedir: "${basedir}/grails-app/migrations")
}
setDefaultTarget(pack)
为了简单起见,我已将所有任务放在一个目标中。您可以将它们分成多个目标,并根据需要将它们添加到depends
。我使用Grails 2.5.1
来测试这个脚本。
您可以在这里查看http://mrhaki.blogspot.com/2014/05/grails-goodness-run-groovy-scripts-in.html脚本中的一些可用道具/配置。