A search for meaning in software and life
RSS icon Email icon Home icon
  • Building a Grails project with Gradle

    Posted on February 5th, 2010 Peter 33 comments

    Earlier this week, I attended Hans Dockter’s first ever Gradle course. Everything seemed to click into place and it inspired me to have another crack at writing a Gradle plugin for building Grails projects. A lot of the hard work had already been done to get the Maven plugin working reasonably well, so all I had to do was make use of the helper classes that already existed for bootstrapping the Grails build system.

    So how do you use it? Simple. Here’s a basic Gradle build file (build.gradle) that you can put in the root of your Grails project:

    buildscript {
        repositories {
            mavenCentral()
            mavenRepo urls: 'http://snapshots.repository.codehaus.org'
            mavenRepo urls: 'http://download.java.net/maven/2/'
        }
    
        dependencies {
            classpath "org.grails:grails-gradle-plugin:1.0-SNAPSHOT"
        }
    }
    
    apply id: "grails"
    
    repositories {
        mavenCentral()
        flatDir dirs: "lib"
    }
    
    configurations {
        compile.exclude module: "commons-logging"
    }
    
    dependencies {
        compile "org.grails:grails-crud:1.2.0",
                "org.grails:grails-gorm:1.2.0",
                "org.apache.httpcomponents:httpclient:4.0",
                "org.apache.httpcomponents:httpmime:4.0",
                ":apache-solr-solrj:1.4:dev",
                ":pgptools:0.1"
    
        // Required by the Joda Time plugin.
        compile "org.springframework:spring-web:3.0.0.RELEASE"
        compile "org.springframework:spring-webmvc:3.0.0.RELEASE"
    
        // Required by the JMS plugin.
        compile "org.springframework:spring-jms:3.0.0.RELEASE"
    
        runtime "org.slf4j:slf4j-log4j12:1.5.5",
                "hsqldb:hsqldb:1.8.0.5",
                "postgresql:postgresql:8.3-603.jdbc3",
                "org.bouncycastle:bcpg-jdk15:1.44",
                "net.sf.ehcache:ehcache-core:1.7.1"
    }
    

    Unbelievably, that’s it! You can now execute any of the Grails commands via Gradle like so:

    gradle compile
    gradle create-domain-class -Pargs=org.example.Book
    gradle run-app
    gradle run-app -Pargs=--https -Penv=prod
    gradle create-gwt-action -Pargs="--shared-pkg=client org.example cmd ListBooks"
    

    The -Pargs option allows you to pass any arguments you want to the corresponding Grails command, while -Penv allows you to override the default environment. In case you’re wondering, the -P option allows you configure project properties. args and env are simply two that are understood by the Grails plugin.

    So what’s happening in this build file? First of all, we use the buildscript {} section to configure the plugin as a dependency of the script. Once the plugin is on the classpath, we can apply it to our build via apply id: 'grails'. Alternatively, you could download the plugin JAR file and put it into $GRADLE_HOME/lib.

    The rest of the build file is concerned with setting up the dependencies for our Grails project. Quite a few are declared in this example, but the only requirement is that you declare dependencies on at least one Grails module, a logging implementation, and the JDBC driver(s) for whatever database(s) you’re using. If you’re using the Hibernate second-level cache, you should also include EhCache or OSCache.

    The true beauty of this plugin is that *all* the Grails commands are available to you, including those provided by plugins. This is far better than the Maven plugin, which has a set of hard-coded goals for the most common Grails commands and a generic exec goal for executing arbitrary Grails commands. I haven’t tested all of the commands, so feel free to report any problems.

    The code for the plugin can be found on GitHub. I think you’ll be surprised at how little code there is. In fact, there are only two files: build.gradle in the root of the project, and GrailsPlugin.groovy under src/groovy. I definitely recommend taking a look at the plugin file to see how Gradle’s dynamic tasks made it so easy to support all the Grails commands.

    Give it a go and see what you think!

    Update First off, the plugin currently only works with Grails 1.2.0+ and Gradle 0.9 snapshots. Second, I’m aware that the build script doesn’t work at the moment. Unfortunately, there appears to be a Gradle bug preventing the fixed build script from working. Stay tuned.

    Update 2 My bad. The version of mavenRepo that takes more than one repository URL expects the first one to contain the POM, while the other ones are for getting hold of the JARs. I have corrected the example build so that it has separate mavenRepo lines for the Codehaus Snapshots repository (for the plugin) and the java.net repository (for GParallelizer),

    Update 3 I have uploaded a new version of the plugin that allows you to start from scratch. Create a new directory for your Grails project and add the following build.gradle file to it:

    buildscript {
        repositories {
            mavenCentral()
            mavenRepo urls: 'http://snapshots.repository.codehaus.org'
            mavenRepo urls: 'http://download.java.net/maven/2/'
        }
    
        dependencies {
            classpath "org.grails:grails-gradle-plugin:1.0-SNAPSHOT"
        }
    }
    
    apply id: "grails"
    
    version = "1.0-SNAPSHOT"
    
    repositories {
        mavenCentral()
        flatDir dirs: "lib"
    }
    
    configurations {
        compile.exclude module: "commons-logging"
    }
    
    dependencies {
        compile "org.grails:grails-crud:1.2.0",
                "org.grails:grails-gorm:1.2.0"
    
        runtime "org.slf4j:slf4j-log4j12:1.5.5",
                "hsqldb:hsqldb:1.8.0.5",
                "net.sf.ehcache:ehcache-core:1.7.1"
    }
    

    From the new directory, run gradle init and a fresh Grails project will be created for you.