【问题标题】:How can I use a custom template for a Grails Domain?如何为 Grails 域使用自定义模板?
【发布时间】:2019-10-04 05:01:59
【问题描述】:

在 Grails 中,当使用“create-domain-class”命令创建域类时,它会使用标准模板生成域。如何用我自己的模板覆盖这个模板?

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

我正在使用 Grails 3.3.1。

【问题讨论】:

  • github.com/grails/grails-core/tree/3.3.x/… grails-core 中有一个插件可以完成这部分工作。我可以假设克隆/分叉它-进行更改并在本地将插件实现为一些新名称-可能需要更多跟踪他们如何在核心中启用它-您可能需要以不同的方式调用

标签: grails


【解决方案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

【讨论】:

    猜你喜欢
    • 2021-11-15
    • 2020-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-14
    相关资源
    最近更新 更多