grails g:if_Grails的优点:扩展IntegrateWith命令

grails g:if

我们可以在Grails中扩展integrate-with命令,以生成用于自定义IDE或构建系统的文件。 我们必须将_Events.groovy文件添加到我们的Grails项目中,然后为eventIntegrateWithStart事件编写一个实现。 在事件内部,我们必须使用代码定义一个新的闭包以生成文件。 闭包的名称必须具有以下模式: binding.integrate CustomIdentifierCustomIdentifier的值可用作integrate-with命令的参数。

假设我们要扩展“ integrate-with以生成一个简单的Sublime Text项目文件。 首先,我们创建一个模板Sublime Text项目文件,在其中为Grails应用程序定义文件夹。 我们创建文件夹src/ide-support/sublimetext并添加文件grailsProject.sublimetext-project ,其内容如下:

{
    "folders": [
        {
            "name": "Domain classes",
            "path": "grails-app/domain"
        },
        {
            "name": "Controllers",
            "path": "grails-app/controllers"
        },
        {
            "name": "Taglibs",
            "path": "grails-app/taglib"
        },
        {
            "name": "Views",
            "path": "grails-app/views"
        },
        {
            "name": "Services",
            "path": "grails-app/services"
        },
        {
            "name": "Configuration",
            "path": "grails-app/conf"
        },
        {
            "name": "grails-app/i18n",
            "path": "grails-app/i18n"
        },
        {
            "name": "grails-app/utils",
            "path": "grails-app/utils"
        },
        {
            "name": "grails-app/migrations",
            "path": "grails-app/migrations"
        },
        {
            "name": "web-app",
            "path": "web-app"
        },
        {
            "name": "Scripts",
            "path": "scripts"
        },
        {
            "name": "Sources:groovy",
            "path": "src/groovy"
        },
        {
            "name": "Sources:java",
            "path": "src/java"
        },
        {
            "name": "Tests:integration",
            "path": "test/integration"
        },
        {
            "name": "Tests:unit",
            "path": "test/unit"
        },
        {
            "name": "All files",
            "follow_symlinks": true,
            "path": "."
        }
    ]
}

接下来,我们创建文件scripts/_Events.groovy

includeTargets << grailsScript("_GrailsInit")

eventIntegrateWithStart = {

    // Usage: integrate-with --sublimeText
    binding.integrateSublimeText = {

        // Copy template file.
        ant.copy(todir: basedir) {
            fileset(dir: "src/ide-support/sublimetext/")
        }

        // Move template file to real project file with name of Grails application.
        ant.move(file: "$basedir/grailsProject.sublime-project", 
                 tofile: "$basedir/${grailsAppName}.sublime-project", 
                 overwrite: true)

        grailsConsole.updateStatus "Created SublimeText project file"
    }
}

我们已经完成,现在可以integrate-with带有新参数sublimeTextintegrate-with命令运行:

$ grails integrate-with --sublimeText
| Created SublimeText project file.
$

如果在Sublime Text中打开项目,我们将看到Grails应用程序的文件夹结构:

与st整合

用Grails 2.3.7编写的代码。

翻译自: https://www.javacodegeeks.com/2014/04/grails-goodness-extending-integratewith-command.html

grails g:if

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值