16

I have been trying to configure Gradle to manage a Grails project for a couple of hours with no success. The suggestions I've found here on Stack Overflow and elsewhere on the Internet didn't work for me.

Could you please give me up-to-date directions on how to configure a Gradle+Grails project? Ideally it should relate to the current versions of Grails (2.1.0) and Gradle (1.1).

Dušan Rychnovský
  • 11,699
  • 8
  • 41
  • 65
  • Have you tried this: https://github.com/grails/grails-gradle-plugin I've experimented with it and grails 1.3.5 a while ago and it was quite buggy but it seems to be much more smooth now handling 2.x versions and up. – Raphael Aug 19 '12 at 21:44

1 Answers1

17

BuildScript

import org.grails.gradle.plugin.GrailsTask

buildscript {
        repositories {
            mavenCentral()
            mavenRepo name: "grails", url: 'http://repo.grails.org/grails/repo' 
        }
        dependencies {
            classpath "org.grails:grails-gradle-plugin:1.1.1-SNAPSHOT"
        }
}
repositories {
    mavenCentral()
    mavenRepo name: "grails", url: 'http://repo.grails.org/grails/repo' 
}
version = "1.0"
grailsVersion = "2.1.0"
apply plugin: "grails"
dependencies {
    ['dependencies', 'resources', 'core', 'hibernate', 'plugin-datasource', 'plugin-domain-class', 'plugin-tomcat', 'plugin-services'].each { plugin ->
        compile "org.grails:grails-$plugin:2.1.0"
    }
    bootstrap "org.codehaus.groovy:groovy-all:1.8.6"
}

GRAILS_TASK_PREFIX = "grails-"
if (name.startsWith(GRAILS_TASK_PREFIX)) {
    project.task(name, type: GrailsTask) {
        command "${name - GRAILS_TASK_PREFIX}"
    }
}

Initialize

Then you can do gradle init to initialize the project structure

Commands

Use gradle grails-[grails script] to execute your grails commands. For example: gradle grails-run-app is equivalent to grails run-app

Hope this helps!

Update

This seems to work for Grails 2.3.2:

buildscript {
        repositories {
            mavenCentral()
            maven { url 'http://repo.grails.org/grails/repo' }
        }
        dependencies {
            classpath "org.grails:grails-gradle-plugin:2.0.0-SNAPSHOT"
        }
}
repositories {
    mavenCentral()
    maven { url 'http://repo.grails.org/grails/repo'  }
}
version = "1.0"
apply plugin: "grails"
apply plugin: 'idea'
grails {
  grailsVersion = "2.3.2"
}
dependencies {
    ['dependencies', 'core', 'spring', 'web', 'plugin-datasource', 'plugin-domain-class', 'plugin-controllers', 'plugin-services'].each { plugin ->
        compile "org.grails:grails-$plugin:2.3.2"
    }
    compile 'org.grails.plugins:tomcat:7.0.42'
    compile 'org.grails.plugins:hibernate:3.6.10.3'
    compile 'com.h2database:h2:1.3.173'
    bootstrap "org.codehaus.groovy:groovy-all:2.1.9"
}

A note, throws Exception when generating Grails wrapper, but gradle init seems to initialize the project structure properly otherwise. And, gradle grails-run-app seems to work fine too.

Daniel Woods
  • 1,029
  • 7
  • 10
  • this fails for me. When I change bootstrap "org.codehaus.groovy:groovy-all:1.8.7" to 1.8.6 it works... – MartinL Nov 11 '12 at 21:33
  • This doesn't seem to work for grails commands with arguments such as `gradle grails-create-domain-class x.y.Z` – C. Ross Nov 29 '12 at 13:09
  • @C.Ross, gradle grails-create-domain-class -PgrailsArgs=mydomain.Person – rimero Nov 22 '13 at 05:41
  • Would this be true for Grails plugins as well? I've been trying to use it for the liquibase migrations plugin, e.g. but `./gradlew grails-dbm-list-locks` responds with `Task 'grails-dbm-list-locks' not found in root project 'aps-metrics'.` – Brent Fisher May 07 '21 at 18:52