grails create-command MyExample
create-command
目的
create-command
命令會建立一個新的 Grails Gradle 任務和 shell 命令,可以用終端機視窗中的 grails
命令執行。
範例
命令
會建立一個名為 grails-app/commands/PACKAGE_PATH/MyExampleCommand.groovy
的類別,例如
import grails.dev.commands.*
class MyExampleCommand implements ApplicationCommand {
boolean handle(ExecutionContext ctx) {
def dataSource = applicationContext.getBean(DataSource)
return true
}
}
可以使用 runCommand
命令執行命令。
grails run-command my-example
或作為 Gradle 任務
gradle runCommand -Pargs="myExample"
如果要執行的命令定義在您已宣告相依關係的外掛程式中,則可以使用簡短形式執行命令,如下所示
grails my-example
或作為 Gradle 任務
gradle myExample
外掛程式必須同時存在於 build.gradle
中的建置類別路徑和執行時期類別路徑中,簡短形式才能運作
buildscript {
...
dependencies {
classpath "org.grails.plugins:myplugin:0.1-SNAPSHOT"
}
...
dependencies {
runtime "org.grails.plugins:myplugin:0.1-SNAPSHOT"
}
}
說明
為了區分程式碼產生和建置層,在 Grails 3.x 中,使用 create-script
建立的指令碼無法存取正在執行的應用程式執行個體。
相反地,Grails 3.x 有一個新概念,稱為 ApplicationCommand,透過 Gradle 呼叫它來執行任務,例如與執行時期的類別互動。