Hi all, my name is Hubert A. Klein Ikkink. Not a very common name, right? To make things easier I just picked the first letters of my firstname and surname and came up with haki. So there you have it, now I am also known as Mr. Haki or mrhaki for short. You can read more blog postings at www.mrhaki.com. I am a passionate Groovy and Java developer based in Tilburg, The Netherlands. My goal is to write clean, elegant, user-centered and high quality software. You can find me on Google+ and Twitter. Hubert is a DZone MVB and is not an employee of DZone and has posted 160 posts at DZone. You can read more from them at their website. View Full User Profile

Grails Goodness: Extending IntegrateWith Command

04.18.2014
| 3327 views |
  • submit to reddit

We can extend the integrate-with command in Grails to generate files for a custom IDE or build system. We must add a _Events.groovy file to our Grails projects and then write an implementation for the eventIntegrateWithStart event. Inside the event we must define a new closure with our code to generate files. The name of the closure must have the following pattern: binding.integrateCustomIdentifier. The value forCustomIdentifier can be used as an argument for the integrate-with command.

Suppose we want to extend integrate-with to generate a simple Sublime Text project file. First we create a template Sublime Text project file where we define folders for a Grails application. We create the folder src/ide-support/sublimetext and add the file grailsProject.sublimetext-project with the following contents:

{
    "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": "."
        }
    ]
}

Next we create the file 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"
    }
}

We are done and can now run the integrate-with command with the new argument sublimeText:

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

If we open the project in Sublime Text we see our folder structure for a Grails application:

Code written with Grails 2.3.7.



Published at DZone with permission of Hubert Klein Ikkink, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)