Gradle introduced the continuous build feature in version 2.5. The feature is still incubating, but we can already use it in our daily development. The continuous build feature means Gradle will not shut down after a task is finished, but keeps running and looks for changes to files to re-run tasks automatically. It applies perfectly for a scenario where we want to re-run the test
task while we write our code. With the continuous build feature we start Gradle once with the test
task and Gradle will automatically recompile source files and run tests if a source file changes.
To use the continuous build feature we must use the command line option --continuous
or the shorter version -t
. With this option Gradle will start up in continuous mode. To stop Gradle we must use the Ctrl+D key combination.
Continue reading →
In Gradle we can configure how duplicate files should be handled by the Copy
task. Actually we can configure how duplicate files are handled by any task that implements the CopySpec
interface. For example archive tasks also implements this interface. We must use the setDuplicatesStrategy
method to configure how Gradle behaves. The parameter is a value of the enumeration DuplicatesStrategy
. We can use the values from the enum class or use String
values, which are automatically converted to enum DuplicatesStrategy
values.
We can choose the following strategies:
Continue reading →
The nice thing about Gradle is that we can use Java libraries in our build script. This way we can add extra functionality to our build script in an easy way. We must use the classpath
dependency configuration for our build script to include the library. For example we can include the library Grgit, which provides an easy way to interact with Git from Java or Groovy code. This library is also the basis for the Gradle Git plugin.
In the next example build file we add the Grgit library to our build script classpath. Then we use the open
method of the Grgit
class. From the returned object we invoke the head
to get the commit id identified as id
. With the abbreviatedId
property we get the shorter version of the Git commit id. The build file also includes the application
plugin. We customize the applicationDistribution
CopySpec
from the plugin and expand the properties in a VERSION
file. This way our distribution always includes a plain text file VERSION
with the Git commit id of the code.
Continue reading →
For Java or Groovy projects we can use the application plugin in Gradle to run and package our application. The plugin adds for example the startScripts
task which creates OS specific scripts to run the project as a JVM application. This task is then used again by the installDist
that installs the application, and distZip
and distTar
tasks that create a distributable archive of the application. The startScripts
tasks has the properties unixScript
and windowsScript
that are the actual OS specific script files to run the application. We can use these properties to change the contents of the files.
In the following sample we add the directory configuration
to the CLASSPATH
definition:
Continue reading →
We currently use Vert.x in several internal and external projects. Until the most recent project we where building our Vert.x modules using Maven. Gradle is our build tool of choice, but the default approach described at the Vert.x site caused several issues:
- The task of cloning, cleaning and configuring the template project is error-prone;
- The template project does not support recent Gradle versions >= 2.x;
- This approach is not compatible with the Gradle support in IntelliJ IDEA.
Continue reading →
To define system properties for our Gradle build we can use the command line option --system-prop
or -D
. But we can also add the values for system properties in the gradle.properties
file of our project. This file contains project properties we want to externalized, but if we prefix the property name with systemProp.
the property is turned into a system property. For a multi-module build only system properties defined in the gradle.properties
file in the root of the project structure are used, others are ignored.
In the following build script we have the task showSystemProperty
. Inside the task we assert the value of the system property sample
and the project property sample
:
Continue reading →
Migrating from Ant to Gradle is very easy with the importBuild
method from AntBuilder
. We only have to add this single line and reference our existing Ant build XML file and all Ant tasks can now be executed as Gradle tasks. We can automatically rename the Ant tasks if we want to avoid task name collisions with Gradle task names. We use a closure argument with the importBuild
method and return the new task names. The existing Ant task name is the first argument of the closure.
Let's first create a simple Ant build.xml
file:
Continue reading →
If we run a Gradle build and one of the tasks fails, the whole build stops immediately. So we have fast feedback of our build status. If we don't want to this and want Gradle to execute all tasks, even though some might have failed, we use the command line option --continue
. When we use the --continue
command line option Gradle will execute every task where the dependent tasks are not failing. This is also useful in a multi-module project where we might want to build all projects even though some may have failing tests, so we get a complete overview of failed tests for all modules.
In the following Gradle build file we have two tasks. The task failTask
throws a TaskExecutionException
to purposely fail the task. The successTask
will not fail:
Continue reading →
If we use Gradle in a multi-module project we can define project dependencies between modules. Gradle uses the information from the project dependencies to determine which tasks need to be run. For example if module B depends on module A and we want to build module B, Gradle will also build module A for us, because module B depends on it. But if we know for sure that module A is up to date and has not changed, we can also instruct Gradle to skip building module A, when we build module B.
Let's start with the following module structure, where each module depends on the module above it. So module services depends on common and module web depends on services:
Continue reading →
To get the current Gradle version we can use the gradleVersion
property of the Gradle
object. This returns a string value we can use for displaying the values. If we want to compare Gradle versions we can use the GradleVersion
object. With this class we can get the current version, but we can also compare Gradle versions. This can be useful in our build scripts if we have functionality based on a Gradle version.
In the following build file we first have a task that uses the gradleVersion
of Gradle
. Then inside the task we use the static method current
of the GradleVersion
class. We get an GradleVersion
instance and we display different properties from this instance. In the task compareGradleVersion
we create a GradleVersion
instance with the static version
method. We compare multiple GradleVersion
objects and have different functionality based on the Gradle version.
Continue reading →
To define a Copy
task we specify the files we want to copy and to which directory. This definition is a CopySpec
instance. It contains the rules that defines what we want to copy. The archive tasks Jar
, Zip
and Tar
also use a CopySpec
instance.
When we create a task of type Copy
we get a task object that implements the CopySpec
interface. We can use all the methods from this interface to extend our recipe for copying tasks. In the following build file we first define the task website
. We use CopySpec
methods to configure the task. Then we define a task deploy
of type Sync
that also implements the CopySpec
interface.
Continue reading →
Gradle uses the name build.gradle
as the default name for a build file. If we write our build code in a file build.gradle
then we don't have to specify the build filename when we run tasks. We can create build files with a different name other than build.gradle
. For example we can define our build logic in a file sample.gradle
. To run the tasks from this build file we can use the command line option -b
or --build-file
followed by the file name. But we can also change the project settings and set a new default build file name for our project. With the changed project settings we do not have to use the command line options -b
or --build-file
.
Suppose we have the following build file with the name sample.gradle
:
Continue reading →