2014-04-10 52 views
1

我想創建一個Grails內域類的實例2.3.6腳本里面:創建一個域類的一個實例,一個Grails腳本

def player = new Player(name:"Bob") 
player.save() 

但我不斷收到異常

java.lang.NoClassDefFoundError: gaming/Player 

我已經嘗試了所有我已經成功地在互聯網上找到不同的引導技巧,但他們並不真正改變結果:

我試着輸入:

import gaming.Player 

我試着加載引導腳本:

includeTargets << grailsScript("_GrailsBootstrap") 

我已經試過這取決於每一個任務我設法找到:

depends(configureProxy, packageApp, classpath, loadApp, configureApp, compile, bootstrap) 

我甚至嘗試加載類在運行時:

ApplicationHolder.application.getClassForName("gaming.Player") 

有趣的是,這最後一行這並不表示Grails可以找到我的課程,但是當我真的去使用它時,我們會選擇忽略它。

編輯。按照要求,這裏是腳本

import gaming.Player 

import org.codehaus.groovy.grails.commons.ApplicationHolder 

includeTargets << grailsScript("_GrailsInit") 
includeTargets << grailsScript("_GrailsBootstrap") 
includeTargets << grailsScript("_GrailsClasspath") 

def handleHeaderLine(line) { 
    def retval = [] 
    line.each { 
     if(!it.equals("Game Name") && !it.equals("Total # of Copies")) { 
      println("Creating Player: " + it) 
      def player = new Player(name:it) 
      player.save 
      retval << it 
     } else { 
      retval << null 
     } 
    } 
    return retval; 
} 

def handleGameLine(header, line) { 
    println("Creating Game: " + line[0]) 
    for(int i = 1; i < line.length - 1; i++) { 
     if(!header[i].equals("Total # of Copies")) { 
      def count = line[i] == "" ? 0 : Integer.parseInt(line[i]); 
      for(int j = 0; j < count; j++) { 
       println "Creating copy of " + line[0] + " owned by " + header[i] 
      } 
     } 
    } 
} 

target(loadAssets: "The description of the script goes here!") { 
    depends(configureProxy, packageApp, classpath, loadApp, configureApp, compile, bootstrap) 

    ApplicationHolder.application.getClassForName("gaming.Player") 

    def tsv = new File("...") 

    def header = null; 
    tsv.eachLine { 
     def line = it.split("\t") 
     if(header == null) { 
      header = handleHeaderLine(line) 
      println header 
     } else { 
      handleGameLine(header, line) 
     } 
    } 
} 

setDefaultTarget(loadAssets) 
+0

嘗試增加includeTargets << grailsS​​cript( 「_ GrailsInit」) –

+0

它已經存在(如Grails的一部分創建腳本)。 – spierepf

+0

你能否提供你的'Gant'腳本 – emilan

回答

3

你不必做所有的鍋爐板着力培育環境中運行腳本,而當前版本。 run-script是爲你做的。當使用grails run-script以下目標在默認情況下運行時:checkVersion, configureProxy, bootstrap。最後運行腳本run-script

運行腳本通過將ApplicationContextgrailsApplication作爲綁定到shell來運行您的自定義腳本GroovyShell。所以,你會到底是什麼了你的腳本如下所示,就好像它是寫在Groovy的控制檯/殼:

//scripts/player/PlayerScript.groovy 
def handleHeaderLine(line) { 
    def retval = [] 
    line.each { 
     if(!it.equals("Game Name") && !it.equals("Total # of Copies")) { 
      println("Creating Player: " + it) 
      def player = new Player(name: it) 
      player.save(flush: true) 
      retval << it 
     } else { 
      retval << null 
     } 
    } 
    return retval 
} 

def handleGameLine(header, line) { 
    println("Creating Game: " + line[0]) 
    for(int i = 1; i < line.length - 1; i++) { 
     if(!header[i].equals("Total # of Copies")) { 
      def count = line[i] == "" ? 0 : Integer.parseInt(line[i]); 
      for(int j = 0; j < count; j++) { 
       println "Creating copy of " + line[0] + " owned by " + header[i] 
      } 
     } 
    } 
} 

def tsv = new File("...") 
def header = null 
tsv.eachLine { 
    def line = it.split("\t") 
    if(header == null) { 
     header = handleHeaderLine(line) 
     println header 
    } else { 
     handleGameLine(header, line) 
    } 
} 

然後下面用run-script

grails run-script scripts/player/PlayerScript.groovy 

這將默認運行腳本在開發環境中。如果你想爲其他envirnments那麼作爲

grails test run-script scripts/player/PlayerScript.groovy 


使用由於Grails的最新版本major bug,你將無法運行腳本上述方式,因爲run-script總是取決於bootstrap目標,並將始終嘗試運行腳本作爲build中的插件範圍,這將導致加載插件管理器時出錯tomcat:TomcatGrailsPlugin。解決方法也是在缺陷中提到的,但這裏是一個更加簡單的實現。在BuildConfig.groovy變化:

plugins { 
    if (!System.getProperty("noTomcat")) { 
     build ":tomcat:7.0.52.1" 
    } 
    .... 
} 

然後發出運行腳本命令爲:

grails -DnoTomcat=true run-script scripts/player/PlayerScript.groovy 

在一個側面說明,你的腳本沒有運行的原因是類Player將不加載這段時間運行腳本,供使用。它必須使用classLoader手動加載,然後創建一個實例。喜歡的東西:

includeTargets << grailsScript("_GrailsInit") 
includeTargets << grailsScript("_GrailsBootstrap") 

target(playerScript: "The description of the script goes here!") { 
    depends configureProxy, packageApp, classpath, loadApp, configureApp 

    def playerClass = classLoader.loadClass("gaming.Player") 

    //Skeptical about how a domain class would behave 
    //But a normal POGO should be good being used this way 
    def player = playerClass.newInstance([[name: "Bob"]] as Object[]) 
    player.save(flush: true) 

    println player 
} 

setDefaultTarget(playerScript) 
相關問題