Since Grails 3 Gradle is used as the build tool. The Grails shell and commands use Gradle to execute tasks. When we create a new Grails 3 application a Gradle wrapper is added to our project. The Gradle wrapper is used to download and use a specific Gradle version for a project. This version is also used by the Grails shell and commands. The default version (for Grails 3.0.12) is Gradle 2.3, which is also part of the Grails distribution. At the time of writing this blog post the latest Gradle version is 2.10. Sometimes we use Gradle plugins in our project that need a higher Gradle version, or we just want to use the latest version because of improvements in Gradle. We can change the Gradle version that needs to be used by Grails in different ways.
Grails will first look for an environment variable
GRAILS_GRADLE_HOME. It must be set to the location of a Gradle installation. If it is present is used as the Gradle version by Grails. In the following example we use this environment variable to force Grails to use Gradle 2.10:
$ GRAILS\_GRADLE\_HOME=~/.sdkman/gradle/2.10 grails BUILD SUCCESSFUL | Enter a command name to run. Use TAB for completion: grails> gradle help :help Welcome to Gradle 2.10. To run a build, run gradle ... To see a list of available tasks, run gradle tasks To see a list of command-line options, run gradle --help To see more detail about a task, run gradle help --task BUILD SUCCESSFUL Total time: 0.861 secs grails>
Another way to set the Gradle version is by change the Gradle wrapper version. In our
build.gradle file there is a task
wrapper. This creates a Gradle wrapper for our project with the version that is specified in the file
gradle.properties with the property
gradleWrapperVersion. Let's change the value of
2.10 and execute the
wrapper task. We can change the value in the
grade.properties file, the
build.gradle file or pass it via the command line:
$ ./gradlew wrapper -PgradleWrapperVersion=2.10 :wrapper BUILD SUCCESSFUL Total time: 2.67 secs $ grails BUILD SUCCESSFUL | Enter a command name to run. Use TAB for completion: grails> gradle help :help Welcome to Gradle 2.10. To run a build, run gradle ... To see a list of available tasks, run gradle tasks To see a list of command-line options, run gradle --help To see more detail about a task, run gradle help --task BUILD SUCCESSFUL Total time: 0.861 secs grails>
It could be that we get an
org/gradle/mvn3/org/apache/maven/model/building/ModelBuildingException exception after upgrading to a newer version. This is because the
io.spring.dependency-management plugin is set to a version not supported by the newer Gradle version. If we change the version of the plugin to the latest version (
0.5.4.RELEASE at the time of writing this blog post) the error is solved.
It also important to notice that Grails will look for Gradle wrapper defined for the base project if we use our Grails project in a multi-module project. So the directory that contains the
settings.gradle file is then used to look for a Gradle wrapper. If it is not found the default Gradle version that is defined by the Grails distribution is used.
Written with Grails 3.0.12.