alexcuesta

My tech blog

Gradle on Grails

with 10 comments

I am taking my first steps on grails on a real project after playing around with it for a while.

The first problem I came across was: how to deal with dependencies? In my project, we have some dependencies on libraries stored in our internal maven repository.

You have three options here:

  1. Use Ivy which comes out-of-the-box in Grails. Ivy is just a dependency manager for ant. I have never used it and looks old-fashioned,  so I went to the next step.
  2. Mavenize your project.
  3. Use gradle, which is the latest cool builder. A mix of Ant and Maven with the power of the Groovy language.

As I said, I ignored Ivy.

My first successful attempt was Maven. It is quite easy following the steps in the documentation.

Anyway, I really wanted to use Gradle because it looks really easy to read, so I gave it a go. My starting point was this article by Peter Ledbrook but it was confusing because the build script contains errors. So after reading the comments, I went to the grails-gradle-plugin project instead.

After a couple of hours, I ended up with this solution:

  1. Create an empty folder for your project
  2. Go to root of your new project folder and store the build.gradle script  below.
  3. Run ‘gradle init’ to create the grails layout
  4. Use the normal grails commands using the “grails-” prefix.
    $ gradle grails-run-app

This is the build.gradle script

buildscript {
    repositories {
        mavenCentral()
        mavenRepo urls: "http://repository.jboss.org/maven2/"
    }

    dependencies {
        classpath "org.grails:grails-gradle-plugin:1.0",
                      "org.grails:grails-bootstrap:1.3.6"
    }
}

apply plugin: "grails"

version = '1.0-SNAPSHOT'

// this is your local maven repository
def localMavenRepository = new File( System.getProperty("user.home" ), ".m2/repository" ).toURL().toString()

repositories {
    mavenCentral()
    mavenRepo urls: "http://repository.jboss.org/maven2/"
	mavenRepo urls: "http://download.java.net/maven/2/"
	mavenRepo name:'localMavenRepo', urls: localMavenRepository
}

configurations {
    compile.exclude module: "xml-apis"
}

dependencies {
    compile "org.grails:grails-crud:1.3.6",
               "org.grails:grails-gorm:1.3.6"

    runtime "org.slf4j:slf4j-log4j12:1.5.5",
	      "hsqldb:hsqldb:1.8.0.5",
	      "jstl:jstl:1.1.2"

}

You may need to add or remove dependencies. This depends on your project.

Hope this saves you some time.

UPDATE 4 JAN 2011

The example above was done using gradle 0.9. The new version 0.9.1 supports mavenLocal() so you can remove the localMavenRepository definition and replace the last mavenRepo with mavenLocal()

Written by alexcuesta

December 21, 2010 at 2:27 pm

Posted in Groovy/Grails

10 Responses

Subscribe to comments with RSS.

  1. I’m trying to gradleize an existing Grails 1.3.6 project thanks to your build script but I’m having a series of issues. The first issue I had was a missing ehcache dependency so I added “net.sf.ehcache:ehcache-core:1.7.1” to the runtime dependencies. But now I’m getting another ClassNotFoundException: ClassNotFoundException: org.springframework.aop.aspectj.autoproxy.AspectJAwareAdvisorAutoProxyCreator$PartiallyComparableAdvisorHolder

    And I’m finding it weird that I have to modify my build script so much to get it to run. Any idea?

    Sebastien Arbogast

    December 30, 2010 at 1:54 pm

    • It is not that weird that you have to modify the build script so much.
      The basic script is here: https://github.com/grails/grails-gradle-plugin
      The only thing I did was to add the dependencies I needed for my own test (I tested a web service without DAOs/hibernate ).
      As I said below the script, “you may need to add/remove dependencies to your own project”.

      alexcuesta

      December 31, 2010 at 3:11 am

      • I would expect to have to manage dependencies manually for libraries I’m using, but here the only difference with a simple project is that I installed a few plugins and I was hoping Gradle would manage those dependencies automatically for me via plugin dependencies. Now if I have to go and re-depend on all the dependencies of my plugins, so much for DRY. Don’t you think? But maybe the gradle plugin is not mature enough and it will do all this in the future. Anyway, thanks for your response.

        Sebastien Arbogast

        December 31, 2010 at 8:11 am

  2. OH yes, you are right. That was my first feeling. BTW, I’ve just read that the latest release of Gradle 0.9.1 allows using maven local repository via mavenLocal() method.

    http://docs.codehaus.org/display/GRADLE/Gradle+0.9.1+Release+Notes

    Alejandro

    January 3, 2011 at 10:14 pm

  3. @Sebastien, did you ever really find a resolution for the
    exception on org.springframework.aop.aspectj.autoproxy.AspectJAwareAdvisorAutoProxyCreator$PartiallyComparableAdvisorHolder

    Would love to know.

    Thanks.

    Eli Ezeugoh

    February 15, 2011 at 6:19 pm

    • UPDATE:

      I found the resolution to the problem after a brief chat with the good fellas on the #grails IRC channel. It was missing one of the spring-aop dependencies (in my case aspectjweaver). I needed to add this as a runtime dependency in my build.gradle and that resolved the problem.

      Cheers.

      Eli Ezeugoh

      February 15, 2011 at 6:49 pm

  4. Interesting idea. I tried doing this with Gradle 1.0-milestone-1 and Grails 1.3.7. It will take:

    gradle grails-create-domain-class

    but not

    gradle grails-create-domain-class foo.Bar

    It says ‘foo.Bar’ not found in project. It looks like Gradle wants to take the second command as another gradle target.

    I wonder, why would that be? I revved the dependencies on Grails to 1.3.7 so that is correct.

    Ken Rimple

    March 26, 2011 at 10:34 pm

  5. I’m using Grails 1.3.7 and I’ve used your build file, the file on the plugin page, and tried tweaking the both to no avail. No matter what I do, “gradle grails-run-app” throws the following error: “module not found: org.xhtmlrenderer#core-renderer;R8”

    Tony

    August 10, 2011 at 2:17 am

    • It’s been ages since I last used Gradle or Grails but I guess you might be missing some dependencies.
      http://grails.1312388.n4.nabble.com/CAnnot-release-plugin-td3674116.html

      Sorry, I can’t help you with this.
      Anyway, I think Grails and Gradle have evolved a lot in the last year so it would be worth to have a look at the latests versions and see how it’s done today. This article might be obsoleted.

      alexcuesta

      August 12, 2011 at 10:33 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: