2015-10-05 79 views
1

我已閱讀文檔here,它給出了結構,但對如何創建實際邏輯沒有提供任何幫助。如何爲grails創建一個簡單的構建腳本2.5

我想要構建一個非常簡單的腳本,它可以清理乾淨,創建戰爭,創建源文件的壓縮文件(沒有目標目錄,沒有任何svn目錄),並創建一個tar(理想壓縮)遷移目錄,這可以用於liquibase。每個3件文物應該有他們的名字的應用程序版本(如現有的「Grails的戰爭」確實

項目結構如下所示:

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的創建腳本包-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, [email protected]] 

我找不到任何示例腳本和超出鏈接非常基本概述的文檔。

我甚至無法得到ant.echo()工作 - intellij說只有一個ant.echo函數需要一個LinkedHashmap,一個String和一個Closure,但是ant文檔說回聲只需要一個「消息」字符串。 linkedhashmap,string和closure應該是什麼?我已經嘗試了至少30種不同的變化,沒有任何工作

更新1:我有什麼到目前爲止

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") 
} 

什麼我還沒有想通了工作:

  1. 如何獲得app.version的保持,因此可以把放在文件名。例如:println「爲$ app.version創建戰爭」不起作用
  2. 如何構建戰爭。不可能將它放入depdends列表中,而ant.war(「myfile.war」)不起作用。其他策略可能會在戰爭建築事件中運行這個腳本,這並不理想,因爲戰爭是在沒有這個需要的情況下創建的,或者通過調用shell命令來調用「grails戰爭」。

更新2 - 不能產生一個「正式版」戰爭

與阿什拉夫Purno幫助下,我們有一個腳本(下)它創建了一個戰爭,拉鍊的來源和TAR.GZ的liquibase文件並生產我們的包裝。然而,它有一個主要缺陷,創建的戰爭總是「開發」版本,因爲當你將它部署到tomcat時,它會疲於使用開發數據源和開發環境。在構建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") 
} 

回答

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對於腳本中的一些可用的道具/配置。

+0

哇,你有沒有發現這在谷歌?我一直在Google上搜索例子或文檔,並且幾乎空白。 –

+0

我確實找到了一個簡單的方法來拉app.version:「xxx _ $ {metadata.'app.version'}」 –

+0

你是正確的谷歌搜索除了在腳本中可用的應用程序屬性的列表沒有提供給我很多。我主要得到grails核心腳本資源的幫助。 *「xxx _ $ {metadata.'app.version'}」*其中'metadata'來自哪裏?它是否被自動注入? –

相關問題