(Quick Reference)

5.5 Building with Gradle - Reference Documentation

Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith, Lari Hotari

Version: 3.0.11

5.5 Building with Gradle

Grails 3.0 uses the Gradle Build System for build related tasks such as compilation, runnings tests and producing binary distrubutions of your project. It is recommended to use Gradle 2.2 or above with Grails 3.0.

The build is defined by the build.gradle file which specifies the version of your project, the dependencies of the project and the repositories where to find those dependencies (amongst other things).

When you invoke the grails command the version of Gradle that ships with Grails 3.0 (currently 2.3) is invoked by the grails process via the Gradle Tooling API:

# Equivalent to 'gradle classes'
$ grails compile

You can invoke Gradle directly using the gradle command and use your own local version of Gradle, however you will need Gradle 2.2 or above to work with Grails 3.0:

$ gradle assemble

5.5.1 Defining Dependencies with Gradle

Dependencies for your project are defined in the dependencies block. In general you can follow the Gradle documentation on dependency management to understand how to configure additional dependencies.

The default dependencies for the "web" profile can be seen below:

dependencies {
  compile 'org.springframework.boot:spring-boot-starter-logging'
  compile('org.springframework.boot:spring-boot-starter-actuator')
  compile 'org.springframework.boot:spring-boot-autoconfigure'
  compile 'org.springframework.boot:spring-boot-starter-tomcat'
  compile 'org.grails:grails-dependencies'
  compile 'org.grails:grails-web-boot'

compile 'org.grails.plugins:hibernate' compile 'org.grails.plugins:cache' compile 'org.hibernate:hibernate-ehcache'

runtime 'org.grails.plugins:asset-pipeline' runtime 'org.grails.plugins:scaffolding'

testCompile 'org.grails:grails-plugin-testing' testCompile 'org.grails.plugins:geb'

// Note: It is recommended to update to a more robust driver (Chrome, Firefox etc.) testRuntime 'org.seleniumhq.selenium:selenium-htmlunit-driver:2.44.0'

console 'org.grails:grails-console' }

Note that version numbers are not present in the majority of the dependencies. This is thanks to the dependency management plugin which configures a Maven BOM that defines the default dependency versions for certain commonly used dependencies and plugins:

dependencyManagement {
    imports {
        mavenBom 'org.grails:grails-bom:' + grailsVersion
    }
    applyMavenExclusions false
}

5.5.2 Working with Gradle Tasks

As mentioned previously the grails command uses an embedded version of Gradle and certain Grails commands that existed in previous versions of Grails map onto their Gradle equivalents. The following table shows which Grails command invoke which Gradle task:

Grails CommandGradle Task
cleanclean
compileclasses
packageassemble
run-apprun
test-apptest
warassemble

You can invoke any of these Grails commands using their Gradle equivalents if you prefer:

$ gradle test

Note however that you will need to use a version of Gradle compatible with Grails 3.0 (Gradle 2.2 or above). If you wish to invoke a Gradle task using the version of Gradle used by Grails you can do so with the grails command:

$ grails gradle compileGroovy

However, it is recommended you do this via interactive mode, as it greatly speeds up execution and provides TAB completion for the available Gradle tasks:

$ grails 
| Enter a command name to run. Use TAB for completion:
 grails> gradle compileGroovy
 ...

To find out what Gradle tasks are available without using interactive mode TAB completion you can use the Gradle tasks task:

gradle tasks

5.5.3 Grails plugins for Gradle

When you create a new project with the create-app command, a default build.gradle is created. The default build.gradle configures the build with a set of Gradle plugins that allow Gradle to build the Grails project:

plugins {
    id "io.spring.dependency-management" version "0.3.1.RELEASE"
}

apply plugin: "spring-boot" apply plugin: "war" apply plugin: "asset-pipeline" apply plugin: "org.grails.grails-web" apply plugin: "org.grails.grails-gsp" apply plugin: "maven"

The default plugins are as follows:

  • dependency-management - The dependency management plugin allows Gradle to read Maven BOM files that define the default dependency versions used by Grails.
  • spring-boot - The Spring Boot Gradle plugin enhances the default packaging tasks provided by Gradle to allow for the creation of runnable JAR/WAR files.
  • war - The WAR plugin changes the packaging so that Gradle creates as WAR file from you application. You can comment out this plugin if you wish to create only a runnable JAR file for standalone deployment.
  • asset-pipeline - The asset pipeline plugin enables the compilation of static assets (JavaScript, CSS etc.)
  • maven - The maven plugin allows installing your application into a local maven repository

Many of these are built in plugins provided by Gradle or third party plugins. The Gradle plugins that Grails provides are as follows:

  • org.grails.grails-core - The primary Grails plugin for Gradle, included by all other plugins and designed to operate with all profiles.
  • org.grails.grails-plugin - A plugin for Gradle for building Grails plugins.
  • org.grails.grails-web - The Grails Web gradle plugin configures Gradle to understand the Grails conventions and directory structure.
  • org.grails.grails-gsp - The Grails GSP plugin adds precompilation of GSP files for production deployments.
  • org.grails.grails-doc - A plugin for Gradle for using Grails 2.0's documentation engine.