如何为Grails域使用自定义模板?

如何为Grails域使用自定义模板?,grails,Grails,在Grails中,当使用“createdomainclass”命令创建域类时,它使用标准模板生成域。如何用自己的模板覆盖此模板 我尝试将自定义模板“DomainClass.groovy”放在/src/main/templates/scaffolding和/src/main/templates/artifacts中,但两者都没有使用 我使用的是Grails 3.3.1。最后我用一个新脚本扩展了我们的自定义插件,以呈现域类模板和域类规范模板 description( "Creates a Grail

在Grails中,当使用“createdomainclass”命令创建域类时,它使用标准模板生成域。如何用自己的模板覆盖此模板

我尝试将自定义模板“DomainClass.groovy”放在/src/main/templates/scaffolding和/src/main/templates/artifacts中,但两者都没有使用


我使用的是Grails 3.3.1。

最后我用一个新脚本扩展了我们的自定义插件,以呈现域类模板和域类规范模板

description( "Creates a Grails domain" ) {
  usage "grails create-new-domain <<DOMAIN NAME>>"
  argument name:'Domain Class Name', description:"The name of the domain to create"
  flag name:'force', description:"Whether to overwrite existing files"
}

def domainClassName = args[0]
def model = model(domainClassName)
def overwrite = flag('force') ? true : false

render  template: template('artifacts/CustomDomainClass.groovy'), //--could not be "DomainClass.groovy" as Grails template would override
        destination: file("grails-app/domain/${model.packagePath}/${model.className}.groovy"),
        model: model,
        overwrite: overwrite


render  template: template('artifacts/CustomDomainClassSpec.groovy'),
        destination: file("src/test/groovy/${model.packagePath}/${model.className}Spec.groovy"),
        model: model,
        overwrite: overwrite
description(“创建Grails域”){
用法“grails创建新域”
参数名称:'Domain Class name',说明:“要创建的域的名称”
标志名称:“强制”,说明:“是否覆盖现有文件”
}
def domainClassName=args[0]
def model=model(domainClassName)
def OVERVERSE=标志('force')?对:错
呈现模板:模板('artifacts/CustomDomainClass.groovy'),/--不能是“DomainClass.groovy”,因为Grails模板将覆盖它
目标:文件(“grails-app/domain/${model.packagePath}/${model.className}.groovy”),
模型:模型,
覆盖:覆盖
呈现模板:模板('artifacts/CustomDomainClassSpec.groovy'),
目标:文件(“src/test/groovy/${model.packagePath}/${model.className}Spec.groovy”),
模型:模型,
覆盖:覆盖

最后,我用一个新脚本扩展了我们的自定义插件,以呈现一个域类模板和一个域类规范模板

description( "Creates a Grails domain" ) {
  usage "grails create-new-domain <<DOMAIN NAME>>"
  argument name:'Domain Class Name', description:"The name of the domain to create"
  flag name:'force', description:"Whether to overwrite existing files"
}

def domainClassName = args[0]
def model = model(domainClassName)
def overwrite = flag('force') ? true : false

render  template: template('artifacts/CustomDomainClass.groovy'), //--could not be "DomainClass.groovy" as Grails template would override
        destination: file("grails-app/domain/${model.packagePath}/${model.className}.groovy"),
        model: model,
        overwrite: overwrite


render  template: template('artifacts/CustomDomainClassSpec.groovy'),
        destination: file("src/test/groovy/${model.packagePath}/${model.className}Spec.groovy"),
        model: model,
        overwrite: overwrite
description(“创建Grails域”){
用法“grails创建新域”
参数名称:'Domain Class name',说明:“要创建的域的名称”
标志名称:“强制”,说明:“是否覆盖现有文件”
}
def domainClassName=args[0]
def model=model(domainClassName)
def OVERVERSE=标志('force')?对:错
呈现模板:模板('artifacts/CustomDomainClass.groovy'),/--不能是“DomainClass.groovy”,因为Grails模板将覆盖它
目标:文件(“grails-app/domain/${model.packagePath}/${model.className}.groovy”),
模型:模型,
覆盖:覆盖
呈现模板:模板('artifacts/CustomDomainClassSpec.groovy'),
目标:文件(“src/test/groovy/${model.packagePath}/${model.className}Spec.groovy”),
模型:模型,
覆盖:覆盖

grails core中有一个插件可以完成这一部分。你可以我假设克隆/分叉它-进行更改并在本地实现插件作为一些新名称-可能需要更多跟踪他们如何在核心中启用它-你可能需要调用不同的方式-如果有人知道如何使它工作,我仍然需要这个。感谢grails core中有一个插件可以完成这一部分。你可以我假设克隆/分叉它-进行更改并在本地实现插件作为一些新名称-可能需要更多跟踪他们如何在核心中启用它-你可能需要调用不同的方式-如果有人知道如何使它工作,我仍然需要这个。谢谢