Gradle User Manual: Version 8.1.1
- About Gradle
- Getting Started
- Upgrading and Migrating
- Running Gradle Builds
- Authoring Gradle Builds
- Build Script Basics
- Authoring Tasks
- Incremental build
- Writing Build Scripts
- Working With Files
- Using Gradle Plugins
- Build Lifecycle
- Logging
- Structuring and Building a Software Component with Gradle
- Sharing Build Logic between Subprojects
- Fine Tuning the Project Layout
- Declaring Dependencies between Subprojects
- Configuration time and execution time
- Organizing Gradle Projects
- Best practices for authoring maintainable builds
- Lazy Configuration
- Testing Build Logic with TestKit
- Using Ant from Gradle
- Dependency Management
- Java & Other JVM Projects
- C++ & Other Native Projects
- Native Projects using the Software Model
- Extending Gradle
- Reference
- Plugins
- The ANTLR Plugin
- The Application Plugin
- The Base Plugin
- Build Init Plugin
- The Checkstyle Plugin
- The CodeNarc Plugin
- The Distribution Plugin
- The Ear Plugin
- The Eclipse Plugins
- The Groovy Plugin
- The IDEA Plugin
- Ivy Publish Plugin
- The JaCoCo Plugin
- The Java Plugin
- The Java Library Plugin
- The Java Library Distribution Plugin
- The Java Platform Plugin
- Maven Publish Plugin
- The PMD Plugin
- The Scala Plugin
- The Signing Plugin
- The War Plugin
- License Information
- License Information
About Gradle
What is Gradle?
Gradle is an open-source build automation tool flexible enough to build almost any type of software. Gradle makes few assumptions about what you’re trying to build or how to build it. This makes Gradle particularly flexible.
Design
Gradle bases its design on the following fundamentals:
- High performance
-
Gradle avoids unnecessary work by only running tasks that need to do work because inputs or outputs have changed. Gradle uses various caches to reuse outputs from previous builds. With a shared build cache, you can even reuse outputs from other machines.
- JVM foundation
-
Gradle runs on the JVM. This is a bonus for users familiar with Java, since build logic can use the standard Java APIs. It also makes it easy to run Gradle on different platforms.
- Conventions
-
Gradle makes common types of projects easy to build through conventions. Plugins set sensible defaults to keep build scripts minimal. But these conventions don’t limit you: you can configure settings, add your own tasks, and make many other customizations in your builds.
- Extensibility
-
Most builds have special requirements that require custom build logic. You can readily extend Gradle to provide your own build logic with custom tasks and plugins. See Android builds for an example: they add many new build concepts such as flavors and build types.
- IDE support
-
Several major IDEs provide interaction with Gradle builds, including Android Studio, IntelliJ IDEA, Eclipse, VSCode, and NetBeans. Gradle can also generate the solution files required to load a project into Visual Studio.
- Insight
-
Build Scan™ provides extensive information about a build that you can use to identify issues. You can use Build Scans to identify problems with a build’s performance and even share them for debugging help.
Terminology
It’s helpful to know the following terminology before you dive into the details of Gradle.
Projects
Projects are the things that Gradle builds.
Projects contain a build script, which is a file located in the project’s root directory usually named build.gradle
or build.gradle.kts
.
Builds scripts define tasks, dependencies, plugins, and other configuration for that project.
A single build can contain one or more projects and each project can contain their own subprojects.
Tasks
Tasks contain the logic for executing some work—compiling code, running tests or deploying software.
In most use cases, you’ll use existing tasks.
Gradle provides tasks that implement many common build system needs, like the built-in Java Test
task that can run tests.
Plugins provide even more types of tasks.
Tasks themselves consist of:
-
Actions: pieces of work that do something, like copy files or compile source
-
Inputs: values, files and directories that the actions use or operate on
-
Outputs: files and directories that the actions modify or generate
Plugins
Plugins allow you to introduce new concepts into a build beyond tasks, files and dependency configurations. For example, most language plugins add the concept of source sets to a build.
Plugins provide a means of reusing logic and configuration across multiple projects. With plugins, you can write a task once and use it in multiple builds. Or you can store common configuration, like logging, dependencies, and version management, in one place. This reduces duplication in build scripts. Appropriately modeling build processes with plugins can greatly improve ease of use and efficiency.
Build Phases
Gradle evaluates and executes build scripts in three build phases of the Build Lifecycle:
- Initialization
-
Sets up the environment for the build and determine which projects will take part in it.
- Configuration
-
Constructs and configures the task graph for the build. Determines which tasks need to run and in which order, based on the task the user wants to run.
- Execution
-
Runs the tasks selected at the end of the configuration phase.
Builds
A build is an execution of a collection of tasks in a Gradle project. You run a build via the command line interface (CLI) or an IDE by specifying task selectors. Gradle configures the build and selects the tasks to run. Gradle runs the smallest complete set of tasks based on the requested tasks and their dependencies.
Getting Started
Getting Started
Everyone has to start somewhere and if you’re new to Gradle, this is where to begin.
Before you start
In order to use Gradle effectively, you need to know what it is and understand some of its fundamental concepts. So before you start using Gradle in earnest, we highly recommend you read What is Gradle?.
Installation
If all you want to do is run an existing Gradle build, then you don’t need to install Gradle if the build has a Gradle Wrapper, identifiable via the gradlew and/or gradlew.bat files in the root of the build. You just need to make sure your system satisfies Gradle’s prerequisites.
Android Studio comes with a working installation of Gradle, so you don’t need to install Gradle separately in that case.
In order to create a new build or add a Wrapper to an existing build, you will need to install Gradle according to these instructions. Note that there may be other ways to install Gradle in addition to those described on that page, since it’s nearly impossible to keep track of all the package managers out there.
Try Gradle
Actively using Gradle is a great way to learn about it, so once you’ve installed Gradle, try one of the introductory hands-on tutorials:
There are more samples available on the samples pages.
Command line vs IDEs
Some folks are hard-core command-line users, while others prefer to never leave the comfort of their IDE. Many people happily use both and Gradle endeavors not to discriminate. Gradle is supported by several major IDEs and everything that can be done from the command line is available to IDEs via the Tooling API.
Android Studio and IntelliJ IDEA users should consider using Kotlin DSL build scripts for the superior IDE support when editing them.
Executing Gradle builds
If you follow any of the tutorials linked above, you will execute a Gradle build. But what do you do if you’re given a Gradle build without any instructions?
Here are some useful steps to follow:
-
Determine whether the project has a Gradle wrapper and use it if it’s there — the main IDEs default to using the wrapper when it’s available.
-
Discover the project structure.
Either import the build with an IDE or run
gradle projects
from the command line. If only the root project is listed, it’s a single-project build. Otherwise it’s a multi-project build. -
Find out what tasks you can run.
If you have imported the build into an IDE, you should have access to a view that displays all the available tasks. From the command line, run
gradle tasks
. -
Learn more about the tasks via
gradle help --task <taskname>
.The
help
task can display extra information about a task, including which projects contain that task and what options the task supports. -
Run the task that you are interested in.
Many convention-based builds integrate with Gradle’s lifecycle tasks, so use those when you don’t have something more specific you want to do with the build. For example, most builds have
clean
,check
,assemble
andbuild
tasks.From the command line, just run
gradle <taskname>
to execute a particular task. You can learn more about command-line execution in the corresponding user manual chapter. If you’re using an IDE, check its documentation to find out how to run a task.
Gradle builds often follow standard conventions on project structure and tasks, so if you’re familiar with other builds of the same type — such as Java, Android or native builds — then the file and directory structure of the build should be familiar, as well as many of the tasks and project properties.
For more specialized builds or those with significant customizations, you should ideally have access to documentation on how to run the build and what build properties you can configure.
Authoring Gradle builds
Learning to create and maintain Gradle builds is a process, and one that takes a little time. We recommend that you start with the appropriate core plugins and their conventions for your project, and then gradually incorporate customizations as you learn more about the tool.
Here are some useful first steps on your journey to mastering Gradle:
-
Try one or two basic tutorials to see what a Gradle build looks like, particularly the ones that match the type of project you work with (Java, native, Android, etc.).
-
Make sure you’ve read What is Gradle?
-
Learn about the fundamental elements of a Gradle build: projects, tasks, and the file API.
-
If you are building software for the JVM, be sure to read about the specifics of those types of projects in Building Java & JVM projects and Testing in Java & JVM projects.
-
Familiarize yourself with the core plugins that come packaged with Gradle, as they provide a lot of useful functionality out of the box.
-
Learn how to author maintainable build scripts and best organize your Gradle projects.
The user manual contains a lot of other useful information and you can find samples demonstrating various Gradle features on the samples pages.
Integrating 3rd-party tools with Gradle
Gradle’s flexibility means that it readily works with other tools, such as those listed on our Gradle & Third-party Tools page.
There are two main modes of integration:
-
A tool drives Gradle — uses it to extract information about a build and run it — via the Tooling API
-
Gradle invokes or generates information for a tool via the 3rd-party tool’s APIs — this is usually done via plugins and custom task types
Tools that have existing Java-based APIs are generally straightforward to integrate. You can find many such integrations on Gradle’s plugin portal.
Installing Gradle
You can install the Gradle build tool on Linux, macOS, or Windows. This document covers installing using a package manager like SDKMAN! or Homebrew, as well as manual installation.
Use of the Gradle Wrapper is the recommended way to upgrade Gradle.
You can find all releases and their checksums on the releases page.
Prerequisites
Gradle runs on all major operating systems and requires only a Java Development Kit version 8 or higher to run. To check, run java -version
. You should see something like this:
❯ java -version java version "1.8.0_151" Java(TM) SE Runtime Environment (build 1.8.0_151-b12) Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode)
Gradle ships with its own Groovy library, therefore Groovy does not need to be installed. Any existing Groovy installation is ignored by Gradle.
Gradle uses whatever JDK it finds in your path. Alternatively, you can set the JAVA_HOME
environment variable to point to the installation directory of the desired JDK.
Installing with a package manager
SDKMAN! is a tool for managing parallel versions of multiple Software Development Kits on most Unix-like systems (macOS, Linux, Cygwin, Solaris and FreeBSD). We deploy and maintain the versions available from SDKMAN!.
❯ sdk install gradle
Homebrew is "the missing package manager for macOS".
❯ brew install gradle
MacPorts is another package manager for macOS.
❯ sudo port install gradle
Other package managers are available, but the version of Gradle distributed by them is not controlled by Gradle, Inc. Linux package managers may distribute a modified version of Gradle that is incompatible or incomplete when compared to the official version (available from SDKMAN! or below).
Installing manually
Step 1. Download the latest Gradle distribution
The distribution ZIP file comes in two flavors:
-
Binary-only (bin)
-
Complete (all) with docs and sources
Need to work with an older version? See the releases page.
Step 2. Unpack the distribution
Linux & MacOS users
Unzip the distribution zip file in the directory of your choosing, e.g.:
❯ mkdir /opt/gradle ❯ unzip -d /opt/gradle gradle-8.1.1-bin.zip ❯ ls /opt/gradle/gradle-8.1.1 LICENSE NOTICE bin README init.d lib media
Microsoft Windows users
Create a new directory C:\Gradle
with File Explorer.
Open a second File Explorer window and go to the directory where the Gradle distribution was downloaded. Double-click the ZIP archive to expose the content. Drag the content folder gradle-8.1.1
to your newly created C:\Gradle
folder.
Alternatively, you can unpack the Gradle distribution ZIP into C:\Gradle
using an archiver tool of your choice.
Step 3. Configure your system environment
To run Gradle, the path to the unpacked files from the Gradle website need to be on your terminal’s path. The steps to do this are different for each operating system.
Linux & MacOS users
Configure your PATH
environment variable to include the bin
directory of the unzipped distribution, e.g.:
❯ export PATH=$PATH:/opt/gradle/gradle-8.1.1/bin
Alternatively, you could also add the environment variable GRADLE_HOME
and point this to the unzipped distribution. Instead of adding a specific version of Gradle to your PATH
, you can add $GRADLE_HOME/bin
to your PATH
. When upgrading to a different version of Gradle, just change the GRADLE_HOME
environment variable.
Microsoft Windows users
In File Explorer right-click on the This PC
(or Computer
) icon, then click Properties
→ Advanced System Settings
→ Environmental Variables
.
Under System Variables
select Path
, then click Edit
. Add an entry for C:\Gradle\gradle-8.1.1\bin
. Click OK to save.
Alternatively, you could also add the environment variable GRADLE_HOME
and point this to the unzipped distribution. Instead of adding a specific version of Gradle to your Path
, you can add %GRADLE_HOME%/bin
to your Path
. When upgrading to a different version of Gradle, just change the GRADLE_HOME
environment variable.
Verifying installation
Open a console (or a Windows command prompt) and run gradle -v
to run gradle and display the version, e.g.:
❯ gradle -v ------------------------------------------------------------ Gradle 8.1.1 ------------------------------------------------------------ (environment specific information)
If you run into any trouble, see the section on troubleshooting installation.
You can verify the integrity of the Gradle distribution by downloading the SHA-256 file (available from the releases page) and following these verification instructions.
Next steps
Now that you have Gradle installed, use these resources for getting started:
-
Create your first Gradle project by following one of our step-by-step samples.
-
Sign up for a live introductory Gradle training with a core engineer.
-
Learn how to achieve common tasks through the command-line interface.
-
Configure Gradle execution, such as use of an HTTP proxy for downloading dependencies.
-
Subscribe to the Gradle Newsletter for monthly release and community updates.
Troubleshooting builds
The following is a collection of common issues and suggestions for addressing them. You can get other tips and search the Gradle forums and StackOverflow #gradle answers, as well as Gradle documentation from help.gradle.org.
Troubleshooting Gradle installation
If you followed the installation instructions, and aren’t able to execute your Gradle build, here are some tips that may help.
If you installed Gradle outside of just invoking the Gradle Wrapper, you can check your Gradle installation by running gradle --version
in a terminal.
You should see something like this:
❯ gradle --version ------------------------------------------------------------ Gradle 6.5 ------------------------------------------------------------ Build time: 2020-06-02 20:46:21 UTC Revision: a27f41e4ae5e8a41ab9b19f8dd6d86d7b384dad4 Kotlin: 1.3.72 Groovy: 2.5.11 Ant: Apache Ant(TM) version 1.10.7 compiled on September 1 2019 JVM: 14 (AdoptOpenJDK 14+36) OS: Mac OS X 10.15.2 x86_64
If not, here are some things you might see instead.
Command not found: gradle
If you get "command not found: gradle", you need to ensure that Gradle is properly added to your PATH
.
JAVA_HOME is set to an invalid directory
If you get something like:
ERROR: JAVA_HOME is set to an invalid directory Please set the JAVA_HOME variable in your environment to match the location of your Java installation.
You’ll need to ensure that a Java Development Kit version 8 or higher is properly installed, the JAVA_HOME
environment variable is set, and Java is added to your PATH
.
Permission denied
If you get "permission denied", that means that Gradle likely exists in the correct place, but it is not executable.
You can fix this using chmod +x path/to/executable
on *nix-based systems.
Other installation failures
If gradle --version
works, but all of your builds fail with the same error, it is possible there is a problem with one of your Gradle build configuration scripts.
You can verify the problem is with Gradle scripts by running gradle help
which executes configuration scripts, but no Gradle tasks. If the error persists, build configuration is problematic.
If not, then the problem exists within the execution of one or more of the requested tasks (Gradle executes configuration scripts first, and then executes build steps).
Debugging dependency resolution
Common dependency resolution issues such as resolving version conflicts are covered in Troubleshooting Dependency Resolution.
You can see a dependency tree and see which resolved dependency versions differed from what was requested by clicking the Dependencies view and using the search functionality, specifying the resolution reason.

The actual build scan with filtering criteria is available for exploration.
Troubleshooting slow Gradle builds
For build performance issues (including “slow sync time”), see improving the Performance of Gradle Builds.
Android developers should watch a presentation by the Android SDK Tools team about Speeding Up Your Android Gradle Builds. Many tips are also covered in the Android Studio user guide on optimizing build speed.
Debugging build logic
Attaching a debugger to your build
You can set breakpoints and debug buildSrc and standalone plugins in your Gradle build itself by setting the org.gradle.debug
property to “true” and then attaching a remote debugger to port 5005.
You can change the port number by setting the org.gradle.debug.port
property to the desired port number.
To attach the debugger remotely via network, you need to set the org.gradle.debug.host
property to the machine’s IP address or *
(listen on all interfaces).
❯ gradle help -Dorg.gradle.debug=true
In addition, if you’ve adopted the Kotlin DSL, you can also debug build scripts themselves.
The following video demonstrates how to debug an example build using IntelliJ IDEA.

Adding and changing logging
In addition to controlling logging verbosity, you can also control display of task outcomes (e.g. “UP-TO-DATE”) in lifecycle logging using the --console=verbose
flag.
You can also replace much of Gradle’s logging with your own by registering various event listeners. One example of a custom event logger is explained in the logging documentation. You can also control logging from external tools, making them more verbose in order to debug their execution.
Note
|
Additional logs from the Gradle Daemon can be found under $GRADLE_USER_HOME/daemon/<gradle-version>/ .
|
Task executed when it should have been UP-TO-DATE
--info
logs explain why a task was executed, though build scans do this in a searchable, visual way by going to the Timeline view and clicking on the task you want to inspect.

You can learn what the task outcomes mean from this listing.
Debugging IDE integration
Many infrequent errors within IDEs can be solved by "refreshing" Gradle. See also more documentation on working with Gradle in IntelliJ IDEA and in Eclipse.
Refreshing IntelliJ IDEA
NOTE: This only works for Gradle projects linked to IntelliJ.
From the main menu, go to View
> Tool Windows
> Gradle
. Then click on the Refresh icon.

Refreshing Eclipse (using Buildship)
If you’re using Buildship for the Eclipse IDE, you can re-synchronize your Gradle build by opening the "Gradle Tasks" view and clicking the "Refresh" icon, or by executing the Gradle
> Refresh Gradle Project
command from the context menu while editing a Gradle script.

Troubleshooting daemon connection issues
If your Gradle build fails before running any tasks, you may be encountering problems with your network configuration. When Gradle is unable to communicate with the Gradle daemon process, the build will immediately fail with a message similar to this:
$ gradle help
Starting a Gradle Daemon, 1 stopped Daemon could not be reused, use --status for details
FAILURE: Build failed with an exception.
* What went wrong:
A new daemon was started but could not be connected to: pid=DaemonInfo{pid=55913, address=[7fb34c82-1907-4c32-afda-888c9b6e2279 port:42751, addresses:[/127.0.0.1]], state=Busy, ...
We have observed this can occur when network address translation (NAT) masquerade is used. When NAT masquerade is enabled, connections that should be considered local to the machine are masked to appear from external IP addresses. Gradle refuses to connect to any external IP address as a security precaution.
The solution to this problem is to adjust your network configuration such that local connections are not modified to appear as from external addresses.
You can monitor the detected network setup and the connection requests in the daemon log file ($GRADLE_USER_HOME/daemon/<Gradle version>/daemon-<PID>.out.log
).
2021-08-12T12:01:50.755+0200 [DEBUG] [org.gradle.internal.remote.internal.inet.InetAddresses] Adding IP addresses for network interface enp0s3
2021-08-12T12:01:50.759+0200 [DEBUG] [org.gradle.internal.remote.internal.inet.InetAddresses] Is this a loopback interface? false
2021-08-12T12:01:50.769+0200 [DEBUG] [org.gradle.internal.remote.internal.inet.InetAddresses] Adding remote address /fe80:0:0:0:85ba:3f3e:1b88:c0e1%enp0s3
2021-08-12T12:01:50.770+0200 [DEBUG] [org.gradle.internal.remote.internal.inet.InetAddresses] Adding remote address /10.0.2.15
2021-08-12T12:01:50.770+0200 [DEBUG] [org.gradle.internal.remote.internal.inet.InetAddresses] Adding IP addresses for network interface lo
2021-08-12T12:01:50.771+0200 [DEBUG] [org.gradle.internal.remote.internal.inet.InetAddresses] Is this a loopback interface? true
2021-08-12T12:01:50.771+0200 [DEBUG] [org.gradle.internal.remote.internal.inet.InetAddresses] Adding loopback address /0:0:0:0:0:0:0:1%lo
2021-08-12T12:01:50.771+0200 [DEBUG] [org.gradle.internal.remote.internal.inet.InetAddresses] Adding loopback address /127.0.0.1
2021-08-12T12:01:50.775+0200 [DEBUG] [org.gradle.internal.remote.internal.inet.TcpIncomingConnector] Listening on [7fb34c82-1907-4c32-afda-888c9b6e2279 port:42751, addresses:[localhost/127.0.0.1]].
...
2021-08-12T12:01:50.797+0200 [INFO] [org.gradle.launcher.daemon.server.DaemonRegistryUpdater] Advertising the daemon address to the clients: [7fb34c82-1907-4c32-afda-888c9b6e2279 port:42751, addresses:[localhost/127.0.0.1]]
...
2021-08-12T12:01:50.923+0200 [ERROR] [org.gradle.internal.remote.internal.inet.TcpIncomingConnector] Cannot accept connection from remote address /10.0.2.15.
Getting additional help
If you didn’t find a fix for your issue here, please reach out to the Gradle community on the help forum or search relevant developer resources using help.gradle.org.
If you believe you’ve found a bug in Gradle, please file an issue on GitHub.
Compatibility Matrix
The sections below describe Gradle’s compatibility with several integrations. Other versions not listed here may or may not work.
Java
A Java version between 8 and 19 is required to execute Gradle. Java 20 and later versions are not yet supported.
Java 6 and 7 can still be used for compilation, but are deprecated for use with testing. Testing with Java 6 and 7 will not be supported in Gradle 9.0.
Any fully supported version of Java can be used for compile or test. The latest Java version may however only be supported for compile or test but not yet for running Gradle.
For older Gradle versions, please see the table below which Java version is supported by which Gradle release.
Java version |
First Gradle version to support it |
8 |
2.0 |
9 |
4.3 |
10 |
4.7 |
11 |
5.0 |
12 |
5.4 |
13 |
6.0 |
14 |
6.3 |
15 |
6.7 |
16 |
7.0 |
17 |
7.3 |
18 |
7.5 |
19 |
7.6 |
20 |
8.1 ⚠ |
⚠: Indicates that the Java version can be used for compilation and tests, but not yet running Gradle itself.
Kotlin
Gradle is tested with Kotlin 1.6.10 through 1.8.10. Beta and RC versions may or may not work.
Gradle version |
Embedded Kotlin version |
Kotlin Language version |
5.0 |
1.3.10 |
1.3 |
5.1 |
1.3.11 |
1.3 |
5.2 |
1.3.20 |
1.3 |
5.3 |
1.3.21 |
1.3 |
5.5 |
1.3.31 |
1.3 |
5.6 |
1.3.41 |
1.3 |
6.0 |
1.3.50 |
1.3 |
6.1 |
1.3.61 |
1.3 |
6.3 |
1.3.70 |
1.3 |
6.4 |
1.3.71 |
1.3 |
6.5 |
1.3.72 |
1.3 |
6.8 |
1.4.20 |
1.3 |
7.0 |
1.4.31 |
1.4 |
7.2 |
1.5.21 |
1.4 |
7.3 |
1.5.31 |
1.4 |
7.5 |
1.6.21 |
1.4 |
7.6 |
1.7.10 |
1.4 |
8.0 |
1.8.10 |
1.8 |
Groovy
Gradle is tested with Groovy 1.5.8 through 4.0.0.
Gradle plugins written in Groovy must use Groovy 3.x for compatibility with Gradle and Groovy DSL build scripts.
Android
Gradle is tested with Android Gradle Plugin 7.3, 7.4 and 8.0. Alpha and beta versions may or may not work.
Upgrading and Migrating
Upgrading your build from Gradle 6.x to 7.0
This chapter provides the information you need to migrate your Gradle 6.x builds to Gradle 7.0. For migrating from Gradle 5.x or earlier, complete the older migration guide first.
We recommend the following steps for all users:
-
Try running
gradle help --scan
and view the deprecations view of the generated build scan.This is so that you can see any deprecation warnings that apply to your build.
Alternatively, you could run
gradle help --warning-mode=all
to see the deprecations in the console, though it may not report as much detailed information. -
Update your plugins.
Some plugins will break with this new version of Gradle, for example because they use internal APIs that have been removed or changed. The previous step will help you identify potential problems by issuing deprecation warnings when a plugin does try to use a deprecated part of the API.
-
Run
gradle wrapper --gradle-version 7.0
to update the project to 7.0. -
Try to run the project and debug any errors using the Troubleshooting Guide.
Upgrading from 6.9
Changes in the IDE integration
Changes in the IDEA model
The getGeneratedSourceDirectories()
and getGeneratedTestDirectories()
methods are removed from the IdeaContentRoot
interface.
Clients should replace these invocations with getSourceDirectories()
and getTestDirectories()
and use the isGenerated()
method on the returned instances.
Dependency locking now defaults to a single file per project
The format of the dependency lockfile has been changed and as a consequence there is only one file per project instead of one file per configuration per project. This change only affects writing lock files. Gradle remains capable of loading lock state saved in the older format.
Head over to the documentation to learn how to migrate to the new format. The migration can be performed per configuration and does not have to be done in a single step. Gradle will automatically clean up previous lock files when migrating them over to the new file format.
Gradle Module Metadata is now reproducible by default
The buildId
field will not be populated by default to ensure that the produced metadata file remains unchanged when no build inputs are changed.
Users can still opt in to have this unique identifier part of the produced metadata if they want to, see the documentation.
The jcenter()
convenience method is now deprecated
JFrog announced the sunset of the JCenter repository in February 2021. Many Gradle builds rely on JCenter for project dependencies.
No new packages or versions are published to JCenter, but JFrog says they will keep JCenter running in a read-only state indefinitely.
We recommend that you consider using mavenCentral()
, google()
or a private maven
repository instead.
Gradle emits a deprecation warning when jcenter()
is used as a repository and this method is scheduled to be removed in Gradle 8.0.
Potential breaking changes
Updates to bundled Gradle dependencies
-
Kotlin has been updated to Kotlin 1.4.31.
-
Groovy has been updated to Groovy 3.0.7.
Changes to Groovy and Groovy DSL
Due to the update to the next major version of Groovy, you may experience minor issues when upgrading to Gradle 7.0.
The new version of Groovy has a stricter parser that fails to compile code that may have been accepted in previous Groovy versions. If you encounter syntax errors, check the Groovy issue tracker and Groovy 3 release highlights.
Some very specific regressions have already been fixed in the next minor version of Groovy.
Gradle no longer embeds a copy of groovy-all
that bundles all Groovy modules into a single jar—only the most important modules are distributed in the Gradle distribution.
The localGroovy()
dependency will include these Groovy modules:
-
groovy
-
groovy-ant
-
groovy-astbuilder
-
groovy-console
-
groovy-datetime
-
groovy-dateutil
-
groovy-groovydoc
-
groovy-json
-
groovy-nio
-
groovy-sql
-
groovy-templates
-
groovy-test
-
groovy-xml
But the following Groovy modules are not included:
-
groovy-cli-picocli
-
groovy-docgenerator
-
groovy-groovysh
-
groovy-jmx
-
groovy-jsr223
-
groovy-macro
-
groovy-servlet
-
groovy-swing
-
groovy-test-junit5
-
groovy-testng
You can pull these dependencies into your build like any other external dependency.
Plugins built with Gradle 7.0 will now have Groovy 3 on their classpath when using gradleApi()
or localGroovy()
.
Note
|
If you use Spock to test your plugins, you will need to use Spock 2.x. There are no compatible versions of Spock 1.x and Groovy 3. |
dependencies {
// Ensure you use the Groovy 3.x variant
testImplementation('org.spockframework:spock-core:2.0-groovy-3.0') {
exclude group: 'org.codehaus.groovy'
}
}
// Spock 2 is based on JUnit Platform which needs to be enabled explicitly.
tasks.withType(Test).configureEach {
useJUnitPlatform()
}
Depending on the number of subprojects and Groovy DSL build scripts, you may notice a performance regression when compiling build scripts for the first time or when changes are made to the build script’s classpath. This is due to the slower performance of the Groovy 3 parser, but the Groovy team is aware of the issue and trying to mitigate the regression.
In general, we are also looking at how we can improve the performance of build script compilation for both Groovy DSL and Kotlin DSL.
While the following error initially looks like a compile error, it is actually due to the fact that specific `Configuration`s have been removed.
Please refer to Removal of compile
and runtime
configurations for more details.
Could not find method testCompile() for arguments [DefaultExternalModuleDependency{group='org.junit', name='junit-bom', version='5.7.0', configuration='default'}] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
Updates to default tool integration versions
-
PMD has been updated to PMD 6.31.0.
-
Groovy and GroovyDoc have been updated to Groovy 3.0.7.
Removal of compile
and runtime
configurations
Since its inception, Gradle provided the compile
and runtime
configurations to declare dependencies.
These however did not support a fine grained scoping of dependencies. Hence, better replacements were introduced in Gradle 3.4:
-
The
implementation
configuration should be used to declare dependencies which are implementation details of a library: they are not visible to consumers of the library during compilation time. -
The
api
configuration, available only if you apply thejava-library
plugin, should be used to declare dependencies which are part of the API of a library, that need to be exposed to consumers at compilation time.
In Gradle 7, both the compile
and runtime
configurations are removed.
Therefore, you have to migrate to the implementation
and api
configurations above.
If you are still using the java
plugin for a Java library, you will need to apply the java-library
plugin instead.
Removed Configuration | New Configuration |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
You can find more details about the benefits of the new configurations and which one to use in place of compile
and runtime
by reading the Java Library plugin documentation.
Warning
|
When using the Groovy DSL, you need to watch out for a particular upgrade problem when dealing with the removed configurations. If you were creating custom configurations that extend one of the removed configurations, Gradle may silently create configurations that do not exist. This looks something like:
The result of dependency resolution for your custom configuration may not be the same as Gradle 6.x or before. You may notice missing dependencies or artifacts. |
Location of temporary project files for ProjectBuilder
The ProjectBuilder
API is used for inspecting Gradle builds in unit tests. This API used to create temporary project files under the system temporary directory as defined by java.io.tmpdir
.
The API now creates temporary project files under the Test
task’s temporary directory. This path is usually under the project build directory.
This may cause test failures when the test expects particular file paths.
If the test uses ProjectBuilder.withProjectDir(…)
, it is unaffected.
Location of temporary files for TestKit tests
Tests that use the TestKit API used to create temporary files under the system temporary directory as defined by java.io.tmpdir
.
These files were used to store copies of Gradle distributions or another test-only Gradle User Home.
TestKit tests will now create temporary files under the Test
task’s temporary directory. This path is usually under the project build directory.
This may cause test failures when the test expects particular file paths.
If the test uses GradleRunner.withTestKitDir(…)
, it is unaffected.
File system watching with TestKit on Windows
The file system watching implementation on Windows adds a lock to the root project directory in order to watch for changes.
This may cause errors when you try to delete the root project directory after running a build with TestKit.
For example, tests that use TestKit together with JUnit’s @TempDir
extension, or the TemporaryFolder
rule can run into this problem.
To avoid problems with these file locks, TestKit
disables file system watching for builds executed on Windows via GradleRunner
.
If you’d like to override the default behavior, you can enable file system watching by passing --watch-fs
to GradleRunner.withArguments()
.
Removal of the legacy maven
plugin
The maven
plugin has been removed.
You should use the maven-publish
plugin instead.
Please refer to the documentation of the Maven Publish plugin for more details.
Removal of the uploadArchives
task
The uploadArchives
task was used in combination with the legacy Ivy or Maven publishing mechanisms.
It has been removed in Gradle 7.
You should migrate to the maven-publish
or ivy-publish
plugin instead.
Please refer to the documentation of the Maven Publish plugin for publishing on Maven repositories. Please refer to the documentation of the Ivy Publish plugin for publishing on Ivy repositories.
Changes in dependency version sorting
In the context of dependency version sorting, a -SNAPSHOT
version is now considered to be right before a final release but after any -RC
version.
More special version suffixes are also taken into account.
This brings the Gradle algorithm closer to the Maven one for well-known version suffixes.
Have a look at the documentation for all the rules Gradle applies.
Removal of Play Framework plugins
The deprecated Play plugins have been removed. An external replacement, the Play Framework plugin, is available from the plugin portal.
Removal of deprecated JVM plugins
These unmaintained alternative JVM plugins have been removed:
java-lang
, scala-lang
, junit-test-suite
, jvm-component
, jvm-resources
.
Please use the stable Java Library and Scala plugins instead.
Removal of experimental JavaScript plugins
The following plugins for experimental JavaScript integration are now removed from the distribution:
coffeescript-base
, envjs
, javascript-base
, jshint
, rhino
.
If you used these plugins despite their experimental nature, you may find suitable replacements in the Plugin Portal.
Configuring the layout of an Ivy repository
The layout
method taking a configuration block has been removed and is replaced by patternLayout.
Executing a Gradle build without a settings file is now an error
A Gradle build is defined by its settings.gradle(.kts)
file found in the current or parent directory.
Without a settings file, a Gradle build is undefined and Gradle produces an error when attempting to execute tasks.
To fix this error, create a settings.gradle(.kts)
file for the build.
Exceptions to this are invoking Gradle with the init
task or using diagnostic command line flags, such as --version
.
Calling Project.afterEvaluate() after project evaluation is now an error
Gradle 6.x warns users about the wrong behavior and ignores the target action in this scenario.
Starting from 7.0 the same case will produce an error. Plugins and build scripts should be adjusted to call afterEvaluate
only at configuration time.
If you have such a build failure and the related afterEvaluate
statement is declared in your build sources then you can simply delete it.
If afterEvaluate
is declared in a plugin then report the issue to the plugin maintainers.
Modifying file collections after values finalized is now an error
Calling any mutator methods (i.e. clear()
, add()
, remove()
, etc.) on ConfigurableFileCollection
after the stored value calculated throws an exception.
Users and plugin authors should adjust their code such that all configuration on ConfigurableFileCollection
happens during configuration time, before the values are read.
Removal of ProjectLayout#configurableFiles
Please use ObjectFactory#fileCollection()
instead.
Removal of BasePluginConvention.libsDir
and BasePluginConvention.distsDir
Please use the libsDirectory
and distsDirectory
properties instead.
Removal of UnableToDeleteFileException
Existing usages should be replaced with RuntimeException
.
Properties removed in Checkstyle and PMD plugins
-
The
configDir
getters and setters have been removed from the Checkstle task and extension. Use theconfigDirectory
property instead. -
The
rulePriority
getter and setter have been removed from the Pmd task and extension. Use therulesMinimumPriority
property instead.
Removal of baseName
property in distribution
plugin
The getBaseName()
and setBaseName()
methods were removed from the Distribution
class. Clients should replace the usages with the distributionBaseName
property.
Using AbstractTask
Registering a task with the AbstractTask
type or with a type extending AbstractTask
was deprecated in Gradle 6.5 and is now an error in Gradle 7.0.
You can use DefaultTask instead.
Removal of BuildListener.buildStarted(Gradle)
BuildListener.buildStarted(Gradle)
was deprecated in Gradle 6.0 and is now removed in Gradle 7.0.
Please use BuildListener.beforeSettings(Settings) instead.
Removal of unused StartParameter
APIs
The following APIs, which were not usable via command line options anymore since Gradle 5.0, are now removed:
StartParameter.useEmptySettings()
, StartParameter.isUseEmptySettings()
, StartParameter.setSearchUpwards(boolean)
and StartParameter.isSearchUpwards()
.
Removal of searching for settings files in 'master' directories
Gradle no longer supports discovering the settings file in a directory named master
in a sibling directory.
If your build still uses this deprecated feature, consider refactoring the build to have the root directory match the physical root of the project hierarchy.
You can find more information about how to structure a Gradle build or a composition of builds in the user manual.
Alternatively, you can still run tasks in builds like this by invoking the build from the master
directory only using a
fully qualified path to the task.
modularity.inferModulePath
defaults to 'true'
Compiling,
testing and
executing
now works automatically for any source set that defines a module by containing a module-info.java
file.
Usually, this is the behavior you need.
If this is causing issues in cases you manually configure the module path, or use a 3rd party plugin for it, you can still opt out of this by setting modularity.inferModulePath
to false
on the java extension or individual tasks.
Removal of ValidateTaskProperties
The ValidateTaskProperties
task has been removed and replaced by the ValidatePlugins task.
Removal of ImmutableFileCollection
The ImmutableFileCollection
type has been removed.
Use the factory method instead.
A handle to the project layout can be obtained via Project.layout.
Removal of ComponentSelectionReason.getDescription
The method ComponentSelectionReason.getDescription
has been removed.
It is replaced by ComponentSelectionReason.getDescriptions
which returns a list of ComponentSelectionDescriptor
, each having a getDescription
.
Removal of domain object collection constructors
The following deprecated constructors were removed:
-
DefaultNamedDomainObjectList(Class, Instantiator, Namer)
-
DefaultNamedDomainObjectSet(Class, Instantiator)
-
DefaultPolymorphicDomainObjectContainer(Class, Instantiator)
-
FactoryNamedDomainObjectContainer(Class, Instantiator, NamedDomainObjectFactory)
Removal of arbitrary local cache configuration
The local build cache configuration now needs to be done via BuildCacheConfiguration.local().
Removal of DefaultVersionSelectorScheme constructor
This internal API was used in plugins, amongst other the Nebula plugins, and was deprecated in the Gradle 5.x timeline and is now removed. Latest plugins version should no longer reference it.
Setting the config_loc
config property on the checkstyle
plugin is now an error
The checkstyle
plugin now fails for the following configuration
checkstyle {
configProperties['config_loc'] = file("path/to/checkstyle-config-dir")
}
Builds should declare the checkstyle configuration with the checkstyle
block:
checkstyle {
configDirectory = file("path/to/checkstyle-config-dir")
}
Querying the mapped value of a provider before the producer has completed is now an error
Gradle 6.x warns users about the wrong behavior and then returns a possibly incorrect provider value. Starting with 7.0 the same case will produce an error. Plugins and build scripts should be adjusted to query the mapped value of a provider, for example a task output property, after the task has completed.
Task validation problems are now errors
Gradle 6.0 started warning about problems with task definitions (such as incorrectly defined inputs or outputs). For Gradle 7.0, those warnings are now errors and will fail the build.
Change in behavior when there’s a strict version conflict with a local project
Previous Gradle releases had an inconsistent behavior in regard to conflict resolution in a particular configuration:
- your project declares a strict dependency on a published module (for example, com.mycompany:some-module:1.2!!
, where 1.2!!
is the short hand notation for a strict dependency on 1.2)
- your build actually provides com.mycompany:some-module
in a higher version
Previous Gradle releases would succeed, selecting the project dependency despite the strict constraint. Starting from Gradle 7, this will trigger a dependency resolution failure.
See this issue for more context.
Deprecations
Missing dependencies between tasks
Having a task which produces an output in a location and another task consuming that location by referring to it as an input without the consumer task depending on the producer task has been deprecated. A fix for this problem is to add a dependency from the consumer to the producer.
Duplicates strategy
Gradle 7 now fails when a copy operation (or any operation which uses a org.gradle.api.file.CopySpec
) encounters a duplicate entry, and that the duplicates strategy isn’t set.
Please look at the CopySpec docs for details.
Upgrading from 6.8
No upgrade notes from 6.8 to 6.9, as 6.9 only contains bug fixes.
Upgrading from 6.7
Potential breaking changes
Toolchain API is now marked as @NonNull
The API supporting the Java Toolchain feature in org.gradle.jvm.toolchain
is now marked as @NonNull
.
This may impact Kotlin consumers where the return types of APIs are no longer nullable.
Updates to default tool integration versions
-
JaCoCo has been updated to 0.8.6.
-
Checkstyle has been updated to Checkstyle 8.37.
-
CodeNarc has been updated to CodeNarc 2.0.0.
Updates to bundled Gradle dependencies
-
Kotlin has been updated to Kotlin 1.4.20. Note that Gradle scripts are still using the Kotlin 1.3 language.
-
Apache Ant has been updated to 1.10.9 to fix CVE-2020-11979
Projects imported into Eclipse now include custom source set classpaths
Previously, projects imported by Eclipse only included dependencies for the main and test source sets. The compile and runtime classpaths of custom source sets were ignored.
Since Gradle 6.8, projects imported into Eclipse include the compile and runtime classpath for every source set defined by the build.
SourceTask is no longer sensitive to empty directories
Previously, empty directories would be taken into account during up-to-date checks and build cache key calculations for the sources declared in SourceTask
.
This meant that a source tree that contained an empty directory and an otherwise identical source tree that did not contain the empty directory would be considered different sources, even if the task would produce the same outputs.
In Gradle 6.8, SourceTask
now ignores empty directories during doing up-to-date checks and build cache key calculations.
In the vast majority of cases, this is the desired behavior, but it is possible that a task may extend SourceTask
but also produce different outputs when empty directories are present in the sources.
For tasks where this is a concern, you can expose a separate property without the @IgnoreEmptyDirectories
annotation in order to capture those changes:
@InputFiles
@SkipWhenEmpty
@PathSensitive(PathSensitivity.ABSOLUTE)
public FileTree getSourcesWithEmptyDirectories() {
return super.getSource()
}
Changes to publications
Publishing a component which has a dependency on an enforced platform now triggers a validation error, preventing accidental publishing of bad metadata: enforced platforms use cases should be limited to applications, not things which can be consumed from another library or an application.
If, for some reason, you still want to publish components with dependencies on enforced platforms, you can disable the validation following the documentation.
Changing default excludes during the execution phase
Gradle’s file trees apply some default exclude patterns for convenience — the same defaults as Ant in fact.
See the user manual for more information.
Sometimes, Ant’s default excludes prove problematic, for example when you want to include the .gitignore
in an archive file.
Changing Gradle’s default excludes during the execution phase can lead to correctness problems with up-to-date checks. As a consequence, you are only allowed to change Gradle’s default excludes in the settings script, see the user manual for an example.
Deprecations
Referencing tasks from included builds
Direct references to tasks from included builds in mustRunAfter
, shouldRunAfter
and finalizedBy
task methods have been deprecated.
Task ordering using mustRunAfter
and shouldRunAfter
as well as finalizers specified by finalizedBy
should be used for task ordering within a build.
If you happen to have cross-build task ordering defined using above mentioned methods, consider restructuring such builds and decoupling them from one another.
Searching for settings files in 'master' directories
Gradle will emit a deprecation warning when your build relies on finding the settings file in a directory named master
in a sibling directory.
If your build uses this feature, consider refactoring the build to have the root directory match the physical root of the project hierarchy.
Alternatively, you can still run tasks in builds like this by invoking the build from the master
directory only using a
fully qualified path to the task.
Using method NamedDomainObjectContainer<T>.invoke(kotlin.Function1)
Gradle Kotlin DSL extensions have been changed to favor Gradle’s Action<T>
type over Kotlin function types.
While the change should be transparent to Kotlin clients, Java clients calling Kotlin DSL extensions need to be updated to use the Action<T>
APIs.
Upgrading from 6.6
Potential breaking changes
buildSrc can now see included builds from the root
Previously, buildSrc
was built in such a way that included builds were ignored from the root build.
Since Gradle 6.7, buildSrc
can see any included build from the root build.
This may cause dependencies to be substituted from an included build in buildSrc
.
This may also change the order in which some builds are executed if an included build is needed by buildSrc
.
Updates to default tool integration versions
-
PMD has been updated to PMD 6.26.0.
-
Checkstyle has been updated to Checkstyle 8.35.
-
CodeNarc has been updated to CodeNarc 1.6.1.
Deprecations
Changing default excludes during the execution phase
Gradle’s file trees apply some default exclude patterns for convenience — the same defaults as Ant in fact.
See the user manual for more information.
Sometimes, Ant’s default excludes prove problematic, for example when you want to include the .gitignore
in an archive file.
Changing Gradle’s default excludes during the execution phase can lead to correctness problems with up-to-date checks, and is deprecated. You are only allowed to change Gradle’s default excludes in the settings script, see the user manual for an example.
Using a Configuration directly as a dependency
Gradle allowed instances of Configuration
to be used directly as dependencies:
dependencies {
implementation(configurations.myConfiguration)
}
This behavior is now deprecated as it is confusing: one could expect the "dependent configuration" to be resolved first and add the result of resolution as dependencies to the including configuration, which is not the case. The deprecated version can be replaced with the actual behavior, which is configuration inheritance:
configurations.implementation.extendsFrom(configurations.myConfiguration)
Upgrading from 6.5
Potential breaking changes
Updates to bundled Gradle dependencies
-
Ant has been updated to 1.10.8.
-
Groovy has been updated to Groovy 2.5.12.
Dependency substitutions and variant aware dependency resolution
While adding support for expressing variant support in dependency substitutions, a bug fix introduced a behaviour change that some builds may rely upon. Previously a substituted dependency would still use the attributes of the original selector instead of the ones from the replacement selector.
With that change, existing substitutions around dependencies with richer selectors, such as for platform dependencies, will no longer work as they did. It becomes mandatory to define the variant aware part in the target selector.
You can be affected by this change if you:
-
have dependencies on platforms, like
implementation platform("org:platform:1.0")
-
or if you specify attributes on dependencies,
-
and you use resolution rules on these dependencies.
See the documentation for resolving issues if you are impacted.
Deprecations
No deprecations were made in Gradle 6.6.
Upgrading from 6.4
Potential breaking changes
Updates to bundled Gradle dependencies
-
Kotlin has been updated to Kotlin 1.3.72.
-
Groovy has been updated to Groovy 2.5.11.
Updates to default tool integration versions
-
PMD has been updated to PMD 6.23.0.
Deprecations
Internal class AbstractTask is deprecated
AbstractTask
is an internal class which is visible on the public API, as a superclass of public type DefaultTask
.
AbstractTask
will be removed in Gradle 7.0, and the following are deprecated in Gradle 6.5:
-
Registering a task whose type is
AbstractTask
orTaskInternal
. You can remove the task type from the task registration and Gradle will useDefaultTask
instead. -
Registering a task whose type is a subclass of
AbstractTask
but not a subclass ofDefaultTask
. You can change the task type to extendDefaultTask
instead. -
Using the class
AbstractTask
from plugin code or build scripts. You can change the code to useDefaultTask
instead.
Upgrading from 6.3
Potential breaking changes
PMD plugin expects PMD 6.0.0 or higher by default
Gradle 6.4 enabled incremental analysis by default. Incremental analysis is only available in PMD 6.0.0 or higher. If you want to use an older PMD version, you need to disable incremental analysis:
pmd {
incrementalAnalysis = false
}
Changes in dependency locking
With Gradle 6.4, the incubating API for dependency locking LockMode
has changed.
The value is now set via a Property<LockMode>
instead of a direct setter.
This means that the notation to set the value has to be updated for the Kotlin DSL:
dependencyLocking {
lockMode.set(LockMode.STRICT)
}
Users of the Groovy DSL should not be impacted as the notation lockMode = LockMode.STRICT
remains valid.
Java versions in published metadata
If a Java library is published with Gradle Module Metadata, the information which Java version it supports is encoded in the org.gradle.jvm.version
attribute.
By default, this attribute was set to what you configured in java.targetCompatibility
.
If that was not configured, it was set to the current Java version running Gradle.
Changing the version of a particular compile task, e.g. javaCompile.targetCompatibility
had no effect on that attribute, leading to wrong information if the attribute was not adjusted manually.
This is now fixed and the attribute defaults to the setting of the compile task that is associated with the sources from which the published jar is built.
Ivy repositories with custom layouts
Gradle versions from 6.0 to 6.3.x included could generate bad Gradle Module Metadata when publishing on an Ivy repository which had a custom repository layout. Starting from 6.4, Gradle will no longer publish Gradle Module Metadata if it detects that you are using a custom repository layout.
New properties may shadow variables in build scripts
This release introduces some new properties — mainClass
, mainModule
, modularity
— in different places.
Since these are very generic names, there is a chance that you use one of them in your build scripts as variable name.
A new property might then shadow one of your variables in an undesired way, leading to a build failure where the property is accessed instead of the local variable with the same name.
You can fix it by renaming the corresponding variable in the build script.
Affected is configuration code inside the application {}
and java {}
configuration blocks, inside a java execution setup with project.javaexec {}
, and inside various task configurations
(JavaExec
, CreateStartScripts
, JavaCompile
, Test
, Javadoc
).
Updates to bundled Gradle dependencies
-
Kotlin has been updated to Kotlin 1.3.71.
Deprecations
There were no deprecations between Gradle 6.3 and 6.4.
Upgrading from 6.2
Potential breaking changes
Fewer dependencies available in IDEA
Gradle no longer includes the annotation processor classpath as provided dependencies in IDEA.
The dependencies IDEA sees at compile time are the same as what Gradle sees after resolving the compile classpath (configuration named compileClasspath
).
This prevents the leakage of annotation processor dependencies into the project’s code.
Before Gradle introduced incremental annotation processing support, IDEA required all annotation processors to be on the compilation classpath to be able to run annotation processing when compiling in IDEA. This is no longer necessary because Gradle has a separate annotation processor classpath. The dependencies for annotation processors are not added to an IDEA module’s classpath when a Gradle project with annotation processors is imported.
Updates to bundled Gradle dependencies
-
Kotlin has been updated to Kotlin 1.3.70.
-
Groovy has been updated to Groovy 2.5.10.
Updates to default tool integration versions
-
PMD has been updated to PMD 6.21.0.
-
CodeNarc has been updated to CodeNarc 1.5.
Rich console support removed for some 32-bit operating systems
Gradle 6.3 does not support the rich console for 32-bit Unix systems and for old FreeBSD versions (older than FreeBSD 10). Microsoft Windows 32-bit is unaffected.
Gradle will continue building projects on 32-bit systems but will no longer show the rich console.
Deprecations
Using default and archives configurations
Almost every Gradle project has the default and archives configurations which are added by the base plugin. These configurations are no longer used in modern Gradle builds that use variant aware dependency management and the new publishing plugins.
While the configurations will stay in Gradle for backwards compatibility for now, using them to declare dependencies or to resolve dependencies is now deprecated.
Resolving these configurations was never an intended use case and only possible because in earlier Gradle versions every configuration was resolvable. For declaring dependencies, please use the configurations provided by the plugins you use, for example by the Java Library plugin.
Upgrading from 6.1
Potential breaking changes
Compile and runtime classpath now request library variants by default
A classpath in a JVM project now explicitly requests the org.gradle.category=library
attribute.
This leads to clearer error messages if a certain library cannot be used.
For example, when the library does not support the required Java version.
The practical effect is that now all platform dependencies have to be declared as such.
Before, platform dependencies also worked, accidentally, when the platform()
keyword was omitted for local platforms or platforms published with Gradle Module Metadata.
Properties from project root gradle.properties
leaking into buildSrc
and included builds
There was a regression in Gradle 6.2 and Gradle 6.2.1 that caused Gradle properties set in the project root gradle.properties
file to leak into the buildSrc
build and any builds included by the root.
This could cause your build to start failing if the buildSrc
build or an included build suddenly found an unexpected or incompatible value for a property coming from the project root gradle.properties
file.
The regression has been fixed in Gradle 6.2.2.
Deprecations
There were no deprecations between Gradle 6.1 and 6.2.
Upgrading from 6.0 and earlier
Deprecations
Querying a mapped output property of a task before the task has completed
Querying the value of a mapped output property before the task has completed can cause strange build failures because it indicates stale or non-existent outputs may be used by mistake. This behavior is deprecated and will emit a deprecation warning. This will become an error in Gradle 7.0.
The following example demonstrates this problem where the Producer’s output file is parsed before the Producer executes:
class Consumer extends DefaultTask {
@Input
final Property<Integer> threadPoolSize = ...
}
class Producer extends DefaultTask {
@OutputFile
final RegularFileProperty outputFile = ...
}
// threadPoolSize is read from the producer's outputFile
consumer.threadPoolSize = producer.outputFile.map { it.text.toInteger() }
// Emits deprecation warning
println("thread pool size = " + consumer.threadPoolSize.get())
Querying the value of consumer.threadPoolSize
will produce a deprecation warning if done prior to producer
completing, as the output file has not yet been generated.
Discontinued methods
The following methods have been discontinued and should no longer be used. They will be removed in Gradle 7.0.
-
BasePluginConvention.setProject(ProjectInternal)
-
BasePluginConvention.getProject()
-
StartParameter.useEmptySettings()
-
StartParameter.isUseEmptySettings()
Alternative JVM plugins (a.k.a "Software Model")
A set of alternative plugins for Java and Scala development were introduced in Gradle 2.x as an experiment based on the "software model".
These plugins are now deprecated and will eventually be removed.
If you are still using one of these old plugins (java-lang
, scala-lang
, jvm-component
, jvm-resources
, junit-test-suite
) please consult the documentation on Building Java & JVM projects to determine which of the stable JVM plugins are appropriate for your project.
Potential breaking changes
ProjectLayout
is no longer available to worker actions as a service
In Gradle 6.0, the ProjectLayout
service was made available to worker actions via service injection. This service allowed for mutable state to leak into a worker action and introduced a way for dependencies to go undeclared in the worker action.
ProjectLayout
has been removed from the available services. Worker actions that were using ProjectLayout
should switch to injecting the projectDirectory
or buildDirectory
as a parameter instead.
Updates to bundled Gradle dependencies
-
Kotlin has been updated to Kotlin 1.3.61.
Updates to default tool integration versions
-
Checkstyle has been updated to Checkstyle 8.27.
-
PMD has been updated to PMD 6.20.0.
Publishing Spring Boot applications
Starting from Gradle 6.2, Gradle performs a sanity check before uploading, to make sure you don’t upload stale files (files produced by another build).
This introduces a problem with Spring Boot applications which are uploaded using the components.java
component:
Artifact my-application-0.0.1-SNAPSHOT.jar wasn't produced by this build.
This is caused by the fact that the main jar
task is disabled by the Spring Boot application, and the component expects it to be present.
Because the bootJar
task uses the same file as the main jar
task by default, previous releases of Gradle would either:
-
publish a stale
bootJar
artifact -
or fail if the
bootJar
task hasn’t been called previously
A workaround is to tell Gradle what to upload.
If you want to upload the bootJar
, then you need to configure the outgoing configurations to do this:
configurations {
[apiElements, runtimeElements].each {
it.outgoing.artifacts.removeIf { it.buildDependencies.getDependencies(null).contains(jar) }
it.outgoing.artifact(bootJar)
}
}
Alternatively, you might want to re-enable the jar
task, and add the bootJar
with a different classifier.
jar {
enabled = true
}
bootJar {
classifier = 'application'
}
Upgrading your build from Gradle 5.x to 6.0
This chapter provides the information you need to migrate your Gradle 5.x builds to Gradle 6.0. For migrating from Gradle 4.x, complete the 4.x to 5.0 guide first.
We recommend the following steps for all users:
-
Try running
gradle help --scan
and view the deprecations view of the generated build scan.This is so that you can see any deprecation warnings that apply to your build.
Alternatively, you could run
gradle help --warning-mode=all
to see the deprecations in the console, though it may not report as much detailed information. -
Update your plugins.
Some plugins will break with this new version of Gradle, for example because they use internal APIs that have been removed or changed. The previous step will help you identify potential problems by issuing deprecation warnings when a plugin does try to use a deprecated part of the API.
-
Run
gradle wrapper --gradle-version 6.0
to update the project to 6.0. -
Try to run the project and debug any errors using the Troubleshooting Guide.
Upgrading from 5.6 and earlier
Deprecations
Dependencies should no longer be declared using the compile
and runtime
configurations
The usage of the compile
and runtime
configurations in the Java ecosystem plugins has been discouraged since Gradle 3.4.
These configurations are used for compiling and running code from the main
source set.
Other sources sets create similar configurations (e.g. testCompile
and testRuntime
for the test
source set), should not be used either.
The implementation
, api
, compileOnly
and runtimeOnly
configurations should be used to declare dependencies and the compileClasspath
and runtimeClasspath
configurations to resolve dependencies.
See the relationship of these configurations.
Legacy publication system is deprecated and replaced with the *-publish
plugins
The uploadArchives
task and the maven
plugin are deprecated.
Users should migrate to the publishing system of Gradle by using either the maven-publish
or ivy-publish
plugins.
These plugins have been stable since Gradle 4.8.
The publishing system is also the only way to ensure the publication of Gradle Module Metadata.
Problems with tasks emit deprecation warnings
When Gradle detects problems with task definitions (such as incorrectly defined inputs or outputs) it will show the following message on the console:
Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.0/userguide/command_line_interface.html#sec:command_line_warnings
The deprecation warnings show up in build scans for every build, regardless of the command-line switches used.
When the build is executed with --warning-mode all
, the individual warnings will be shown:
> Task :myTask
Property 'inputDirectory' is declared without normalization specified. Properties of cacheable work must declare their normalization via @PathSensitive, @Classpath or @CompileClasspath. Defaulting to PathSensitivity.ABSOLUTE. This behavior is scheduled to be removed in Gradle 7.0.
Property 'outputFile' is not annotated with an input or output annotation. This behavior is scheduled to be removed in Gradle 7.0.
If you own the code of the tasks in question, you can fix them by following the suggestions. You can also use --stacktrace
to see where in the code each warning originates from.
Otherwise, you’ll need to report the problems to the maintainer of the relevant task or plugin.
Old API for incremental tasks, IncrementalTaskInputs
, has been deprecated
In Gradle 5.4 we introduced a new API for implementing incremental tasks: InputChanges. The old API based on IncrementalTaskInputs
has been deprecated.
Forced dependencies
Forcing dependency versions using force = true
on a first-level dependency has been deprecated.
Force has both a semantic and ordering issue which can be avoided by using a strict version constraint.
Search upwards related APIs in StartParameter
have been deprecated
In Gradle 5.0, we removed the --no-search-upward
CLI parameter.
The related APIs in StartParameter
(like isSearchUpwards()
) are now deprecated.
APIs BuildListener.buildStarted
and Gradle.buildStarted
have been deprecated
These methods currently do not work as expected since the callbacks will never be called after the build has started.
The methods are being deprecated to avoid confusion.
Implicit duplicate strategy for Copy
or archive tasks has been deprecated
Archive tasks Tar
and Zip
by default allow multiple entries for the same path to exist in the created archive.
This can cause "grossly invalid zip files" that can trigger zip bomb detection.
To prevent this from happening accidentally, encountering duplicates while creating an archive now produces a deprecation message and will fail the build starting with Gradle 7.0.
Copy
tasks also happily copy multiple sources with the same relative path to the destination directory.
This behavior has also been deprecated.
If you want to allow duplicates, you can specify that explicitly:
task archive(type: Zip) {
duplicatesStrategy = DuplicatesStrategy.INCLUDE // allow duplicates
...
}
Executing Gradle without a settings file has been deprecated
A Gradle build is defined by a settings.gradle[.kts]
file in the current or parent directory.
Without a settings file, a Gradle build is undefined and will emit a deprecation warning.
In Gradle 7.0, Gradle will only allow you to invoke the init
task or diagnostic command line flags, such as --version
, with undefined builds.
Calling Project.afterEvaluate
on an evaluated project has been deprecated
Once a project is evaluated, Gradle ignores all configuration passed to Project#afterEvaluate
and emits a deprecation warning. This scenario will become an error in Gradle 7.0.
Deprecated plugins
The following bundled plugins were never announced and will be removed in the next major release of Gradle:
-
org.gradle.coffeescript-base
-
org.gradle.envjs
-
org.gradle.javascript-base
-
org.gradle.jshint
-
org.gradle.rhino
Some of these plugins may have replacements on the Plugin Portal.
Potential breaking changes
Android Gradle Plugin 3.3 and earlier is no longer supported
Gradle 6.0 supports Android Gradle Plugin versions 3.4 and later.
Build scan plugin 2.x is no longer supported
For Gradle 6, usage of the build scan plugin must be replaced with the Gradle Enterprise plugin. This also requires changing how the plugin is applied. Please see https://gradle.com/help/gradle-6-build-scan-plugin for more information.
Updates to bundled Gradle dependencies
-
Groovy has been updated to Groovy 2.5.8.
-
Kotlin has been updated to Kotlin 1.3.50.
-
Ant has been updated to Ant 1.10.7.
Updates to default integration versions
-
Checkstyle has been updated to Checkstyle 8.24.
-
CodeNarc has been updated to CodeNarc 1.4.
-
PMD has been updated to PMD 6.17.0.
-
JaCoCo has been updated to 0.8.5. Contributed by Evgeny Mandrikov
Changes to build and task names in composite builds
Previously, Gradle used the name of the root project as the build name for an included build. Now, the name of the build’s root directory is used and the root project name is not considered if different. A different name for the build can be specified if the build is being included via a settings file.
includeBuild("some-other-build") {
name = "another-name"
}
The previous behavior was problematic as it caused different names to be used at different times during the build.
buildSrc is now reserved as a project and subproject build name
Previously, Gradle did not prevent using the name “buildSrc” for a subproject of a multi-project build or as the name of an included build. Now, this is not allowed. The name “buildSrc” is now reserved for the conventional buildSrc project that builds extra build logic.
Typical use of buildSrc is unaffected by this change.
You will only be affected if your settings file specifies include("buildSrc")
or includeBuild("buildSrc")
.
Scala Zinc compiler
The Zinc compiler has been upgraded to version 1.3.0. Gradle no longer supports building for Scala 2.9.
The minimum Zinc compiler supported by Gradle is 1.2.0 and the maximum tested version is 1.3.0.
To make it easier to select the version of the Zinc compiler, you can now configure a zincVersion
property:
scala {
zincVersion = "1.2.1"
}
Please remove any explicit dependencies you’ve added to the zinc
configuration and use this property instead.
If you try to use the com.typesafe.zinc:zinc
dependency, Gradle will switch to the new Zinc implementation.
Local build cache is always a directory cache
In the past, it was possible to use any build cache implementation as the local
cache. This is no longer allowed as the local cache must always be a DirectoryBuildCache
.
Calls to BuildCacheConfiguration.local(Class)
with anything other than DirectoryBuildCache
as the type will fail the build. Calling these methods with the DirectoryBuildCache
type will produce a deprecation warning.
Use getLocal()
and local(Action)
instead.
Failing to pack or unpack cached results will now fail the build
In the past, when Gradle encountered a problem while packing the results of a cached task, Gradle would ignore the problem and continue running the build.
When encountering a corrupt cached artifact, Gradle would remove whatever was already unpacked and re-execute the task to make sure the build had a chance to succeed.
While this behavior was intended to make a build successful, this had the adverse effect of hiding problems and led to reduced cache performance.
In Gradle 6.0, both pack and unpack errors will cause the build to fail, so that these problems will be surfaced more easily.
buildSrc projects automatically use build cache configuration
Previously, in order to use the build cache for the buildSrc build you needed to duplicate your build cache config in the buildSrc build. Now, it automatically uses the build cache configuration defined by the top level settings script.
Gradle Module Metadata is always published
Officially introduced in Gradle 5.3, Gradle Module Metadata was created to solve many of the problems that have plagued dependency management for years, in particular, but not exclusively, in the Java ecosystem.
With Gradle 6.0, Gradle Module Metadata is enabled by default.
This means, if you are publishing libraries with Gradle and using the maven-publish or ivy-publish plugin, the Gradle Module Metadata file is always published in addition to traditional metadata.
The traditional metadata file will contain a marker so that Gradle knows that there is additional metadata to consume.
Gradle Module Metadata has stricter validation
The following rules are verified when publishing Gradle Module Metadata:
-
Variant names must be unique,
-
Each variant must have at least one attribute,
-
Two variants cannot have the exact same attributes and capabilities,
-
If there are dependencies, at least one, across all variants, must carry version information.
These are documented in the specification as well.
Maven or Ivy repositories are no longer queried for artifacts without metadata by default
If Gradle fails to locate the metadata file (.pom
or ivy.xml
) of a module in a repository defined in the repositories { }
section, it now assumes that the module does not exist in that repository.
For dynamic versions, the maven-metadata.xml
for the corresponding module needs to be present in a Maven repository.
Previously, Gradle would also look for a default artifact (.jar
).
This behavior often caused a large number of unnecessary requests when using multiple repositories that slowed builds down.
You can opt into the old behavior for selected repositories by adding the artifact()
metadata source.
Changing the pom packaging
property no longer changes the artifact extension
Previously, if the pom packaging was not jar, ejb, bundle or maven-plugin, the extension of the main artifact published to a Maven repository was changed during publishing to match the pom packaging.
This behavior led to broken Gradle Module Metadata and was difficult to understand due to handling of different packaging types.
Build authors can change the artifact name when the artifact is created to obtain the same result as before — e.g. by setting jar.archiveExtension.set(pomPackaging)
explicitly.
An ivy.xml
published for Java libraries contains more information
A number of fixes were made to produce more correct ivy.xml
metadata in the ivy-publish
plugin.
As a consequence, the internal structure of the ivy.xml
file has changed. The runtime
configuration now contains more information, which corresponds to the runtimeElements variant of a Java library. The default
configuration should yield the same result as before.
In general, users are advised to migrate from ivy.xml
to the new Gradle Module Metadata format.
Classes from buildSrc
are no longer visible to settings scripts
Previously, the buildSrc project was built before applying the project’s settings script and its classes were visible within the script. Now, buildSrc is built after the settings script and its classes are not visible to it. The buildSrc classes remain visible to project build scripts and script plugins.
Custom logic can be used from a settings script by declaring external dependencies.
The pluginManagement
block in settings scripts is now isolated
Previously, any pluginManagement {}
blocks inside a settings script were executed during the normal execution of the script.
Now, they are executed earlier in a similar manner to buildscript {}
or plugins {}
. This means that code inside such a block cannot reference anything declared elsewhere in the script.
This change has been made so that pluginManagement
configuration can also be applied when resolving plugins for the settings script itself.
Plugins and classes loaded in settings scripts are visible to project scripts and buildSrc
Previously, any classes added to the a settings script by using buildscript {}
were not visible outside of the script.
Now, they are visible to all of the project build scripts.
They are also visible to the buildSrc
build script and its settings script.
This change has been made so that plugins applied to the settings script can contribute logic to the entire build.
Plugin validation changes
-
The
validateTaskProperties
task is now deprecated, usevalidatePlugins
instead. The new name better reflects the fact that it also validates artifact transform parameters and other non-property definitions. -
The
ValidateTaskProperties
type is replaced byValidatePlugins
. -
The
setClasses()
method is now removed. UsegetClasses().setFrom()
instead. -
The
setClasspath()
method is also removed. usegetClasspath().setFrom()
instead. -
The failOnWarning option is now enabled by default.
-
The following task validation errors now fail the build at runtime and are promoted to errors for ValidatePlugins:
-
A task property is annotated with a property annotation not allowed for tasks, like
@InputArtifact
.
-
Using the embedded-kotlin
plugin now requires a repository
Just like when using the kotlin-dsl
plugin, it is now required to declare a repository where Kotlin dependencies can be found if you apply the embedded-kotlin
plugin.
plugins {
`embedded-kotlin`
}
repositories {
mavenCentral()
}
Kotlin DSL IDE support now requires Kotlin IntelliJ Plugin >= 1.3.50
With Kotlin IntelliJ plugin versions prior to 1.3.50, Kotlin DSL scripts will be wrongly highlighted when the Gradle JVM is set to a version different from the one in Project SDK. Simply upgrade your IDE plugin to a version >= 1.3.50 to restore the correct Kotlin DSL script highlighting behavior.
Kotlin DSL script base types no longer extend Project
, Settings
or Gradle
In previous versions, Kotlin DSL scripts were compiled to classes that implemented one of the three core Gradle configuration interfaces in order to implicitly expose their APIs to scripts. org.gradle.api.Project
for project scripts, org.gradle.api.initialization.Settings
for settings scripts and org.gradle.api.invocation.Gradle
for init scripts.
Having the script instance implement the core Gradle interface of the model object it was supposed to configure was convenient because it made the model object API immediately available to the body of the script but it was also a lie that could cause all sorts of trouble whenever the script itself was used in place of the model object, a project script was not a proper Project
instance just because it implemented the core Project
interface and the same was true for settings and init scripts.
In 6.0 all Kotlin DSL scripts are compiled to classes that implement the newly introduced org.gradle.kotlin.dsl.KotlinScript
interface and the corresponding model objects are now available as implicit receivers in the body of the scripts. In other words, a project script behaves as if the body of the script is enclosed within a with(project) { … }
block, a settings script as if the body of the script is enclosed within a with(settings) { … }
block and an init script as if the body of the script is enclosed within a with(gradle) { … }
block. This implies the corresponding model object is also available as a property in the body of the script, the project
property for project scripts, the settings
property for settings scripts and the gradle
property for init scripts.
As part of the change, the SettingsScriptApi
interface is no longer implemented by settings scripts and the InitScriptApi
interface is no longer implemented by init scripts. They should be replaced with the corresponding model object interfaces, Settings
and Gradle
.
Javadoc and Groovydoc don’t include timestamps by default
Timestamps in the generated documentation have very limited practical use, however they make it impossible to have repeatable documentation builds.
Therefore, the Javadoc
and Groovydoc
tasks are now configured to not include timestamps by default any more.
User provided 'config_loc' properties are ignored by Checkstyle
Gradle always uses configDirectory
as the value for 'config_loc' when running Checkstyle.
New Tooling API progress event
In Gradle 6.0, we introduced a new progress event (org.gradle.tooling.events.test.TestOutputEvent) to expose the output of test execution. This new event breaks the convention of having a StartEvent
-FinishEvent
pair to express progress. TaskOutputEvent
is a simple ProgressEvent
.
Changes to the task container behavior
The following deprecated methods on the task container now result in errors:
-
TaskContainer.add()
-
TaskContainer.addAll()
-
TaskContainer.remove()
-
TaskContainer.removeAll()
-
TaskContainer.retainAll()
-
TaskContainer.clear()
-
TaskContainer.iterator().remove()
Additionally, the following deprecated functionality now results in an error:
-
Replacing a task that has already been realized.
-
Replacing a registered (unrealized) task with an incompatible type. A compatible type is the same type or a sub-type of the registered type.
-
Replacing a task that has never been registered.
Methods on DefaultTask
and ProjectLayout
replaced with ObjectFactory
Use ObjectFactory.fileProperty()
instead of the following methods that are now removed:
-
DefaultTask.newInputFile()
-
DefaultTask.newOutputFile()
-
ProjectLayout.fileProperty()
Use ObjectFactory.directoryProperty()
instead of the following methods that are now removed:
-
DefaultTask.newInputDirectory()
-
DefaultTask.newOutputDirectory()
-
ProjectLayout.directoryProperty()
Annotation @Nullable
has been removed
The org.gradle.api.Nullable
annotation type has been removed. Use javax.annotation.Nullable
from JSR-305 instead.
The FindBugs plugin has been removed
The deprecated FindBugs plugin has been removed. As an alternative, you can use the SpotBugs plugin from the Gradle Plugin Portal.
The JDepend plugin has been removed
The deprecated JDepend plugin has been removed. There are a number of community-provided plugins for code and architecture analysis available on the Gradle Plugin Portal.
The OSGI plugin has been removed
The deprecated OSGI plugin has been removed. There are a number of community-provided OSGI plugins available on the Gradle Plugin Portal.
The announce and build-announcements plugins have been removed
The deprecated announce and build-announcements plugins have been removed. There are a number of community-provided plugins for sending out notifications available on the Gradle Plugin Portal.
The Compare Gradle Builds plugin has been removed
The deprecated Compare Gradle Builds plugin has been removed. Please use build scans for build analysis and comparison.
The Play plugins have been removed
The deprecated Play plugin has been removed. An external replacement, the Play Framework plugin, is available from the plugin portal.
Method AbstractCompile.compile()
method has been removed
The abstract method compile()
is no longer declared by AbstractCompile
.
Tasks extending AbstractCompile
can implement their own @TaskAction
method with the name of their choosing.
They are also free to add a method annotated with @TaskAction
using an InputChanges
parameter without having to implement a parameter-less one as well.
Other Deprecated Behaviors and APIs
-
The
org.gradle.util.internal.GUtil.savePropertiesNoDateComment
has been removed. There is no public replacement for this internal method. -
The deprecated class
org.gradle.api.tasks.compile.CompilerArgumentProvider
has been removed. Use org.gradle.process.CommandLineArgumentProvider instead. -
The deprecated class
org.gradle.api.ConventionProperty
has been removed. Use Providers instead of convention properties. -
The deprecated class
org.gradle.reporting.DurationFormatter
has been removed. -
The bridge method
org.gradle.api.tasks.TaskInputs.property(String name, @Nullable Object value)
returningTaskInputs
has been removed. A plugin using the method must be compiled with Gradle 4.3 to work on Gradle 6.0. -
The following setters have been removed from
JacocoReportBase
:-
executionData - use
getExecutionData().setFrom()
instead. -
sourceDirectories - use
getSourceDirectories().setFrom()
instead. -
classDirectories - use
getClassDirectories().setFrom()
instead. -
additionalClassDirs - use
getAdditionalClassDirs().setFrom()
instead. -
additionalSourceDirs - use
getAdditionalSourceDirs().setFrom()
instead.
-
-
The
append
property onJacocoTaskExtension
has been removed.append
is now always configured to be true for the Jacoco agent. -
The
configureDefaultOutputPathForJacocoMerge
method onJacocoPlugin
has been removed. The method was never meant to be public. -
File paths in deployment descriptor file name for the ear plugin are not allowed any more. Use a simple name, like
application.xml
, instead. -
The
org.gradle.testfixtures.ProjectBuilder
constructor has been removed. Please useProjectBuilder.builder()
instead. -
When incremental Groovy compilation is enabled, a wrong configuration of the source roots or enabling Java annotation for Groovy now fails the build. Disable incremental Groovy compilation when you want to compile in those cases.
-
ComponentSelectionRule
no longer can inject the metadata or ivy descriptor. Use the methods on theComponentSelection
parameter instead. -
Declaring an incremental task without declaring outputs is now an error. Declare file outputs or use TaskOutputs.upToDateWhen() instead.
-
The
getEffectiveAnnotationProcessorPath()
method is removed from theJavaCompile
andScalaCompile
tasks. -
Changing the value of a task property with type
Property<T>
after the task has started execution now results in an error. -
The
isLegacyLayout()
method is removed fromSourceSetOutput
. -
The map returned by
TaskInputs.getProperties()
is now unmodifiable. Trying to modify it will result in anUnsupportedOperationException
being thrown. -
There are slight changes in the incubating capabilities resolution API, which has been introduced in 5.6, to also allow variant selection based on variant name
Upgrading from 5.5 and earlier
Deprecations
Changing the contents of ConfigurableFileCollection
task properties after task starts execution
When a task property has type ConfigurableFileCollection
, then the file collection referenced by the property will ignore changes made to the contents of the collection once the task
starts execution. This has two benefits. Firstly, this prevents accidental changes to the property value during task execution which can cause Gradle up-to-date checks and build cache lookup
using different values to those used by the task action. Secondly, this improves performance as Gradle can calculate the value once and cache the result.
This will become an error in Gradle 6.0.
Creating SignOperation
instances
Creating SignOperation
instances directly is now deprecated. Instead, the methods of SigningExtension
should be used to create these instances.
This will become an error in Gradle 6.0.
Declaring an incremental task without outputs
Declaring an incremental task without declaring outputs is now deprecated. Declare file outputs or use TaskOutputs.upToDateWhen() instead.
This will become an error in Gradle 6.0.
Method WorkerExecutor.submit()
is deprecated
The WorkerExecutor.submit()
method is now deprecated.
The new noIsolation()
, classLoaderIsolation()
and processIsolation()
methods should now be used to submit work.
See the section on the Worker API for more information on using these methods.
WorkerExecutor.submit()
will be removed in Gradle 8.0.
Potential breaking changes
Task dependencies are honored for task @Input
properties whose value is a Property
Previously, task dependencies would be ignored for task @Input
properties of type Property<T>
. These are now honored, so that it is possible to attach a task output property to a task @Input
property.
This may introduce unexpected cycles in the task dependency graph, where the value of an output property is mapped to produce a value for an input property.
Declaring task dependencies using a file Provider
that does not represent a task output
Previously, it was possible to pass Task.dependsOn()
a Provider<File>
, Provider<RegularFile>
or Provider<Directory>
instance that did not represent a task output. These providers would be silently ignored.
This is now an error because Gradle does not know how to build files that are not task outputs.
Note that it is still possible to pass Task.dependsOn()
a Provider
that returns a file and that represents a task output, for example myTask.dependsOn(jar.archiveFile)
or myTask.dependsOn(taskProvider.flatMap { it.outputDirectory })
, when the Provider
is an annotated @OutputFile
or @OutputDirectory
property of a task.
Setting Property
value to null
uses the property convention
Previously, calling Property.set(null)
would always reset the value of the property to 'not defined'. Now, the convention that is associated with the property using the convention()
method
will be used to determine the value of the property.
Enhanced validation of names for publishing.publications
and publishing.repositories
The repository and publication names are used to construct task names for publishing. It was possible to supply a name that would result in an invalid task name. Names for publications and repositories are now restricted to [A-Za-z0-9_\\-.]+
.
Restricted Worker API classloader and process classpath
Gradle now prevents internal dependencies (like Guava) from leaking into the classpath used by Worker API actions. This fixes an issue where a worker needs to use a dependency that is also used by Gradle internally.
In previous releases, it was possible to rely on these leaked classes. Plugins relying on this behavior will now fail. To fix the plugin, the worker should explicitly include all required dependencies in its classpath.
Default PMD version upgraded to 6.15.0
The PMD plugin has been upgraded to use PMD version 6.15.0 instead of 6.8.0 by default.
Contributed by wreulicke
Configuration copies have unique names
Previously, all copies of a configuration always had the name <OriginConfigurationName>Copy
. Now when creating multiple copies, each will have a unique name by adding an index starting from the second copy. (e.g. CompileOnlyCopy2
)
Changed classpath filtering for Eclipse
Gradle 5.6 no longer supplies custom classpath attributes in the Eclipse model. Instead, it provides the attributes for Eclipse test sources. This change requires Buildship version 3.1.1 or later.
Embedded Kotlin upgraded to 1.3.41
Gradle Kotlin DSL scripts and Gradle Plugins authored using the kotlin-dsl
plugin are now compiled using Kotlin 1.3.41.
The minimum supported Kotlin Gradle Plugin version is now 1.2.31. Previously it was 1.2.21.
Automatic capability conflict resolution
Previous versions of Gradle would automatically select, in case of capability conflicts, the module which has the highest capability version. Starting from 5.6, this is an opt-in behavior that can be activated using:
configurations.all {
resolutionStrategy.capabilitiesResolution.all { selectHighestVersion() }
}
See the capabilities section of the documentation for more options.
File removal operations don’t follow symlinked directories
When Gradle has to remove the output files of a task for various reasons, it will not follow symlinked directories. The symlink itself will be deleted, but the contents of the linked directory will stay intact.
Disabled debug argument parsing in JavaExec
Gradle 5.6 introduced a new DSL element (JavaForkOptions.debugOptions(Action<JavaDebugOptions>)
) to configure debug properties for forked Java processes. Due to this change, Gradle no longer parses debug-related JVM arguments. Consequently, JavaForkOptions.getDebu()
no longer returns true
if the -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005
or the -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005
argument is specified to the process.
Scala 2.9 and Zinc compiler
Gradle no longer supports building applications using Scala 2.9.
Upgrading from 5.4 and earlier
Deprecations
Play
The built-in Play plugin has been deprecated and will be replaced by a new Play Framework plugin available from the plugin portal.
Build Comparison
The build comparison plugin has been deprecated and will be removed in the next major version of Gradle.
Build scans show much deeper insights into your build and you can use Gradle Enterprise to directly compare two build’s build-scans.
Potential breaking changes
User supplied Eclipse project names may be ignored on conflict
Project names configured via EclipseProject.setName(…)
were honored by Gradle and Buildship in all cases, even
when the names caused conflicts and import/synchronization errors.
Gradle can now deduplicate these names if they conflict with other project names in an Eclipse workspace. This may lead to different Eclipse project names for projects with user-specified names.
The upcoming 3.1.1 version of Buildship is required to take advantage of this behavior.
Contributed by Christian Fränkel
Default JaCoCo version upgraded to 0.8.4
The JaCoCo plugin has been upgraded to use JaCoCo version 0.8.4 instead of 0.8.3 by default.
Contributed by Evgeny Mandrikov
Embedded Ant version upgraded to 1.9.14
The version of Ant distributed with Gradle has been upgraded to 1.9.14 from 1.9.13.
Type DependencyHandler
now statically exposes ExtensionAware
This affects Kotlin DSL build scripts that make use of ExtensionAware
extension members such as the extra
properties accessor inside the dependencies {}
block. The receiver for those members will no longer be the enclosing Project
instance but the dependencies
object itself, the innermost ExtensionAware
conforming receiver. In order to address Project
extra properties inside dependencies {}
the receiver must be explicitly qualified i.e. project.extra
instead of just extra
. Affected extensions also include the<T>()
and configure<T>(T.() → Unit)
.
Improved processing of dependency excludes
Previous versions of Gradle could, in some complex dependency graphs, have a wrong result or a randomized dependency order when lots of excludes were present. To mitigate this, the algorithm that computes exclusions has been rewritten. In some rare cases this may cause some differences in resolution, due to the correctness changes.
Improved classpath separation for worker processes
The system classpath for worker daemons started by the Worker API when using PROCESS
isolation has been reduced to a minimum set of Gradle infrastructure. User code is still segregated into a separate classloader to isolate it from the Gradle runtime. This should be a transparent change for tasks using the worker API, but previous versions of Gradle mixed user code and Gradle internals in the worker process. Worker actions that rely on things like the java.class.path
system property may be affected, since java.class.path
now represents only the classpath of the Gradle internals.
Upgrading from 5.3 and earlier
Deprecations
Using custom local build cache implementations
Using a custom build cache implementation for the local build cache is now deprecated.
The only allowed type will be DirectoryBuildCache
going forward.
There is no change in the support for using custom build cache implementations as the remote build cache.
Potential breaking changes
Use HTTPS when configuring Google Hosted Libraries via googleApis()
The Google Hosted Libraries URL accessible via JavaScriptRepositoriesExtension#GOOGLE_APIS_REPO_URL
was changed to use the HTTPS protocol.
The change also affect the Ivy repository configured via googleApis()
.
Upgrading from 5.2 and earlier
Potential breaking changes
Bug fixes in platform resolution
There was a bug from Gradle 5.0 to 5.2.1 (included) where enforced platforms would potentially include dependencies instead of constraints.
This would happen whenever a POM file defined both dependencies and "constraints" (via <dependencyManagement>
) and that you used enforcedPlatform
.
Gradle 5.3 fixes this bug, meaning that you might have differences in the resolution result if you relied on this broken behavior.
Similarly, Gradle 5.3 will no longer try to download jars for platform
and enforcedPlatform
dependencies (as they should only bring in constraints).
Automatic target JVM version
If you apply any of the Java plugins, Gradle will now do its best to select dependencies which match the target compatibility of the module being compiled. What it means, in practice, is that if you have module A built for Java 8, and module B built for Java 8, then there’s no change. However if B is built for Java 9+, then it’s not binary compatible anymore, and Gradle would complain with an error message like the following:
Unable to find a matching variant of project :producer:
- Variant 'apiElements' capability test:producer:unspecified:
- Provides org.gradle.dependency.bundling 'external'
- Required org.gradle.jvm.version '8' and found incompatible value '9'.
- Required org.gradle.usage 'java-api' and found value 'java-api-jars'.
- Variant 'runtimeElements' capability test:producer:unspecified:
- Provides org.gradle.dependency.bundling 'external'
- Required org.gradle.jvm.version '8' and found incompatible value '9'.
- Required org.gradle.usage 'java-api' and found value 'java-runtime-jars'.
In general, this is a sign that your project is misconfigured and that your dependencies are not compatible. However, there are cases where you still may want to do this, for example when only a subset of classes of your module actually need the Java 9 dependencies, and are not intended to be used on earlier releases. Java in general doesn’t encourage you to do this (you should split your module instead), but if you face this problem, you can workaround by disabling this new behavior on the consumer side:
java {
disableAutoTargetJvm()
}
Bug fix in Maven / Ivy interoperability with dependency substitution
If you have a Maven dependency pointing to an Ivy dependency where the default
configuration dependencies do not match the compile
+ runtime
+ master
ones
and that Ivy dependency was substituted (using a resolutionStrategy.force
, resolutionStrategy.eachDependency
or resolutionStrategy.dependencySubstitution
)
then this fix will impact you.
The legacy behaviour of Gradle, prior to 5.0, was still in place instead of being replaced by the changes introduced by improved pom support.
Delete operations correctly handle symbolic links on Windows
Gradle no longer ignores the followSymlink
option on Windows for the clean
task, all Delete
tasks, and project.delete {}
operations in the presence of junction points and symbolic links.
Fix in publication of additional artifacts
In previous Gradle versions, additional artifacts registered at the project level were not published by maven-publish
or ivy-publish
unless they were also added as artifacts in the publication configuration.
With Gradle 5.3, these artifacts are now properly accounted for and published.
This means that artifacts that are registered both on the project and the publication, Ivy or Maven, will cause publication to fail since it will create duplicate entries. The fix is to remove these artifacts from the publication configuration.
Upgrading from 5.0 and earlier
Deprecations
Follow the API links to learn how to deal with these deprecations (if no extra information is provided here):
-
Setters for
classes
andclasspath
onorg.gradle.plugin.devel.tasks.ValidateTaskProperties
(removed) -
There should not be setters for lazy properties like
ConfigurableFileCollection
. UsesetFrom
instead. For example,
validateTaskProperties.getClasses().setFrom(fileCollection) validateTaskProperties.getClasspath().setFrom(fileCollection)
Potential breaking changes
The following changes were not previously deprecated:
Signing API changes
Input and output files of Sign
tasks are now tracked via Signature.getToSign()
and Signature.getFile()
, respectively.
Collection properties default to empty collection
In Gradle 5.0, the collection property instances created using ObjectFactory
would have no value defined, requiring plugin authors to explicitly set an initial value. This proved to be awkward and error prone so ObjectFactory
now returns instances with an empty collection as their initial value.
Worker API: working directory of a worker can no longer be set
Since JDK 11 no longer supports changing the working directory of a running process, setting the working directory of a worker via its fork options is now prohibited. All workers now use the same working directory to enable reuse. Please pass files and directories as arguments instead. See examples in the Worker API documentation.
Changes to native linking tasks
To expand our idiomatic Provider API practices, the install name property from org.gradle.nativeplatform.tasks.LinkSharedLibrary
is affected by this change.
-
getInstallName()
was changed to return aProperty
. -
setInstallName(String)
was removed. UseProperty.set()
instead.
Passing arguments to Windows Resource Compiler
To expand our idiomatic Provider API practices, the WindowsResourceCompile
task has been converted to use the Provider API.
Passing additional compiler arguments now follow the same pattern as the CppCompile
and other tasks.
Copied configuration no longer shares a list of beforeResolve
actions with original
The list of beforeResolve
actions are no longer shared between a copied configuration and the original.
Instead, a copied configuration receives a copy of the beforeResolve
actions at the time the copy is made.
Any beforeResolve
actions added after copying (to either configuration) will not be shared between the original and the copy.
This may break plugins that relied on the previous behaviour.
Changes to incubating POM customization types
-
The type of
MavenPomDeveloper.properties
has changed fromProperty<Map<String, String>>
toMapProperty<String, String>
. -
The type of
MavenPomContributor.properties
has changed fromProperty<Map<String, String>>
toMapProperty<String, String>
.
Changes to specifying operating system for native projects
The incubating operatingSystems
property on native components has been replaced with the targetMachines property.
Changes for archive tasks (Zip
, Jar
, War
, Ear
, Tar
)
AbstractArchiveTask
The AbstractArchiveTask
has several new properties using the Provider API.
Plugins that extend these types and override methods from the base class may no longer behave the same way.
Internally, AbstractArchiveTask
prefers the new properties and methods like getArchiveName()
are façades over the new properties.
If your plugin/build only uses these types (and does not extend them), nothing has changed.
Upgrading your build from Gradle 4.x to 5.0
This chapter provides the information you need to migrate your older Gradle 4.x builds to Gradle 5.0. In most cases, you will need to apply the changes from all versions that come after the one you’re upgrading from. For example, if you’re upgrading from Gradle 4.3 to 5.0, you will also need to apply the changes since 4.4, 4.5, etc up to 5.0.
Tip
|
If you are using Gradle for Android, you need to move to version 3.3 or higher of both the Android Gradle Plugin and Android Studio. |
For all users
-
If you are not already on the latest 4.10.x release, read the sections below for help upgrading your project to the latest 4.10.x release. We recommend upgrading to the latest 4.10.x release to get the most useful warnings and deprecations information before moving to 5.0. Avoid upgrading Gradle and migrating to Kotlin DSL at the same time in order to ease troubleshooting in case of potential issues.
-
Try running
gradle help --scan
and view the deprecations view of the generated build scan. If there are no warnings, the Deprecations tab will not appear.This is so that you can see any deprecation warnings that apply to your build. Gradle 5.x will generate (potentially less obvious) errors if you try to upgrade directly to it.
Alternatively, you could run
gradle help --warning-mode=all
to see the deprecations in the console, though it may not report as much detailed information. -
Update your plugins.
Some plugins will break with this new version of Gradle, for example because they use internal APIs that have been removed or changed. The previous step will help you identify potential problems by issuing deprecation warnings when a plugin does try to use a deprecated part of the API.
In particular, you will need to use at least a 2.x version of the Shadow Plugin.
-
Run
gradle wrapper --gradle-version 5.0
to update the project to 5.0 -
Move to Java 8 or higher if you haven’t already. Whereas Gradle 4.x requires Java 7, Gradle 5 requires Java 8 to run.
-
Read the Upgrading from 4.10 section and make any necessary changes.
-
Try to run the project and debug any errors using the Troubleshooting Guide.
In addition, Gradle has added several significant new and improved features that you should consider using in your builds:
-
Maven Publish and Ivy Publish Plugins that now support digital signatures with the Signing Plugin.
-
Use native BOM import in your builds.
-
The Worker API for enabling units of work to run in parallel.
-
A new API for creating and configuring tasks lazily that can significantly improve your build’s configuration time.
Other notable changes to be aware of that may break your build include:
-
Separation of compile and runtime dependencies when consuming POMs
-
A change that means you should configure existing
wrapper
andinit
tasks rather than defining your own. -
The honoring of implicit wildcards in Maven POM exclusions, which may result in dependencies being excluded that weren’t before.
-
A change to the way you add Java annotation processors to a project.
-
The default memory settings for the command-line client, the Gradle daemon, and all workers including compilers and test executors, have been greatly reduced.
-
The default versions of several code quality plugins have been updated.
-
Several library versions used by Gradle have been upgraded.
Upgrading from 4.10 and earlier
If you are not already on version 4.10, skip down to the section that applies to your current Gradle version and work your way up until you reach here. Then, apply these changes when moving from Gradle 4.10 to 5.0.
Other changes
-
The
enableFeaturePreview('IMPROVED_POM_SUPPORT')
andenableFeaturePreview('STABLE_PUBLISHING')
flags are no longer necessary. These features are now enabled by default. -
Gradle now bundles JAXB for Java 9 and above. You can remove the
--add-modules java.xml.bind
option fromorg.gradle.jvmargs
, if set.
Potential breaking changes
The changes in this section have the potential to break your build, but the vast majority have been deprecated for quite some time and few builds will be affected by a large number of them. We strongly recommend upgrading to Gradle 4.10 first to get a report on what deprecations affect your build.
The following breaking changes are not from deprecations, but the result of changes in behavior:
-
Separation of compile and runtime dependencies when consuming POMs
-
The evaluation of the
publishing {}
block is no longer deferred until needed but behaves like any other block. Please useafterEvaluate {}
if you need to defer evaluation. -
The
Javadoc
andGroovydoc
tasks now delete the destination dir for the documentation before executing. This has been added to remove stale output files from the last task execution. -
The Java Library Distribution Plugin is now based on the Java Library Plugin instead of the Java Plugin.
While it applies the Java Plugin, it behaves slightly different (e.g. it adds the
api
configuration). Thus, make sure to check whether your build behaves as expected after upgrading. -
The
html
property onCheckstyleReport
andFindBugsReport
now returns aCustomizableHtmlReport
instance that is easier to configure from statically typed languages like Java and Kotlin. -
The Configuration Avoidance API has been updated to prevent the creation and configuration of tasks that are never used.
-
The default memory settings for the command-line client, the Gradle daemon, and all workers including compilers and test executors, have been greatly reduced.
-
The default versions of several code quality plugins have been updated.
-
Several library versions used by Gradle have been upgraded.
The following breaking changes will appear as deprecation warnings with Gradle 4.10:
- General
-
-
<<
for task definitions no longer works. In other words, you can not use the syntaxtask myTask << { … }
.Use the Task.doLast() method instead, like this:
task myTask { doLast { ... } }
-
You can no longer use any of the following characters in domain object names, such as project and task names: <space>
/ \ : < > " ? * |
. You should also not use.
as a leading or trailing character.
-
- Running Gradle & build environment
-
-
As mentioned before, Gradle can no longer be run on Java 7. However, you can still use forked compilation and testing to build and test software for Java 6 and above.
-
The
-Dtest.single
command-line option has been removed — use test filtering instead. -
The
-Dtest.debug
command-line option has been removed — use the--debug-jvm
option instead. -
The
-u
/--no-search-upward
command-line option has been removed — make sure all your builds have a settings.gradle file. -
The
--recompile-scripts
command-line option has been removed. -
You can no longer have a Gradle build nested in a subdirectory of another Gradle build unless the nested build has a settings.gradle file.
-
The
DirectoryBuildCache.setTargetSizeInMB(long)
method has been removed — use DirectoryBuildCache.removeUnusedEntriesAfterDays instead. -
The
org.gradle.readLoggingConfigFile
system property no longer does anything — update affected tests to work with yourjava.util.logging
settings.
-
- Working with files
-
-
You can no longer cast
FileCollection
objects to other types using theas
keyword or theasType()
method. -
You can no longer pass
null
as the configuration action of CopySpec.from(Object, Action). -
For better compatibility with the Kotlin DSL, CopySpec.duplicatesStrategy is no longer nullable. The property setter no longer accepts
null
as a way to reset the property back to its default value. UseDuplicatesStrategy.INHERIT
instead. -
The
FileCollection.stopExecutionIfEmpty()
method has been removed — use the @SkipWhenEmpty annotation onFileCollection
task properties instead. -
The
FileCollection.add()
method has been removed — use Project.files() and Project.fileTree() to create configurable file collections/file trees and add to them via ConfigurableFileCollection.from(). -
SimpleFileCollection
has been removed — use Project.files(Object…) instead. -
Don’t have your own classes extend
AbstractFileCollection
— use the Project.files() method instead. This problem may exhibit as a missinggetBuildDependencies()
method.
-
- Java builds
-
-
The
CompileOptions.bootClasspath
property has been removed — use CompileOptions.bootstrapClasspath instead. -
You can no longer use
-source-path
as a generic compiler argument — use CompileOptions.sourcepath instead. -
You can no longer use
-processorpath
as a generic compiler argument — use CompileOptions.annotationProcessorPath instead. -
Gradle will no longer automatically apply annotation processors that are on the compile classpath — use CompileOptions.annotationProcessorPath instead.
-
The
testClassesDir
property has been removed from the Test task — use testClassesDirs instead. -
The
classesDir
property has been removed from both the JDepend task and SourceSetOutput. Use the JDepend.classesDirs and SourceSetOutput.classesDirs properties instead. -
The
JavaLibrary(PublishArtifact, DependencySet)
constructor has been removed — this was used by the Shadow Plugin, so make sure you upgrade to at least version 2.x of that plugin. -
The
JavaBasePlugin.configureForSourceSet()
method has been removed. -
You can no longer create your own instances of JavaPluginConvention, ApplicationPluginConvention, WarPluginConvention, EarPluginConvention, BasePluginConvention, and ProjectReportsPluginConvention.
-
The
Maven
Plugin used to publish the highly outdated Maven 2 metadata format. This has been changed and it will now publish Maven 3 metadata, just like theMaven Publish
Plugin.With the removal of Maven 2 support, the methods that configure unique snapshot behavior have also been removed. Maven 3 only supports unique snapshots, so we decided to remove them.
-
- Tasks & properties
-
-
The following legacy classes and methods related to lazy properties have been removed — use ObjectFactory.property() to create
Property
instances:-
PropertyState
-
DirectoryVar
-
RegularFileVar
-
ProjectLayout.newDirectoryVar()
-
ProjectLayout.newFileVar()
-
Project.property(Class)
-
Script.property(Class)
-
ProviderFactory.property(Class)
-
-
Tasks configured and registered with the task configuration avoidance APIs have more restrictions on the other methods that can be called from a configuration action.
-
The internal
@Option
and@OptionValues
annotations — packageorg.gradle.api.internal.tasks.options
— have been removed. Use the public @Option and @OptionValues annotations instead. -
The
Task.deleteAllActions()
method has been removed with no replacement. -
The
Task.dependsOnTaskDidWork()
method has been removed — use declared inputs and outputs instead. -
The following properties and methods of
TaskInternal
have been removed — use task dependencies, task rules, reusable utility methods, or the Worker API in place of executing a task directly.-
execute()
-
executer
-
getValidators()
-
addValidator()
-
-
The TaskInputs.file(Object) method can no longer be called with an argument that resolves to anything other than a single regular file.
-
The TaskInputs.dir(Object) method can no longer be called with an argument that resolves to anything other than a single directory.
-
You can no longer register invalid inputs and outputs via TaskInputs and TaskOutputs.
-
The
TaskDestroyables.file()
andTaskDestroyables.files()
methods have been removed — use TaskDestroyables.register() instead. -
SimpleWorkResult
has been removed — use WorkResult.didWork. -
Overriding built-in tasks deprecated in 4.8 now produces an error.
Attempting to replace a built-in task will produce an error similar to the following:
> Cannot add task 'wrapper' as a task with that name already exists.
-
- Scala & Play
-
-
Play 2.2 is no longer supported — please upgrade the version of Play you are using.
-
The
ScalaDocOptions.styleSheet
property has been removed — the Scaladoc Ant task in Scala 2.11.8 and later no longer supports this property.
-
- Kotlin DSL
-
-
Artifact configuration accessors now have the type
NamedDomainObjectProvider<Configuration>
instead ofConfiguration
-
PluginAware.apply<T>(to)
was renamedPluginAware.applyTo<T>(target)
.
Both changes could cause script compilation errors. See the Gradle Kotlin DSL release notes for more information and how to fix builds broken by the changes described above.
-
- Miscellaneous
-
-
The
ConfigurableReport.setDestination(Object)
method has been removed — use ConfigurableReport.setDestination(File) instead. -
The
Signature.setFile(File)
method has been removed — Gradle does not support changing the output file for the generated signature. -
The read-only
Signature.toSignArtifact
property has been removed — it should never have been part of the public API. -
The
@DeferredConfigurable
annotation has been removed. -
The method
isDeferredConfigurable()
was removed fromExtensionSchema
. -
IdeaPlugin.performPostEvaluationActions()
andEclipsePlugin.performPostEvaluationActions()
have been removed. -
The `BroadcastingCollectionEventRegister.getAddAction()
method has been removed with no replacement. -
The internal
org.gradle.util
package is no longer imported by default.Ideally you shouldn’t use classes from this package, but, as a quick fix, you can add explicit imports to your build scripts for those classes.
-
The
gradlePluginPortal()
repository no longer looks for JARs without a POM by default. -
The Tooling API can no longer connect to builds using a Gradle version below Gradle 2.6. The same applies to builds run through TestKit.
-
Gradle 5.0 requires a minimum Tooling API client version of 3.0. Older client libraries can no longer run builds with Gradle 5.0.
-
The IdeaModule Tooling API model element contains methods to retrieve resources and test resources so those elements were removed from the result of
IdeaModule.getSourceDirs()
andIdeaModule.getTestSourceDirs()
. -
In previous Gradle versions, the
source
field inSourceTask
was accessible from subclasses. This is not the case anymore as thesource
field is now declared asprivate
. -
In the Worker API, the working directory of a worker can no longer be set.
-
A change in behavior related to dependency and version constraints may impact a small number of users.
-
There have been several changes to property factory methods on DefaultTask that may impact the creation of custom tasks.
-
Upgrading from 4.9 and earlier
If you are not already on version 4.9, skip down to the section that applies to your current Gradle version and work your way up until you reach here. Then, apply these changes when upgrading to Gradle 4.10.
Deprecated classes, methods and properties
Follow the API links to learn how to deal with these deprecations (if no extra information is provided here):
-
TaskContainer.add()
andTaskContainer.addAll()
— use TaskContainer.create() or TaskContainer.register() instead
Potential breaking changes
-
There have been several potentially breaking changes in Kotlin DSL — see the Breaking changes section of that project’s release notes.
-
You can no longer use any of the Project.beforeEvaluate() or Project.afterEvaluate() methods with lazy task configuration, for example inside a TaskContainer.register() block.
-
Both PluginUnderTestMetadata and GeneratePluginDescriptors — classes used by the Java Gradle Plugin Development Plugin — have been updated to use the Provider API.
Use the Property.set() method to modify their values rather than using standard property assignment syntax, unless you are doing so in a Groovy build script. Standard property assignment still works in that one case.
Upgrading from 4.8 and earlier
Potential breaking changes
-
You can no longer use GPath syntax with tasks.withType().
Use Groovy’s spread operator instead. For example, you would replace
tasks.withType(JavaCompile).name
withtasks.withType(JavaCompile)*.name
.
Upgrading from 4.7 and earlier
-
Configure existing
wrapper
andinit
tasks rather than defining your own -
Consider migrating to the built-in dependency locking mechanism if you are currently using a plugin or custom solution for this
Potential breaking changes
-
Build will now fail if a specified init script is not found.
-
TaskContainer.remove()
now actually removes the given task — some plugins may have accidentally relied on the old behavior. -
Gradle now honors implicit wildcards in Maven POM exclusions.
-
The Kotlin DSL now respects JSR-305 package annotations.
This will lead to some types annotated according to JSR-305 being treated as nullable where they were treated as non-nullable before. This may lead to compilation errors in the build script. See the relevant Kotlin DSL release notes for details.
-
Error messages will be directed to standard error rather than standard output now, unless a console is attached to both standard output and standard error. This may affect tools that scrape a build’s plain console output. Ignore this change if you’re upgrading from an earlier version of Gradle.
Deprecations
Prior to this release, builds were allowed to replace built-in tasks. This feature has been deprecated.
The full list of built-in tasks that should not be replaced is:
wrapper
, init
, help
, tasks
, projects
, buildEnvironment
, components
, dependencies
, dependencyInsight
, dependentComponents
, model
, properties
.
Upgrading from 4.6 and earlier
Potential breaking changes
-
Gradle will now, by convention, look for Checkstyle configuration files in the root project’s config/checkstyle directory.
Checkstyle configuration files in subprojects — the old by-convention location — will be ignored unless you explicitly configure their path via checkstyle.configDir or checkstyle.config.
-
The structure of Gradle’s plain console output has changed, which may break tools that scrape that output.
-
The APIs of many native tasks related to compilation, linking and installation have changed in breaking ways.
-
[Kotlin DSL] Delegated properties used to access Gradle’s build properties — defined in gradle.properties for example — must now be explicitly typed.
-
[Kotlin DSL] Declaring a
plugins {}
block inside a nested scope now throws an exception. -
[Kotlin DSL] Only one
pluginManagement {}
block is allowed now. -
The cache control DSL provided by the
org.gradle.api.artifacts.cache.*
interfaces are no longer available. -
getEnabledDirectoryReportDestinations()
,getEnabledFileReportDestinations()
andgetEnabledReportNames()
have all been removed fromorg.gradle.api.reporting.ReportContainer
. -
StartParameter.projectProperties and StartParameter.systemPropertiesArgs now return immutable maps.
Upgrading from 4.5 and earlier
Deprecations
-
You should not put annotation processors on the compile classpath or declare them with the
-processorpath
compiler argument.They should be added to the
annotationProcessor
configuration instead. If you don’t want any processing, but your compile classpath contains a processor unintentionally (e.g. as part of a library you depend on), use the-proc:none
compiler argument to ignore it. -
Use CommandLineArgumentProvider in place of CompilerArgumentProvider.
Potential breaking changes
-
The Java plugins now add a
sourceSetAnnotationProcessor
configuration for each source set, which might break if any of them match existing configurations you have. We recommend you remove your conflicting configuration declarations. -
The
StartParameter.taskOutputCacheEnabled
property has been replaced by StartParameter.setBuildCacheEnabled(boolean). -
The Visual Studio integration now only configures a single solution for all components in a build.
-
Gradle has replaced HttpClient 4.4.1 with version 4.5.5.
-
Gradle now bundles the
kotlin-stdlib-jdk8
artifact instead ofkotlin-stdlib-jre8
. This may affect your build. Please see the Kotlin documentation for more details.
Upgrading from 4.4 and earlier
-
Make sure you have a settings.gradle file: it avoids a performance penalty and allows you to set the root project’s name.
-
Gradle now ignores the build cache configuration of included builds (composite builds) and instead uses the root build’s configuration for all the builds.
Potential breaking changes
-
Two overloaded
ValidateTaskProperties.setOutputFile()
methods were removed. They are replaced with auto-generated setters when the task is accessed from a build script, but that won’t be the case from plugins and other code outside of the build script. -
The Maven Publish Plugin now produces more complete maven-metadata.xml files, including maintaining a list of
<snapshotVersion>
elements. Some older versions of Maven may not be able to consume this metadata. -
The
Depend
task type has been removed. -
Project.file(Object) no longer normalizes case for file paths on case-insensitive file systems. It now ignores case in such circumstances and does not touch the file system.
-
ListProperty no longer extends Property.
Upgrading from 4.3 and earlier
Potential breaking changes
-
AbstractTestTask is now extended by non-JVM test tasks as well as Test. Plugins should beware configuring all tasks of type
AbstractTestTask
because of this. -
The default output location for EclipseClasspath.defaultOutputDir has changed from
$projectDir
/bin to$projectDir
/bin/default. -
The deprecated
InstallExecutable.setDestinationDir(Provider)
was removed — use InstallExecutable.installDirectory instead. -
The deprecated
InstallExecutable.setExecutable(Provider)
was removed — use InstallExecutable.executableFile instead. -
Gradle will no longer prefer a version of Visual Studio found on the path over other locations. It is now a last resort.
You can bypass the toolchain discovery by specifying the installation directory of the version of Visual Studio you want via VisualCpp.setInstallDir(Object).
-
pluginManagement.repositories
is now of type RepositoryHandler rather thanPluginRepositoriesSpec
, which has been removed. -
5xx HTTP errors during dependency resolution will now trigger exceptions in the build.
-
The embedded Apache Ant has been upgraded from 1.9.6 to 1.9.9.
-
Several third-party libraries used by Gradle have been upgraded to fix security issues.
Upgrading from 4.2 and earlier
-
The
plugins {}
block can now be used in subprojects and for plugins in the buildSrc directory.
Other deprecations
-
You should no longer run Gradle versions older than 2.6 via the Tooling API.
-
You should no longer run any version of Gradle via an older version of the Tooling API than 3.0.
-
You should no longer chain TaskInputs.property(String,Object) and TaskInputs.properties(Map) methods.
Potential breaking changes
-
DefaultTask.newOutputDirectory() now returns a
DirectoryProperty
instead of aDirectoryVar
. -
DefaultTask.newOutputFile() now returns a
RegularFileProperty
instead of aRegularFileVar
. -
DefaultTask.newInputFile() now returns a
RegularFileProperty
instead of aRegularFileVar
. -
ProjectLayout.buildDirectory now returns a
DirectoryProperty
instead of aDirectoryVar
. -
AbstractNativeCompileTask.compilerArgs is now of type
ListProperty<String>
instead ofList<String>
. -
AbstractNativeCompileTask.objectFileDir is now of type
DirectoryProperty
instead ofFile
. -
AbstractLinkTask.linkerArgs is now of type
ListProperty<String>
instead ofList<String>
. -
TaskDestroyables.getFiles()
is no longer part of the public API. -
Overlapping version ranges for a dependency now result in Gradle picking a version that satisfies all declared ranges.
For example, if a dependency on
some-module
is found with a version range of[3,6]
and also transitively with a range of[4,8]
, Gradle now selects version 6 instead of 8. The prior behavior was to select 8. -
The order of elements in
Iterable
properties marked with either@OutputFiles
or@OutputDirectories
now matters. If the order changes, the property is no longer considered up to date.Prefer using separate properties with
@OutputFile
/@OutputDirectory
annotations or useMap
properties with@OutputFiles
/@OutputDirectories
instead. -
Gradle will no longer ignore dependency resolution errors from a repository when there is another repository it can check. Dependency resolution will fail instead. This results in more deterministic behavior with respect to resolution results.
Upgrading from 4.1 and earlier
Potential breaking changes
-
The
withPathSensitivity()
methods on TaskFilePropertyBuilder and TaskOutputFilePropertyBuilder have been removed. -
The bundled
bndlib
has been upgraded from 3.2.0 to 3.4.0. -
The FindBugs Plugin no longer renders progress information from its analysis. If you rely on that output in any way, you can enable it with FindBugs.showProgress.
Upgrading from 4.0
-
Consider using the new Worker API to enable units of work within your build to run in parallel.
Deprecated classes, methods and properties
Follow the API links to learn how to deal with these deprecations (if no extra information is provided here):
Potential breaking changes
-
Non-Java projects that have a project dependency on a Java project now consume the
runtimeElements
configuration by default instead of thedefault
configuration.To override this behavior, you can explicitly declare the configuration to use in the project dependency. For example:
project(path: ':myJavaProject', configuration: 'default')
. -
Default Zinc compiler upgraded from 0.3.13 to 0.3.15.
-
[Kotlin DSL] Base package renamed from
org.gradle.script.lang.kotlin
toorg.gradle.kotlin.dsl
.
Changes in detail
[5.0] Default memory settings changed
The command line client now starts with 64MB of heap instead of 1GB.
This may affect builds running directly inside the client VM using --no-daemon
mode.
We discourage the use of --no-daemon
, but if you must use it, you can increase the available memory using the GRADLE_OPTS
environment variable.
The Gradle daemon now starts with 512MB of heap instead of 1GB.
Large projects may have to increase this setting using the org.gradle.jvmargs
property.
All workers, including compilers and test executors, now start with 512MB of heap. The previous default was 1/4th of physical memory.
Large projects may have to increase this setting on the relevant tasks, e.g. JavaCompile
or Test
.
[5.0] New default versions for code quality plugins
The default tool versions of the following code quality plugins have been updated:
-
The Checkstyle Plugin now uses 8.12 instead of 6.19 by default.
-
The CodeNarc Plugin now uses 1.2.1 instead of 1.1 by default.
-
The JaCoCo Plugin now uses 0.8.2 instead of 0.8.1 by default.
-
The PMD Plugin now uses 6.8.0 instead of 5.6.1 by default.
In addition, the default ruleset was changed from the now deprecated
java-basic
tocategory/java/errorprone.xml
.We recommend configuring a ruleset explicitly, though.
[5.0] Library upgrades
Several libraries that are used by Gradle have been upgraded:
-
Groovy was upgraded from 2.4.15 to 2.5.4.
-
Ant has been upgraded from 1.9.11 to 1.9.13.
-
The AWS SDK used to access S3-backed Maven/Ivy repositories has been upgraded from 1.11.267 to 1.11.407.
-
The BND library used by the OSGi Plugin has been upgraded from 3.4.0 to 4.0.0.
-
The Google Cloud Storage JSON API Client Library used to access Google Cloud Storage backed Maven/Ivy repositories has been upgraded from v1-rev116-1.23.0 to v1-rev136-1.25.0.
-
Ivy has been upgraded from 2.2.0 to 2.3.0.
-
The JUnit Platform libraries used by the
Test
task have been upgraded from 1.0.3 to 1.3.1. -
The Maven Wagon libraries used to access Maven repositories have been upgraded from 2.4 to 3.0.0.
-
SLF4J has been upgraded from 1.7.16 to 1.7.25.
[5.0] Improved support for dependency and version constraints
Through the Gradle 4.x release stream, new @Incubating
features were added to the dependency resolution engine.
These include sophisticated version constraints (prefer
, strictly
, reject
), dependency constraints, and platform
dependencies.
If you have been using the IMPROVED_POM_SUPPORT
feature preview, playing with constraints or prefer, reject and other specific version indications, then make sure to take a good look at your dependency resolution results.
[5.0] BOM import
Gradle now provides support for importing bill of materials (BOM) files, which are effectively POM files that use <dependencyManagement>
sections to control the versions of direct and transitive dependencies. All you need to do is declare the POM as a platform
dependency.
The following example picks the versions of the gson
and dom4j
dependencies from the declared Spring Boot BOM:
dependencies { // import a BOM implementation platform('org.springframework.boot:spring-boot-dependencies:1.5.8.RELEASE') // define dependencies without versions implementation 'com.google.code.gson:gson' implementation 'dom4j:dom4j' }
[5.0] Separation of compile and runtime dependencies when consuming POMs
Since Gradle 1.0, runtime-scoped dependencies have been included in the Java compilation classpath, which has some drawbacks:
-
The compilation classpath is much larger than it needs to be, slowing down compilation.
-
The compilation classpath includes runtime-scoped files that do not impact compilation, resulting in unnecessary re-compilation when those files change.
With this new behavior, the Java and Java Library plugins both honor the separation of compile and runtime scopes. This means that the compilation classpath only includes compile-scoped dependencies, while the runtime classpath adds the runtime-scoped dependencies as well. This is particularly useful if you develop and publish Java libraries with Gradle where the separation between api
and implementation
dependencies is reflected in the published scopes.
[5.0] Changes to property factory methods on DefaultTask
Property factory methods on DefaultTask
are now final
The property factory methods such as newInputFile()
are intended to be called from the constructor of a type that extends DefaultTask
. These methods are now final to avoid subclasses overriding these methods and using state that is not initialized.
Inputs and outputs are not automatically registered
The Property instances that are returned by these methods are no longer automatically registered as inputs or outputs of the task. The Property instances need to be declared as inputs or outputs in the usual ways, such as attaching annotations such as @OutputFile
or using the runtime API to register the property.
For example, you could previously use the following syntax and have both outputFile instances registered as declared outputs:
class MyTask extends DefaultTask {
// note: no annotation here
final RegularFileProperty outputFile = newOutputFile()
}
task myOtherTask {
def outputFile = newOutputFile()
doLast { ... }
}
open class MyTask : DefaultTask() {
// note: no annotation here
val outputFile: RegularFileProperty = newOutputFile()
}
task("myOtherTask") {
val outputFile = newOutputFile()
doLast { ... }
}
Now you have to explicitly register outputFile
, like this:
class MyTask extends DefaultTask {
@OutputFile // property needs an annotation
final RegularFileProperty outputFile = project.objects.fileProperty()
}
task myOtherTask {
def outputFile = project.objects.fileProperty()
outputs.file(outputFile) // or to be registered using the runtime API
doLast { ... }
}
open class MyTask : DefaultTask() {
@OutputFile // property needs an annotation
val outputFile: RegularFileProperty = project.objects.fileProperty()
}
task("myOtherTask") {
val outputFile = project.objects.fileProperty()
outputs.file(outputFile) // or to be registered using the runtime API
doLast { ... }
}
[5.0] Gradle now bundles JAXB for Java 9 and above
In order to use S3 backed artifact repositories, you previously had to add --add-modules java.xml.bind
to org.gradle.jvmargs
when running on Java 9 and above.
Since Java 11 no longer contains the java.xml.bind
module, Gradle now bundles JAXB 2.3.1 (com.sun.xml.bind:jaxb-impl
) and uses it on Java 9 and above.
Please remove the --add-modules java.xml.bind
option from org.gradle.jvmargs
, if set.
[5.0] The gradlePluginPortal()
repository no longer looks for JARs without a POM by default
With this new behavior, if a plugin or a transitive dependency of a plugin found in the gradlePluginPortal()
repository has no Maven POM it will fail to resolve.
Artifacts published to a Maven repository without a POM should be fixed. If you encounter such artifacts, please ask the plugin or library author to publish a new version with proper metadata.
If you are stuck with a bad plugin, you can work around by re-enabling JARs as metadata source for the gradlePluginPortal()
repository:
pluginManagement {
repositories {
gradlePluginPortal().tap {
metadataSources {
mavenPom()
artifact()
}
}
}
}
pluginManagement {
repositories {
gradlePluginPortal().apply {
(this as MavenArtifactRepository).metadataSources {
mavenPom()
artifact()
}
}
}
}
Java Library Distribution Plugin utilizes Java Library Plugin
The Java Library Distribution Plugin is now based on the Java Library Plugin instead of the Java Plugin.
Additionally, the default distribution created by the plugin will contain all artifacts of the runtimeClasspath
configuration instead of the deprecated runtime
configuration.
Configuration Avoidance API disallows common configuration errors
The configuration avoidance API introduced in Gradle 4.9 allows you to avoid creating and configuring tasks that are never used.
With the existing API, this example adds two tasks (foo
and bar
):
tasks.create("foo") {
tasks.create("bar")
}
tasks.create("foo") {
tasks.create("bar")
}
When converting this to use the new API, something surprising happens: bar
doesn’t exist. The new API only executes configuration actions when necessary,
so the register()
for task bar
only executes when foo
is configured.
tasks.register("foo") {
tasks.register("bar") // WRONG
}
tasks.register("foo") {
tasks.register("bar") // WRONG
}
To avoid this, Gradle now detects this and prevents modification to the underlying container (through create()
or register()
) when using the new API.
[5.0] Worker API: working directory of a worker can no longer be set
Since JDK 11 no longer supports changing the working directory of a running process, setting the working directory of a worker via its fork options is now prohibited.
All workers now use the same working directory to enable reuse.
Please pass files and directories as arguments instead.
[4.10] Publishing to AWS S3 requires new permissions
The S3 repository transport protocol allows Gradle to publish artifacts to AWS S3 buckets. Starting with this release, every artifact uploaded to an S3 bucket will be equipped with the bucket-owner-full-control
canned ACL. Make sure that the AWS account used to publish artifacts has the s3:PutObjectAcl
and s3:PutObjectVersionAcl
permissions, otherwise the upload will fail.
{
"Version":"2012-10-17",
"Statement":[
// ...
{
"Effect":"Allow",
"Action":[
"s3:PutObject", // necessary for uploading objects
"s3:PutObjectAcl", // required starting with this release
"s3:PutObjectVersionAcl" // if S3 bucket versioning is enabled
],
"Resource":"arn:aws:s3:::myCompanyBucket/*"
}
]
}
See AWS S3 Cross Account Access for more information.
[4.9] Consider trying the lazy API for task creation and configuration
Gradle 4.9 introduced a new way to create and configure tasks that works lazily. When you use this approach for tasks that are expensive to configure, or when you have many, many tasks, your build configuration time can drop significantly when those tasks don’t run.
You can learn more about lazily creating tasks in the Task Configuration Avoidance chapter. You can also read about the background to this new feature in this blog post.
[4.8] Switch to the Maven Publish and Ivy Publish Plugins
Now that the publishing plugins are stable, we recommend that you migrate from the legacy publishing mechanism for standard Java projects, i.e. those based on the Java Plugin. That includes projects that use any one of: Java Library Plugin, Application Plugin or War Plugin.
To use the new approach, simply replace any upload<Conf>
configuration with a publishing {}
block. See the publishing overview chapter for more information.
[4.8] Use deferred configuration for publishing plugins
Prior to Gradle 4.8, the publishing {}
block was implicitly treated as if all the logic inside it was executed after the project was evaluated.
This was confusing, because it was the only block that behaved that way.
As part of the stabilization effort in Gradle 4.8, we are deprecating this behavior and asking all users to migrate their build.
The new, stable behavior can be switched on by adding the following to your settings file:
enableFeaturePreview('STABLE_PUBLISHING')
enableFeaturePreview("STABLE_PUBLISHING")
We recommend doing a test run with a local repository to see whether all artifacts still have the expected coordinates. In most cases everything should work as before and you are done. However, your publishing block may rely on the implicit deferred configuration, particularly if it relies on values that may change during the configuration phase of the build.
For example, under the new behavior, the following logic assumes that jar.archiveBaseName
doesn’t change after artifactId
is set:
subprojects {
publishing {
publications {
mavenJava {
from components.java
artifactId = jar.archiveBaseName
}
}
}
}
subprojects {
publishing {
publications {
named<MavenPublication>("mavenJava") {
from(components["java"])
artifactId = tasks.jar.get().archiveBaseName.get()
}
}
}
}
If that assumption is incorrect or might possibly be incorrect in the future, the artifactId
must be set within an afterEvaluate {}
block, like so:
subprojects {
publishing {
publications {
mavenJava {
from components.java
afterEvaluate {
artifactId = jar.archiveBaseName
}
}
}
}
}
subprojects {
publishing {
publications {
named<MavenPublication>("mavenJava") {
from(components["java"])
afterEvaluate {
artifactId = tasks.jar.get().archiveBbaseName.get()
}
}
}
}
}
[4.8] Configure existing wrapper
and init
tasks
You should no longer define your own wrapper
and init
tasks. Configure the existing tasks instead, for example by converting this:
task wrapper(type: Wrapper) {
...
}
task<Wrapper>("wrapper") {
...
}
to this:
wrapper {
...
}
tasks.wrapper {
...
}
[4.8] Gradle now honors implicit wildcards in Maven POM exclusions
If an exclusion in a Maven POM was missing either a groupId
or artifactId
, Gradle used to ignore the exclusion. Now the missing elements are treated as implicit wildcards — e.g. <groupId>*</groupId>
— which means that some of your dependencies may now be excluded where they weren’t before.
You will need to explicitly declare any missing dependencies that you need.
[4.7] Changes to the structure of Gradle’s plain console output
The plain console mode now formats output consistently with the rich console, which means that the output format has changed. For example:
-
The output produced by a given task is now grouped together, even when other tasks execute in parallel with it.
-
Task execution headers are printed with a "> Task" prefix.
-
All output produced during build execution is written to the standard output file handle. This includes messages written to System.err unless you are redirecting standard error to a file or any other non-console destination.
This may break tools that scrape details from the plain console output.
[4.6] Changes to the APIs of native tasks related to compilation, linking and installation
Many tasks related to compiling, linking and installing native libraries and applications have been converted to the Provider API so that they support lazy configuration. This conversion has introduced some breaking changes to the APIs of the tasks so that they match the conventions of the Provider API.
The following tasks have been changed:
- AbstractLinkTask and its subclasses
-
-
getDestinationDir()
was replaced bygetDestinationDirectory()
. -
getBinaryFile()
,getOutputFile()
was replaced bygetLinkedFile()
. -
setOutputFile(File)
was removed. UseProperty.set()
instead. -
setOutputFile(Provider)
was removed. UseProperty.set()
instead. -
getTargetPlatform()
was changed to return aProperty
. -
setTargetPlatform(NativePlatform)
was removed. UseProperty.set()
instead. -
getToolChain()
was changed to return aProperty
. -
setToolChain(NativeToolChain)
was removed. UseProperty.set()
instead.
-
- CreateStaticLibrary
-
-
getOutputFile()
was changed to return aProperty
. -
setOutputFile(File)
was removed. UseProperty.set()
instead. -
setOutputFile(Provider)
was removed. UseProperty.set()
instead. -
getTargetPlatform()
was changed to return aProperty
. -
setTargetPlatform(NativePlatform)
was removed. UseProperty.set()
instead. -
getToolChain()
was changed to return aProperty
. -
setToolChain(NativeToolChain)
was removed. UseProperty.set()
instead. -
getStaticLibArgs()
was changed to return aListProperty
. -
setStaticLibArgs(List)
was removed. UseListProperty.set()
instead.
-
- InstallExecutable
-
-
getSourceFile()
was replaced bygetExecutableFile()
. -
getPlatform()
was replaced bygetTargetPlatform()
. -
setTargetPlatform(NativePlatform)
was removed. UseProperty.set()
instead. -
getToolChain()
was changed to return aProperty
. -
setToolChain(NativeToolChain)
was removed. UseProperty.set()
instead.
-
The following have also seen similar changes:
[4.6] Visual Studio integration only supports a single solution file for all components of a build
VisualStudioExtension no longer has a solutions
property. Instead, you configure a single solution via VisualStudioRootExtension in the root project, like so:
model {
visualStudio {
solution {
solutionFile.location = "vs/${name}.sln"
}
}
}
In addition, there are no longer individual tasks to generate the solution files for each component, but rather a single visualStudio
task that generates a solution file that encompasses all components in the build.
[4.5] HttpBuildCache
no longer follows redirects
When connecting to an HTTP build cache backend via HttpBuildCache
, Gradle does not follow redirects any more, treating them as errors instead. Getting a redirect from the build cache backend is mostly a configuration error — using an "http" URL instead of "https" for example — and has negative effects on performance.
[4.4] Third-party dependency upgrades
This version includes several upgrades of third-party dependencies:
-
jackson: 2.6.6 → 2.8.9
-
plexus-utils: 2.0.6 → 2.1
-
xercesImpl: 2.9.1 → 2.11.0
-
bsh: 2.0b4 → 2.0b6
-
bouncycastle: 1.57 → 1.58
This fix the following security issues:
-
CVE-2017-7525 (critical)
-
SONATYPE-2017-0359 (critical)
-
SONATYPE-2017-0355 (critical)
-
SONATYPE-2017-0398 (critical)
-
CVE-2013-4002 (critical)
-
CVE-2016-2510 (severe)
-
SONATYPE-2016-0397 (severe)
-
CVE-2009-2625 (severe)
-
SONATYPE-2017-0348 (severe)
Gradle does not expose public APIs for these 3rd-party dependencies, but those who customize Gradle will want to be aware.
Migrating Builds From Apache Maven
Tip
|
Suffering from slow Maven builds? Register here for our Build Cache training session to learn how Gradle Enterprise can speed up Maven builds by up to 90%. |
Apache Maven is a build tool for Java and other JVM-based projects that’s in widespread use, and so people that want to use Gradle often have to migrate an existing Maven build. This guide will help with such a migration by explaining the differences and similarities between the two tools' models and providing steps that you can follow to ease the process.
Converting a build can be scary, but you don’t have to do it alone. You can search docs, forums, and StackOverflow from help.gradle.org or reach out to the Gradle community on the forums if you get stuck.
Making a case for migration
The primary differences between Gradle and Maven are flexibility, performance, user experience, and dependency management. A visual overview of these aspects is available in the Maven vs Gradle feature comparison.
Since Gradle 3.0, Gradle has invested heavily in making Gradle builds much faster, with features such as build caching, compile avoidance, and an improved incremental Java compiler. Gradle is now 2-10x faster than Maven for the vast majority of projects, even without using a build cache. In-depth performance comparison and business cases for switching from Maven to Gradle can be found here.
General guidelines
Gradle and Maven have fundamentally different views on how to build a project. Gradle provides a flexible and extensible build model that delegates the actual work to the execution of a graph of tasks. Maven uses a model of fixed, linear phases to which you can attach goals (the things that do the work). This may make migrating between the two seem intimidating, but migrations can be surprisingly easy because Gradle follows many of the same conventions as Maven — such as the standard project structure — and its dependency management works in a similar way.
Here we lay out a series of steps for you to follow that will help facilitate the migration of any Maven build to Gradle:
Tip
|
Keep the old Maven build and new Gradle build side by side. You know the Maven build works, so you should keep it until you are confident that the Gradle build produces all the same artifacts and otherwise does what you need. This also means that users can try the Gradle build without getting a new copy of the source tree. |
-
Create a build scan for the Maven build.
A build scan will make it easier to visualize what’s happening in your existing Maven build. For Maven builds, you’ll be able to see the project structure, what plugins are being used, a timeline of the build steps, and more. Keep this handy so you can compare it to the Gradle build scans you get while converting the project.
-
Develop a mechanism to verify that the two builds produce the same artifacts
This is a vitally important step to ensure that your deployments and tests don’t break. Even small changes, such as the contents of a manifest file in a JAR, can cause problems. If your Gradle build produces the same output as the Maven build, this will give you and others confidence in switching over and make it easier to implement the big changes that will provide the greatest benefits.
This doesn’t mean that you need to verify every artifact at every stage, although doing so can help you quickly identify the source of a problem. You can just focus on the critical output such as final reports and the artifacts that are published or deployed.
You will need to factor in some inherent differences in the build output that Gradle produces compared to Maven. Generated POMs will contain only the information needed for consumption and they will use
<compile>
and<runtime>
scopes correctly for that scenario. You might also see differences in the order of files in archives and of files on classpaths. Most differences will be benign, but it’s worth identifying them and verifying that they are OK. -
This will create all the Gradle build files you need, even for multi-module builds. For simpler Maven projects, the Gradle build will be ready to run!
-
Create a build scan for the Gradle build.
A build scan will make it easier to visualize what’s happening in the build. For Gradle builds, you’ll be able to see the project structure, the dependencies (regular and inter-project ones), what plugins are being used and the console output of the build.
Your build may fail at this point, but that’s ok; the scan will still run. Compare the build scan for the Gradle build to the one for the Maven build and continue down this list to troubleshoot the failures.
We recommend that you regularly generate build scans during the migration to help you identify and troubleshoot problems. If you want, you can also use a Gradle build scan to identify opportunities to improve the performance of the build, after all performance is a big reason for switching to Gradle in the first place.
-
Configure integration and functional tests
Many tests can simply be migrated by configuring an extra source set. If you are using a third-party library, such as FitNesse, look to see whether there is a suitable community plugin available on the Gradle Plugin Portal.
-
Replace Maven plugins with Gradle equivalents
In the case of popular plugins, Gradle often has an equivalent plugin that you can use. You might also find that you can replace a plugin with built-in Gradle functionality. As a last resort, you may need to reimplement a Maven plugin via your own custom plugins and task types.
The rest of this chapter looks in more detail at specific aspects of migrating a build from Maven to Gradle.
Understanding the build lifecycle
Maven builds are based around the concept of build lifecycles that consist of a set of fixed phases. This can prove an impediment for users migrating to Gradle because its build lifecycle is something different, although it’s important to understand how Gradle builds fit into the structure of initialization, configuration, and execution phases. Fortunately, Gradle has a feature that can mimic Maven’s phases: lifecycle tasks.
These allow you to define your own "lifecycles" by creating no-action tasks that simply depend on the tasks you’re interested in. And to make the transition to Gradle easier for Maven users, the Base Plugin — applied by all the JVM language plugins like the Java Library Plugin — provides a set of lifecycle tasks that correspond to the main Maven phases.
Here is a list of some of the main Maven phases and the Gradle tasks that they map to:
clean
-
Use the
clean
task provided by the Base Plugin. compile
-
Use the
classes
task provided by the Java Plugin and other JVM language plugins. This compiles all classes for all source files of all languages and also performs resource filtering via theprocessResources
task. test
-
Use the
test
task provided by the Java Plugin. It runs just the unit tests, or more specifically, the tests that make up thetest
source set. package
-
Use the
assemble
task provided by the Base Plugin. This builds whatever is the appropriate package for the project, for example a JAR for Java libraries or a WAR for traditional Java webapps. verify
-
Use the
check
task provided by the Base Plugin. This runs all verification tasks that are attached to it, which typically includes the unit tests, any static analysis tasks — such as Checkstyle — and others. If you want to include integration tests, you will have to configure these manually, which is a simple process. install
-
Use the
publishToMavenLocal
task provided by the Maven Publish Plugin.Note that Gradle builds don’t require you to "install" artifacts as you have access to more appropriate features like inter-project dependencies and composite builds. You should only use
publishToMavenLocal
for interoperating with Maven builds.Gradle also allows you to resolve dependencies against the local Maven cache, as described in the Declaring repositories section.
deploy
-
Use the
publish
task provided by the Maven Publish Plugin — making sure you switch from the older Maven Plugin (ID:maven
) if your build is using that one. This will publish your package to all configured publication repositories. There are also other tasks that allow you to publish to a single repository even when multiple ones are defined.Note that the Maven Publish Plugin does not publish source and Javadoc JARs by default, but this can easily be activated as explained in the guide for building java projects.
Performing an automatic conversion
Gradle’s init
task is typically used to create a new skeleton project, but you can also use it to convert an existing Maven build to Gradle automatically.
Once Gradle is installed on your system, all you have to do is run the command
> gradle init
from the root project directory and let Gradle do its thing. That basically consists of parsing the existing POMs and generating the corresponding Gradle build scripts. Gradle will also create a settings script if you’re migrating a multi-project build.
You’ll find that the new Gradle build includes the following:
-
All the custom repositories that are specified in the POM
-
Your external and inter-project dependencies
-
The appropriate plugins to build the project (limited to one or more of the Maven Publish, Java and War Plugins)
See the Build Init Plugin chapter for a complete list of the automatic conversion features.
One thing to bear in mind is that assemblies are not automatically converted. They aren’t necessarily problematic to convert, but you will need to do some manual work. Options include:
-
Using the Distribution Plugin
-
Using the Java Library Distribution Plugin
-
Using the Application Plugin
-
Using a suitable community plugin from the Gradle Plugin Portal
If your Maven build does not have many plugins or much in the way of customisation, you can simply run
> gradle build
once the migration has completed. This will run the tests and produce the required artifacts without any extra intervention on your part.
Migrating dependencies
Gradle’s dependency management system is more flexible than Maven’s, but it still supports the same concepts of repositories, declared dependencies, scopes (dependency configurations in Gradle), and transitive dependencies. In fact, Gradle works perfectly with Maven-compatible repositories, which makes it easy to migrate your dependencies.
Note
|
One notable difference between the two tools is in how they manage version conflicts. Maven uses a "closest" match algorithm, whereas Gradle picks the newest. Don’t worry though, you have a lot of control over which versions are selected, as documented in Managing Transitive Dependencies. |
Over the following sections, we will show you how to migrate the most common elements of a Maven build’s dependency management information.
Declaring dependencies
Gradle uses the same dependency identifier components as Maven: group ID, artifact ID and version. It also supports classifiers. So all you need to do is substitute the identifier information for a dependency into Gradle’s syntax, which is described in the Declaring Dependencies chapter.
For example, consider this Maven-style dependency on Log4J:
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
</dependencies>
This dependency would look like the following in a Gradle build script:
dependencies {
implementation("log4j:log4j:1.2.12") // (1)
}
dependencies {
implementation 'log4j:log4j:1.2.12' // (1)
}
-
Attaches version 1.2.12 of Log4J to the
implementation
configuration (scope)
The string identifier takes the Maven values of groupId
, artifactId
and version
, although Gradle refers to them as group
, module
and version
.
The above example raises an obvious question: what is that implementation
configuration?
It’s one of the standard dependency configurations provided by the Java Plugin and is often used as a substitute for Maven’s default compile
scope.
Several of the differences between Maven’s scopes and Gradle’s standard configurations come down to Gradle distinguishing between the dependencies required to build a module and the dependencies required to build a module that depends on it. Maven makes no such distinction, so published POMs typically include dependencies that consumers of a library don’t actually need.
Here are the main Maven dependency scopes and how you should deal with their migration:
compile
-
Gradle has two configurations that can be used in place of the
compile
scope:implementation
andapi
. The former is available to any project that applies the Java Plugin, whileapi
is only available to projects that specifically apply the Java Library Plugin.In most cases you should simply use the
implementation
configuration, particularly if you’re building an application or webapp. But if you’re building a library, you can learn about which dependencies should be declared usingapi
in the section on Building Java libraries. Even more information on the differences betweenapi
andimplementation
is provided in the Java Library Plugin chapter linked above. runtime
-
Use the
runtimeOnly
configuration. test
-
Gradle distinguishes between those dependencies that are required to compile a project’s tests and those that are only needed to run them.
Dependencies required for test compilation should be declared against the
testImplementation
configuration. Those that are only required for running the tests should usetestRuntimeOnly
. provided
-
Use the
compileOnly
configuration.Note that the War Plugin adds
providedCompile
andprovidedRuntime
dependency configurations. These behave slightly differently fromcompileOnly
and simply ensure that those dependencies aren’t packaged in the WAR file. However, the dependencies are included on runtime and test runtime classpaths, so use these configurations if that’s the behavior you need. import
-
The
import
scope is mostly used within<dependencyManagement>
blocks and applies solely to POM-only publications. Read the section on Using bills of materials to learn more about how to replicate this behavior.You can also specify a regular dependency on a POM-only publication. In this case, the dependencies declared in that POM are treated as normal transitive dependencies of the build.
For example, imagine you want to use the
groovy-all
POM for your tests. It’s a POM-only publication that has its own dependencies listed inside a<dependencies>
block. The appropriate configuration in the Gradle build looks like this:Example 2. Consuming a POM-only dependencybuild.gradle.ktsdependencies { testImplementation("org.codehaus.groovy:groovy-all:2.5.4") }
build.gradledependencies { testImplementation 'org.codehaus.groovy:groovy-all:2.5.4' }
The result of this will be that all
compile
andruntime
scope dependencies in thegroovy-all
POM get added to the test runtime classpath, while only thecompile
scope dependencies get added to the test compilation classpath. Dependencies with other scopes will be ignored.
Declaring repositories
Gradle allows you to retrieve declared dependencies from any Maven-compatible or Ivy-compatible repository. Unlike Maven, it has no default repository and so you have to declare at least one. In order to have the same behavior as your Maven build, just configure Maven Central in your Gradle build, like this:
repositories {
mavenCentral()
}
repositories {
mavenCentral()
}
You can also use the repositories {}
block to configure custom repositories, as described in the Repository Types chapter.
Lastly, Gradle allows you to resolve dependencies against the local Maven cache/repository.
This helps Gradle builds interoperate with Maven builds, but it shouldn’t be a technique that you use if you don’t need that interoperability.
If you want to share published artifacts via the filesystem, consider configuring a custom Maven repository with a file://
URL.
You might also be interested in learning about Gradle’s own dependency cache, which behaves more reliably than Maven’s and can be used safely by multiple concurrent Gradle processes.
Controlling dependency versions
The existence of transitive dependencies means that you can very easily end up with multiple versions of the same dependency in your dependency graph. By default, Gradle will pick the newest version of a dependency in the graph, but that’s not always the right solution. That’s why it provides several mechanisms for controlling which version of a given dependency is resolved.
On a per-project basis, you can use:
There are even more, specialized options listed in the controlling transitive dependencies chapter.
If you want to ensure consistency of versions across all projects in a multi-project build, similar to how the <dependencyManagement>
block in Maven works, you can use the Java Platform Plugin.
This allows you declare a set of dependency constraints that can be applied to multiple projects.
You can even publish the platform as a Maven BOM or using Gradle’s metadata format.
See the plugin page for more information on how to do that, and in particular the section on Consuming platforms to see how you can apply a platform to other projects in the same build.
Excluding transitive dependencies
Maven builds use exclusions to keep unwanted dependencies — or unwanted versions of dependencies — out of the dependency graph. You can do the same thing with Gradle, but that’s not necessarily the right thing to do. Gradle provides other options that may be more appropriate for a given situation, so you really need to understand why an exclusion is in place to migrate it properly.
If you want to exclude a dependency for reasons unrelated to versions, then check out the section on excluding transitive dependencies. It shows you how to attach an exclusion either to an entire configuration (often the most appropriate solution) or to a dependency. You can even easily apply an exclusion to all configurations.
If you’re more interested in controlling which version of a dependency is actually resolved, see the previous section.
Handling optional dependencies
You are likely to encounter two situations regarding optional dependencies:
-
Some of your transitive dependencies are declared as optional
-
You want to declare some of your direct dependencies as optional in your project’s published POM
For the first scenario, Gradle behaves the same way as Maven and simply ignores any transitive dependencies that are declared as optional. They are not resolved and have no impact on the versions selected if the same dependencies appear elsewhere in the dependency graph as non-optional.
As for publishing dependencies as optional, Gradle provides a richer model called feature variants, which will let you declare the "optional features" your library provides.
Using bills of materials (BOMs)
Maven allows you to share dependency constraints by defining dependencies inside a <dependencyManagement>
section of a POM file that has a packaging type of pom
.
This special type of POM (a BOM) can then be imported into other POMs so that you have consistent library versions across your projects.
Gradle can use such BOMs for the same purpose, using a special dependency syntax based on platform() and enforcedPlatform() methods. You simply declare the dependency in the normal way, but wrap the dependency identifier in the appropriate method, as shown in this example that "imports" the Spring Boot Dependencies BOM:
dependencies {
implementation(platform("org.springframework.boot:spring-boot-dependencies:1.5.8.RELEASE")) // (1)
implementation("com.google.code.gson:gson") // (2)
implementation("dom4j:dom4j")
}
dependencies {
implementation platform('org.springframework.boot:spring-boot-dependencies:1.5.8.RELEASE') // (1)
implementation 'com.google.code.gson:gson' // (2)
implementation 'dom4j:dom4j'
}
-
Applies the Spring Boot Dependencies BOM
-
Adds a dependency whose version is defined by that BOM
You can learn more about this feature and the difference between platform()
and enforcedPlatform()
in the section on importing version recommendations from a Maven BOM.
Note
|
You can use this feature to apply the <dependencyManagement> information from any dependency’s POM to the Gradle build, even those that don’t have a packaging type of pom . Both platform() and enforcedPlatform() will ignore any dependencies declared in the <dependencies> block.
|
Migrating multi-module builds (project aggregation)
Maven’s multi-module builds map nicely to Gradle’s multi-project builds. Try the corresponding sample to see how a basic multi-project Gradle build is set up.
To migrate a multi-module Maven build, simply follow these steps:
-
Create a settings script that matches the
<modules>
block of the root POM.For example, this
<modules>
block:<modules> <module>simple-weather</module> <module>simple-webapp</module> </modules>
can be migrated by adding the following line to the settings script:
settings.gradle.ktsrootProject.name = "simple-multi-module" // (1) include("simple-weather", "simple-webapp") // (2)
settings.gradlerootProject.name = 'simple-multi-module' // (1) include 'simple-weather', 'simple-webapp' // (2)
-
Sets the name of the overall project
-
Configures two subprojects as part of this build
Output ofgradle projects
> gradle projects ------------------------------------------------------------ Root project 'simple-multi-module' ------------------------------------------------------------ Root project 'simple-multi-module' +--- Project ':simple-weather' \--- Project ':simple-webapp' To see a list of the tasks of a project, run gradle <project-path>:tasks For example, try running gradle :simple-weather:tasks
-
-
Replace cross-module dependencies with project dependencies.
-
Replicate project inheritance with convention plugins.
This basically involves creating a root project build script that injects shared configuration into the appropriate subprojects.
Sharing versions across projects
If you want to replicate the Maven pattern of having dependency versions declared in the dependencyManagement
section of the root POM file, the best approach is to leverage the java-platform
plugin.
You will need to add a dedicated project for this and consume it in the regular projects of your build.
See the documentation for more details on this pattern.
Migrating Maven profiles and properties
Maven allows you parameterize builds using properties of various sorts. Some are read-only properties of the project model, others are user-defined in the POM. It even allows you to treat system properties as project properties.
Gradle has a similar system of project properties, although it differentiates between those and system properties. You can, for example, define properties in:
-
the build script
-
a
gradle.properties
file in the root project directory -
a
gradle.properties
file in the$HOME/.gradle
directory
Those aren’t the only options, so if you are interested in finding out more about how and where you can define properties, check out the Build Environment chapter.
One important piece of behavior you need to be aware of is what happens when the same property is defined in both the build script and one of the external properties files: the build script value takes precedence. Always. Fortunately, you can mimic the concept of profiles to provide overridable default values.
Which brings us on to Maven profiles. These are a way to enable and disable different configurations based on environment, target platform, or any other similar factor. Logically, they are nothing more than limited ‘if' statements. And since Gradle has much more powerful ways to declare conditions, it does not need to have formal support for profiles (except in the POMs of dependencies). You can easily get the same behavior by combining conditions with secondary build scripts, as you’ll see.
Let’s say you have different deployment settings depending on the environment: local development (the default), a test environment, and production.
To add profile-like behavior, you first create build scripts for each environment in the project root: profile-default.gradle
, profile-test.gradle
, and profile-prod.gradle
.
You can then conditionally apply one of those profile scripts based on a project property of your own choice.
The following example demonstrates the basic technique using a project property called buildProfile
and profile scripts that simply initialize an extra project property called message
:
val buildProfile: String? by project // (1)
apply(from = "profile-${buildProfile ?: "default"}.gradle.kts") // (2)
tasks.register("greeting") {
// Store the message into a variable, because referencing extras from the task action
// is not compatible with the configuration cache.
val message = project.extra["message"]
doLast {
println(message) // (3)
}
}
val message by extra("foobar") // (4)
val message by extra("testing 1 2 3") // (4)
val message by extra("Hello, world!") // (4)
if (!hasProperty('buildProfile')) ext.buildProfile = 'default' // (1)
apply from: "profile-${buildProfile}.gradle" // (2)
tasks.register('greeting') {
// Store the message into a variable, because referencing extras from the task action
// is not compatible with the configuration cache.
def message = project.message
doLast {
println message // (3)
}
}
ext.message = 'foobar' // (4)
ext.message = 'testing 1 2 3' // (4)
ext.message = 'Hello, world!' // (4)
-
Checks for the existence of (Groovy) or binds (Kotlin) the
buildProfile
project property -
Applies the appropriate profile script, using the value of
buildProfile
in the script filename -
Prints out the value of the
message
extra project property -
Initializes the
message
extra project property, whose value can then be used in the main build script
With this setup in place, you can activate one of the profiles by passing a value for the project property you’re using — buildProfile
in this case:
gradle greeting
> gradle greeting foobar
gradle -PbuildProfile=test greeting
> gradle -PbuildProfile=test greeting testing 1 2 3
You’re not limited to checking project properties. You could also check environment variables, the JDK version, the OS the build is running on, or anything else you can imagine.
One thing to bear in mind is that high level condition statements make builds harder to understand and maintain, similar to the way they complicate object-oriented code.
The same applies to profiles.
Gradle offers you many better ways to avoid the extensive use of profiles that Maven often requires, for example by configuring multiple tasks that are variants of one another.
See the publishPubNamePublicationToRepoNameRepository
tasks created by the Maven Publish Plugin.
For a lengthier discussion on working with Maven profiles in Gradle, look no further than this blog post.
Filtering resources
Maven has a phase called process-resources
that has the goal resources:resources
bound to it by default.
This gives the build author an opportunity to perform variable substitution on various files, such as web resources, packaged properties files, etc.
The Java plugin for Gradle provides a processResources
task to do the same thing.
This is a ProcessResources task that copies files from the configured resources directory — src/main/resources
by default — to an output directory.
And as with any ProcessResources
or Copy
task, you can configure it to perform file filtering, renaming, and content filtering.
As an example, here’s a configuration that treats the source files as Groovy SimpleTemplateEngine
templates, providing version
and buildNumber
properties to those templates:
tasks {
processResources {
expand("version" to version, "buildNumber" to currentBuildNumber)
}
}
processResources {
expand(version: version, buildNumber: currentBuildNumber)
}
See the API docs for CopySpec to see all the options available to you.
Configuring integration tests
Many Maven builds incorporate integration tests of some sort, which Maven supports through an extra set of phases: pre-integration-test
, integration-test
, post-integration-test
, and verify
.
It also uses the Failsafe plugin in place of Surefire so that failed integration tests don’t automatically fail the build (because you may need to clean up resources, such as a running application server).
This behavior is easy to replicate in Gradle with source sets, as explained in our chapter on Testing in Java & JVM projects. You can then configure a clean-up task, such as one that shuts down a test server for example, to always run after the integration tests regardless of whether they succeed or fail using Task.finalizedBy().
If you really don’t want your integration tests to fail the build, then you can use the Test.ignoreFailures setting described in the Test execution section of the Java testing chapter.
Source sets also give you a lot of flexibility on where you place the source files for your integration tests.
You can easily keep them in the same directory as the unit tests or, more preferably, in a separate source directory like src/integTest/java
.
To support other types of tests, you just add more source sets and Test tasks!
Migrating common plugins
Maven and Gradle share a common approach of extending the build through plugins. Although the plugin systems are very different beneath the surface, they share many feature-based plugins, such as:
-
Shade/Shadow
-
Jetty
-
Checkstyle
-
JaCoCo
-
AntRun (see further down)
Why does this matter? Because many plugins rely on standard Java conventions, so migration is just a matter of replicating the configuration of the Maven plugin in Gradle. As an example, here’s a simple Maven Checkstyle plugin configuration:
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.17</version>
<executions>
<execution>
<id>validate</id>
<phase>validate</phase>
<configuration>
<configLocation>checkstyle.xml</configLocation>
<encoding>UTF-8</encoding>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
<linkXRef>false</linkXRef>
</configuration>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
...
Everything outside of the configuration block can safely be ignored when migrating to Gradle. In this case, the corresponding Gradle configuration looks like the following:
checkstyle {
config = resources.text.fromFile("checkstyle.xml", "UTF-8")
isShowViolations = true
isIgnoreFailures = false
}
checkstyle {
config = resources.text.fromFile('checkstyle.xml', 'UTF-8')
showViolations = true
ignoreFailures = false
}
The Checkstyle tasks are automatically added as dependencies of the check
task, which also includes test
.
If you want to ensure that Checkstyle runs before the tests, then just specify an ordering with the mustRunAfter() method:
checkstyle
task runstasks {
test {
mustRunAfter(checkstyleMain, checkstyleTest)
}
}
test.mustRunAfter checkstyleMain, checkstyleTest
As you can see, the Gradle configuration is often much shorter than the Maven equivalent. You also have a much more flexible execution model since you are no longer constrained by Maven’s fixed phases.
While migrating a project from Maven, don’t forget about source sets. These often provide a more elegant solution for handling integration tests or generated sources than Maven can provide, so you should factor them into your migration plans.
Ant goals
Many Maven builds rely on the AntRun plugin to customize the build without the overhead of implementing a custom Maven plugin.
Gradle has no equivalent plugin because Ant is a first-class citizen in Gradle builds, via the ant
object.
For example, you can use Ant’s Echo task like this:
tasks.register("sayHello") {
doLast {
ant.withGroovyBuilder {
"echo"("message" to "Hello!")
}
}
}
tasks.register('sayHello') {
doLast {
ant.echo message: 'Hello!'
}
}
Even Ant properties and filesets are supported natively. To learn more, see Using Ant from Gradle.
Tip
|
It may be simpler and cleaner to just create custom task types to replace the work that Ant is doing for you. You can then more readily benefit from incremental build and other useful Gradle features. |
Understanding which plugins you don’t need
It’s worth remembering that Gradle builds are typically easier to extend and customize than Maven ones. In this context, that means you may not need a Gradle plugin to replace a Maven one. For example, the Maven Enforcer plugin allows you to control dependency versions and environmental factors, but these things can easily be configured in a normal Gradle build script.
Dealing with uncommon and custom plugins
You may come across Maven plugins that have no counterpart in Gradle, particularly if you or someone in your organisation has written a custom plugin. Such cases rely on you understanding how Gradle (and potentially Maven) works, because you will usually have to write your own plugin.
For the purposes of migration, there are two key types of Maven plugins:
-
Those that use the Maven project object.
-
Those that don’t.
Why is this important? Because if you use one of the latter, you can trivially reimplement it as a custom Gradle task type. Simply define task inputs and outputs that correspond to the mojo parameters and convert the execution logic into a task action.
If a plugin depends on the Maven project, then you will have to rewrite it. Don’t start by considering how the Maven plugin works, but look at what problem it is trying to solve. Then try to work out how to solve that problem in Gradle. You’ll probably find that the two build models are different enough that "transcribing" Maven plugin code into a Gradle plugin just won’t be effective. On the plus side, the plugin is likely to be much easier to write than the original Maven one because Gradle has a much richer build model and API.
If you do need to implement custom logic, either via build scripts or plugins, check out the Guides related to plugin development.
Also be sure to familiarize yourself with Gradle’s Groovy DSL Reference, which provides comprehensive documentation on the API that you’ll be working with.
It details the standard configuration blocks (and the objects that back them), the core types in the system (Project
, Task
, etc.), and the standard set of task types.
The main entry point is the Project interface as that’s the top-level object that backs the build scripts.
Further reading
This chapter has covered the major topics that are specific to migrating Maven builds to Gradle. All that remain are a few other areas that may be useful during or after a migration:
-
Learn how to configure Gradle’s build environment, including the JVM settings used to run it
-
Learn how to structure your builds effectively
-
Configure Gradle’s logging and use it from your builds
As a final note, this guide has only touched on a few of Gradle’s features and we encourage you to learn about the rest from the other chapters of the user manual and from our step-by-step samples.
Migrating Builds From Apache Ant
Apache Ant is a build tool with a long history in the Java world that is still widely used, albeit by a decreasing number of teams. While flexible, it lacks conventions and many of the powerful features that Gradle can provide. Migrating to Gradle is worthwhile so that your builds can become slimmer, simpler and faster, while still retaining the flexibility you enjoy with Ant. You’ll also benefit from robust support for multi-project builds and easy-to-use, flexible dependency management.
The biggest challenge in migrating from Ant to Gradle is that there is no such thing as a standard Ant build. That makes it difficult to provide specific instructions. Fortunately, Gradle has some great integration features with Ant that can make the process relatively smooth. And even migrating from Ivy-based dependency management isn’t particularly hard because Gradle has a similar model based on dependency configurations that works with Ivy-compatible repositories.
We will start by outlining the things you should consider at the outset of migrating a build from Ant to Gradle and offer some general guidelines on how to proceed.
General guidelines
When you undertake to migrate a build from Ant to Gradle, you should keep in mind the nature of both what you already have and where you would like to end up. Do you want a Gradle build that mirrors the structure of the existing Ant build? Or do you want to move to something that is more idiomatic to Gradle? What are the main benefits you are looking for?
To understand the implications, consider the two extreme endpoints that you could aim for:
-
An imported build via
ant.importBuild()
This approach is quick, simple and works for many Ant-based builds. You end up with a build that’s effectively identical to the original Ant build, except your Ant targets become Gradle tasks. Even the dependencies between targets are retained.
The downside is that you’re still using the Ant build, which you must continue to maintain. You also lose the advantages of Gradle’s conventions, many of its plugins, its dependency management, and so on. You can still enhance the build with incremental build information, but it’s more effort than would be the case for a normal Gradle build.
-
An idiomatic Gradle build
If you want to future proof your build, this is where you want to end up. Making use of Gradle’s conventions and plugins will result in a smaller, easier-to-maintain build, with a structure that is familiar to many Java developers. You will also find it easier to take advantage of Gradle’s power features to improve build performance.
The main downside is the extra work required to perform the migration, particularly if the existing build is complex and has many inter-project dependencies. But such builds often benefit the most from a switch to idomatic Gradle. In addition, Gradle provides many features that can ease the migration, such as the ability to use core and custom Ant tasks directly from a Gradle build.
You ideally want to end up somewhere close to the second option in the long term, but you don’t have to get there in one fell swoop.
What follows is a series of steps to help you decide the approach you want to take and how to go about it:
-
Keep the old Ant build and new Gradle build side by side
You know the Ant build works, so you should keep it until you are confident that the Gradle build produces all the same artifacts and otherwise does what you need. This also means that users can try the Gradle build without getting a new copy of the source tree.
Don’t try to change the directory and file structure of the build until after you’re ready to make the switch.
-
Develop a mechanism to verify that the two builds produce the same artifacts
This is a vitally important step to ensure that your deployments and tests don’t break. Even small changes, such as the contents of a manifest file in a JAR, can cause problems. If your Gradle build produces the same output as the Ant build, this will give you and others confidence in switching over and make it easier to implement the big changes that will provide the greatest benefits.
-
Decide whether you have a multi-project build or not
Multi-project builds are generally harder to migrate and require more work than single-project ones. We have provided some dedicated advice to help with the process in the Migrating multi-project builds section.
-
Work out what plugins to use for each project
We expect that the vast majority of Ant builds are for JVM-based projects, for which there are a wealth of plugins that provide a lot of the functionality you need. Not only are there the core plugins that come packaged with Gradle, but you can also find many useful plugins on the Plugin Portal.
Even if the Java Plugin or one of its derivatives (such as the Java Library Plugin) aren’t a good match for your build, you should at least consider the Base Plugin for its lifecycle tasks.
-
Import the Ant build or create a Gradle build from scratch
This step very much depends on the requirements of your build. If a selection of Gradle plugins can do the vast majority of the work your Ant build does, then it probably makes sense to create a fresh Gradle build script that doesn’t depend on the Ant build and either implements the missing pieces itself or utilizes existing Ant tasks.
The alternative approach is to import the Ant build into the Gradle build script and gradually replace the Ant build functionality. This allows you to have a working Gradle build at each stage, but it requires a bit of work to get the Gradle tasks working properly with the Ant ones. You can learn more about this approach in Working with an imported build.
-
Configure your build for the existing directory and file structure
Gradle makes use of conventions to eliminate much of the boilerplate associated with older builds and to make it easier for users to work with new builds once they are familiar with those conventions. But that doesn’t mean you have to follow them.
Gradle provides many configuration options that allow for a good degree of customization. Those options are typically made available through the plugins that provide the conventions. For example, the standard source directory structure for production Java code —
src/main/java
— is provided by the Java Plugin, which allows you to configure a different source path. Many paths can be modified via properties on the Project object. -
Migrate to standard Gradle conventions if you wish
Once you’re confident that the Gradle build is producing the same artifacts and other resources as the Ant build, you can consider migrating to the standard conventions, such as for source directory paths. Doing so will allow you to remove the extra configuration that was required to override those conventions. New team members will also find it easier to work with the build after the change.
It’s up to you to decide whether this step is worth the time, energy and potential disruption that it might incur, which in turn depends on your specific build and team.
The rest of the chapter covers some common scenarios you will likely deal with during the migration, such as dependency management and working with Ant tasks.
Working with an imported build
Warning
|
Importing an Ant build is not supported with the configuration cache. You need to complete the conversion to Gradle to get the benefits of caching. |
The first step of many migrations will involve importing an Ant build using ant.importBuild()
. If you do that, how do you then move towards a standard Gradle build without replacing everything at once?
The important thing to remember is that the Ant targets become real Gradle tasks, meaning you can do things like modify their task dependencies, attach extra task actions, and so on. This allows you to substitute native Gradle tasks for the equivalent Ant ones, maintaining any links to other existing tasks.
As an example, imagine that you have a Java library project that you want to migrate from Ant to Gradle. The Gradle build script has the line that imports the Ant build and now want to use the standard Gradle mechanism for compiling the Java source files. However, you want to keep using the existing package
task that creates the library’s JAR file.
In diagrammatic form, the scenario looks like the following, where each box represents a target/task:

The idea is to substitute the standard Gradle compileJava
task for the Ant build
task. There are several steps involved in this substitution:
-
Applying the Java Library Plugin
This provides the
compileJava
task shown in the diagram. -
Renaming the old
build
taskThe name
build
conflicts with the standardbuild
task provided by the Base Plugin (via the Java Library Plugin). -
Configuring the compilation to use the existing directory structure
There’s a good chance the Ant build does not conform to the standard Gradle directory structure, so you need to tell Gradle where to find the source files and where to place the compiled classes so
package
can find them. -
Updating task dependencies
compileJava
must depend onprepare
,package
must depend oncompileJava
rather thanant_build
, andassemble
must depend onpackage
rather than the standard Gradlejar
task.
Applying the plugin is as simple as inserting a plugins {}
block at the beginning of the Gradle build script, i.e. before ant.importBuild()
. Here’s how to apply the Java Library Plugin:
plugins {
`java-library`
}
plugins {
id 'java-library'
}
To rename the build
task, use the variant of AntBuilder.importBuild() that accepts a transformer, like this:
ant.importBuild("build.xml") { oldTargetName ->
if (oldTargetName == "build") "ant_build" else oldTargetName // (1)
}
ant.importBuild('build.xml') { String oldTargetName ->
return oldTargetName == 'build' ? 'ant_build' : oldTargetName // (1)
}
-
Renames the
build
target toant_build
and leaves all other targets unchanged
Configuring a different path for the sources is described in the Building Java & JVM projects chapter, while you can change the output directory for the compiled classes in a similar way.
Let’s say the original Ant build stores these paths in Ant properties, src.dir
for the Java source files and classes.dir
for the output. Here’s how you would configure Gradle to use those paths:
sourceSets {
main {
java.setSrcDirs(listOf(ant.properties["src.dir"]))
java.destinationDirectory.set(file(ant.properties["classes.dir"] ?: "$buildDir/classes"))
}
}
sourceSets {
main {
java {
srcDirs = [ ant.properties['src.dir'] ]
destinationDirectory.set(file(ant.properties['classes.dir']))
}
}
}
You should eventually aim to switch the standard directory structure for your type of project if possible and then you’ll be able to remove this customization.
The last step is also straightforward and involves using the Task.dependsOn property and Task.dependsOn() method to detach and link tasks. The property is appropriate for replacing dependencies, while the method is the preferred way to add to the existing dependencies.
Here is the required task dependency configuration required by the example scenario, which should come after the Ant build import:
tasks {
compileJava {
dependsOn("prepare") // (1)
}
named("package") {
setDependsOn(listOf(compileJava)) // (2)
}
assemble {
setDependsOn(listOf("package")) // (3)
}
}
compileJava.dependsOn 'prepare' // (1)
tasks.named('package') { dependsOn = [ 'compileJava' ] } // (2)
assemble.dependsOn = [ 'package' ] // (3)
-
Makes compilation depend on the
prepare
task -
Detaches
package
from theant_build
task and makes it depend oncompileJava
-
Detaches
assemble
from the standard Gradlejar
task and makes it depend onpackage
instead
That’s it! These four steps will successfully replace the old Ant compilation with the Gradle implementation. Even this small migration will be a big help because you’ll be able to take advantage of Gradle’s incremental Java compilation for faster builds.
Tip
|
This is just a demonstration of how to go about performing a migration in stages. It may make more sense to include resource processing — like with properties files — and packaging with the compilation in this stage, since all three aspects are well integrated in Gradle. |
One important question you will have to ask yourself is how many tasks to migrate in each stage. The larger the chunks you can migrate in one go the better, but this must be offset against how many custom steps within the Ant build will be affected by the changes.
For example, if the Ant build follows a fairly standard approach for compilation, static resources, packaging and unit tests, then it is probably worth migrating all those together. But if the build performs some extra processing on the compiled classes, or does something unique when processing the static resources, it is probably worth splitting those tasks into separate stages.
Managing dependencies
Ant builds typically take one of two approaches to dealing with binary dependencies (such as libraries):
-
Storing them with the project in a local "lib" directory
-
Using Apache Ivy to manage them
They each require a different technique for the migration to Gradle, but you will find the process straightforward in either case. We look at the details of each scenario in the following sections.
Serving dependencies from a directory
When you are attempting to migrate a build that stores its dependencies on the filesystem, either locally or on the network, you should consider whether you want to eventually move to managed dependencies using remote repositories. That’s because you can incorporate filesystem dependencies into a Gradle build in one of two ways:
-
Define a flat-directory repository and use standard dependency declarations
-
Attach the files directly to the appropriate dependency configurations (file dependencies)
It’s easier to migrate to managed dependencies served from Maven- or Ivy-compatible repositories if you take the first approach, but doing so requires all your files to conform to the naming convention "<moduleName>-<version>.<extension>".
Note
|
If you store your dependencies in the standard Maven repository layout — |
To demonstrate the two techniques, consider a project that has the following library JARs in its libs
directory:
libs ├── our-custom.jar ├── awesome-framework-2.0.jar └── utility-library-1.0.jar
The file our-custom.jar
lacks a version number, so it has to be added as a file dependency.
But the other two JARs match the required naming convention and so can be declared as normal module dependencies that are retrieved from a flat-directory repository.
The following sample build script demonstrates how you can incorporate all of these libraries into a build:
repositories {
flatDir {
name = "libs dir"
dir(file("libs")) // (1)
}
}
dependencies {
implementation(files("libs/our-custom.jar")) // (2)
implementation(":awesome-framework:2.0") // (3)
implementation(":utility-library:1.0") // (3)
}
repositories {
flatDir {
name = 'libs dir'
dir file('libs') // (1)
}
}
dependencies {
implementation files('libs/our-custom.jar') // (2)
implementation ':awesome-framework:2.0' // (3)
implementation ':utility-library:1.0' // (3)
}
-
Specifies the path to the directory containing the JAR files
-
Declares a file dependency for the unversioned JAR
-
Declares dependencies using standard dependency coordinates — note that no group is specified, but each identifier has a leading
:
, implying an empty group
The above sample will add our-custom.jar
, awesome-framework-2.0.jar
and utility-library-1.0.jar
to the implementation
configuration, which is used to compile the project’s code.
Note
|
You can also specify a group in these module dependencies, even though they don’t actually have a group. That’s because the flat-directory repository simply ignores the information. If you then add a normal Maven- or Ivy-compatible repository at a later date, Gradle will preferentially download the module dependencies that are declared with a group from that repository rather than the flat-directory one. |
Migrating Ivy dependencies
Apache Ivy is a standalone dependency management tool that is widely used with Ant. It works in a similar fashion to Gradle. In fact, they both allow you to
-
Define your own configurations
-
Extend configurations from one another
-
Attach dependencies to configurations
-
Resolve dependencies from Ivy-compatible repositories
-
Publish artifacts to Ivy-compatible repositories
The most notable difference is that Gradle has standard configurations for specific types of projects.
For example, the Java Plugin defines configurations like implementation
, testImplementation
and runtimeOnly
.
You can still define your own dependency configurations, though.
This similarity means that it’s usually quite straightforward to migrate from Ivy to Gradle:
-
Transcribe the dependency declarations from your module descriptors into the dependencies {} block of your Gradle build script, ideally using the standard configurations provided by any plugins you apply.
-
Transcribe any configuration declarations from your module descriptors into the configurations {} block of the build script for any custom configurations that can’t be replaced by Gradle’s standard ones.
-
Transcribe the resolvers from your Ivy settings file into the repositories {} block of the build script.
See the chapters on Managing Dependency Configurations, Declaring Dependencies and Declaring Repositories for more information.
Ivy provides several Ant tasks that handle Ivy’s process for fetching dependencies. The basic steps of that process consist of:
-
Configure — applies the configuration defined in the Ivy settings file
-
Resolve — locates the declared dependencies and downloads them to the cache if necessary
-
Retrieve — copies the cached dependencies to another directory
Gradle’s process is similar, but you don’t have to explicitly invoke the first two steps as it performs them automatically. The third step doesn’t happen at all — unless you create a task to do it — because Gradle typically uses the files in the dependency cache directly in classpaths and as the source for assembling application packages.
Let’s look in more detail at how Ivy’s steps map to Gradle:
- Configuration
-
Most of Gradle’s dependency-related configuration is baked into the build script, as you’ve seen with elements like the
dependencies {}
block. Another particularly important configuration element is resolutionStrategy, which can be accessed from dependency configurations. This provides many of the features you might get from Ivy’s conflict managers and is a powerful way to control transitive dependencies and caching.Some Ivy configuration options have no equivalent in Gradle. For example, there are no lock strategies because Gradle ensures that its dependency cache is concurrency safe, period. Nor are there "latest strategies" because it’s simpler to have a reliable, single strategy for conflict resolution. If the "wrong" version is picked, you can easily override it using forced versions or other resolution strategy options.
See the chapter on controlling transitive dependencies for more information on this aspect of Gradle.
- Resolution
-
At the beginning of the build, Gradle will automatically resolve any dependencies that you have declared and download them to its cache. It searches the repositories for those dependencies, with the search order defined by the order in which the repositories are declared.
It’s worth noting that Gradle supports the same dynamic version syntax as Ivy, so you can still use versions like
1.0.+
. You can also use the speciallatest.integration
andlatest.release
labels if you wish. If you decide to use such dynamic and changing dependencies, you can configure the caching behavior for them via resolutionStrategy.You might also want to consider dependency locking if you’re using dynamic and/or changing dependencies. It’s a way to make the build more reliable and allows for reproducible builds.
- Retrieval
-
As mentioned, Gradle does not automatically copy files from the dependency cache. Its standard tasks typically use the files directly. If you want to copy the dependencies to a local directory, you can use a Copy task like this in your build script:
Example 16. Copying dependencies to a local directorybuild.gradle.ktstasks.register<Copy>("retrieveRuntimeDependencies") { into(layout.buildDirectory.dir("libs")) from(configurations.runtimeClasspath) }
build.gradletasks.register('retrieveRuntimeDependencies', Copy) { into layout.buildDirectory.dir('libs') from configurations.runtimeClasspath }
A configuration is also a file collection, hence why it can be used in the
from()
configuration. You can use a similar technique to attach a configuration to a compilation task or one that produces documentation. See the chapter on Working with Files for more examples and information on Gradle’s file API.
Publishing artifacts
Projects that use Ivy to manage dependencies often also use it for publishing JARs and other artifacts to repositories. If you’re migrating such a build, then you’ll be glad to know that Gradle has built-in support for publishing artifacts to Ivy-compatible repositories.
Before you attempt to migrate this particular aspect of your build, read the Publishing chapter to learn about Gradle’s publishing model. That chapter’s examples are based on Maven repositories, but the same model is used for Ivy repositories as well.
The basic migration process looks like this:
-
Apply the Ivy Publish Plugin to your build
-
Configure at least one publication, representing what will be published (including additional artifacts if desired)
Once that’s all done, you’ll be able to generate an Ivy module descriptor for each publication and publish them to one or more repositories.
Let’s say you have defined a publication named "myLibrary" and a repository named "myRepo". Ivy’s Ant tasks would then map to the Gradle tasks like this:
-
<deliver>
→generateDescriptorFileForMyLibraryPublication
-
<publish>
→publishMyLibraryPublicationToMyRepoRepository
There is also a convenient publish
task that publishes all publications to all repositories.
If you’d prefer to limit which publications go to which repositories, check out the relevant section of the Publishing chapter.
Note
|
On dependency versions
Ivy will, by default, automatically replace dynamic versions of dependencies with the resolved "static" versions when it generates the module descriptor. Gradle does not mimic this behavior: declared dependency versions are left unchanged. You can replicate the default Ivy behavior by using the Nebula Ivy Resolved Plugin. Alternatively, you can customize the descriptor file so that it contains the versions you want. |
Dealing with custom Ant tasks
One of the advantages of Ant is that it’s fairly easy to create a custom task and incorporate it into a build. If you have such tasks, then there are two main options for migrating them to a Gradle build:
-
Using the custom Ant task from the Gradle build
-
Rewriting the task as a custom Gradle task type
The first option is usually quick and easy, but not always. And if you want to integrate the task into incremental build, you must use the incremental build runtime API. You also often have to work with Ant paths and filesets, which are clunky.
The second option is preferable in the long term, if you have the time. Gradle task types tend to be simpler than Ant tasks because they don’t have to work with an XML-based interface. You also gain access to Gradle’s rich APIs. Lastly, this approach can make use of the type-safe incremental build API based on typed properties.
Working with files
Ant has many tasks for working with files, most of which have Gradle equivalents. As with other areas of Ant to Gradle migration, you can use those Ant tasks from within your Gradle build. However, we strongly recommend migrating to native Gradle constructs where possible so that the build benefits from:
-
Easier integration with other parts of the build, such as dependency configurations
-
More idiomatic build scripts
That said, it can be convenient to use those Ant tasks that have no direct equivalents, such as <checksum>
and <chown>
. Even then, in the long run it may be better to convert these to native Gradle task types that make use of standard Java APIs or third-party libraries to achieve the same thing.
Here are the most common file-related elements used by Ant builds, along with the Gradle equivalents:
You can see several examples of Gradle’s file API and learn more about it in the Working with Files chapter.
Note
|
On paths and filesets
Ant makes use of the concepts of path-like structures and filesets to enable users to work with collections of files and directories. Gradle has a simpler, more powerful model based on FileCollections and FileTrees that can be treated as objects from within the build. Both types allow filtering based on Ant’s glob syntax, e.g. You can still construct Ant paths and filesets from within your build via the |
Migrating Ant properties
Ant makes use of a properties map to store values that can be reused throughout the build. The big downsides to this approach are that property values are all strings and the properties themselves behave like global variables.
Tip
|
Interacting with Ant properties in Gradle
Sometimes you will want to make use of an Ant task directly from your Gradle build and that task requires one or more Ant properties to be set. If that’s the case, you can easily set those properties via the |
Gradle does use something similar in the form of project properties, which are a reasonable way to parameterize a build. These can be set from the command line, in a gradle.properties
file, or even via specially named system properties and environment variables.
If you have existing Ant properties files, you can copy their contents into the project’s gradle.properties
file. Just be aware of two important points:
-
Properties set in
gradle.properties
do not override extra project properties defined in the build script with the same name -
Imported Ant tasks will not automatically "see" the Gradle project properties — you must copy them into the Ant properties map for that to happen
Another important factor to understand is that a Gradle build script works with an object-oriented API and it’s often best to use the properties of tasks, source sets and other objects where possible. For example, this build script fragment creates tasks for packaging Javadoc documentation as a JAR and unpacking it, linking tasks via their properties:
val tmpDistDir = layout.buildDirectory.dir("dist")
tasks.register<Jar>("javadocJarArchive") {
from(tasks.javadoc) // (1)
archiveClassifier.set("javadoc")
}
tasks.register<Copy>("unpackJavadocs") {
from(zipTree(tasks.named<Jar>("javadocJarArchive").get().archiveFile)) // (2)
into(tmpDistDir) // (3)
}
def tmpDistDir = layout.buildDirectory.dir('dist')
tasks.register('javadocJarArchive', Jar) {
from javadoc // (1)
archiveClassifier = 'javadoc'
}
tasks.register('unpackJavadocs', Copy) {
from zipTree(javadocJarArchive.archiveFile) // (2)
into tmpDistDir // (3)
}
-
Packages all
javadoc
's output files — equivalent tofrom javadoc.destinationDir
-
Uses the location of the Javadoc JAR held by the
javadocJar
task -
Uses an project property called
tmpDistDir
to define the location of the 'dist' directory
As you can see from the example with tmpDistDir
, there is often still a need to define paths and the like through properties, which is why Gradle also provides extra properties that can be attached to the project, tasks and some other types of objects.
Migrating multi-project builds
Multi-project builds are a particular challenge to migrate because there is no standard approach in Ant for either structuring them or handling inter-project dependencies. Most of them likely use the <ant>
task in some way, but that’s about all that one can say.
Fortunately, Gradle’s multi-project support can handle fairly diverse project structures and it provides much more robust and helpful support than Ant for constructing and maintaining multi-project builds. The ant.importBuild()
method also handles <ant>
and <antcall>
tasks transparently, which allows for a phased migration.
We will suggest one process for migration here and hope that it either works for your case or at least gives you some ideas. It breaks down like this:
-
Start by learning how Gradle configures multi-project builds.
-
Create a Gradle build script in each project of the build, setting their contents to this line:
ant.importBuild 'build.xml'
ant.importBuild("build.xml")
Replace
build.xml
with the path to the actual Ant build file that corresponds to the project. If there is no corresponding Ant build file, leave the Gradle build script empty. Your build may not be suitable in that case for this migration approach, but continue with these steps to see whether there is still a way to do a phased migration. -
Create a settings file that includes all the projects that now have a Gradle build script.
-
Implement inter-project dependencies.
Some projects in your multi-project build will depend on artifacts produced by one or more other projects in that build. Such projects need to ensure that those projects they depend on have produced their artifacts and that they know the paths to those artifacts.
Ensuring the production of the required artifacts typically means calling into other projects' builds via the
<ant>
task. This unfortunately bypasses the Gradle build, negating any changes you make to the Gradle build scripts. You will need to replace targets that use<ant>
tasks with Gradle task dependencies.For example, imagine you have a web project that depends on a "util" library that’s part of the same build. The Ant build file for "web" might have a target like this:
web/build.xml<target name="buildRequiredProjects"> <ant dir="${root.dir}/util" target="build"/> <!--(1)--> </target>
-
root.dir
would have to be defined by the build
This can be replaced by an inter-project task dependency in the corresponding Gradle build script, as demonstrated in the following example that assumes the "web" project’s "compile" task is the thing that requires "util" to be built beforehand:
web/build.gradle.ktsant.importBuild("build.xml") tasks { named<Task>("compile") { setDependsOn(listOf(":util:build")) } }
web/build.gradleant.importBuild 'build.xml' compile.dependsOn = [ ':util:build' ]
This is not as robust or powerful as Gradle’s project dependencies, but it solves the immediate problem without big changes to the build. Just be careful to remove or override any dependencies on tasks that delegate to other subprojects, like the
buildRequiredProjects
task. -
-
Identify the projects that have no dependencies on other projects and migrate them to idiomatic Gradle builds scripts.
Just follow the advice in the rest of this guide to migrate individual project builds. As mentioned elsewhere, you should ideally use Gradle standard plugins where possible. This may mean that you need to add an extra copy task to each build that copies the generated artifacts to the location expected by the rest of the Ant builds.
-
Migrate projects as and when they depend solely on projects with fully migrated Gradle builds.
At this point, you should be able to switch to using proper project dependencies attached to the appropriate dependency configurations.
-
Clean up projects once no part of the Ant build depends on them.
We mentioned in step 5 that you might need to add copy tasks to satisfy the requirements of dependent Ant builds. Once those builds have been migrated, such build logic will no longer be needed and should be removed.
At the end of the process you should have a Gradle build that you are confident works as it should, with much less build logic than before.
Further reading
This chapter has covered the major topics that are specific to migrating Ant builds to Gradle. All that remain are a few other areas that may be useful during or after a migration:
-
Learn how to configure Gradle’s build environment, including the JVM settings used to run it
-
Learn how to structure your builds effectively
-
Configure Gradle’s logging and use it from your builds
As a final note, this guide has only touched on a few of Gradle’s features and we encourage you to learn about the rest from the other chapters of the user manual and from our step-by-step samples.
Running Gradle Builds
Build Environment
Tip
|
Interested in configuring your Build Cache to speed up builds? Register here for our Build Cache training session to learn some of the tips and tricks top engineering teams are using to increase build speed. |
Gradle provides multiple mechanisms for configuring behavior of Gradle itself and specific projects. The following is a reference for using these mechanisms.
When configuring Gradle behavior you can use these methods, listed in order of highest to lowest precedence (first one wins):
-
Command-line flags such as
--build-cache
. These have precedence over properties and environment variables. -
System properties such as
systemProp.http.proxyHost=somehost.org
stored in agradle.properties
file in a root project directory. -
Gradle properties such as
org.gradle.caching=true
that are typically stored in agradle.properties
file in a project directory or in theGRADLE_USER_HOME
. -
Environment variables such as
GRADLE_OPTS
sourced by the environment that executes Gradle.
Aside from configuring Gradle behavior you can configure the build using the same mechanisms and reading the environment from the build logic.
Gradle properties
Gradle provides several options that make it easy to configure the Java process that will be used to execute your build. While it’s possible to configure these in your local environment via GRADLE_OPTS
or JAVA_OPTS
, it is useful to be able to store certain settings like JVM memory configuration and Java home location in version control so that an entire team can work with a consistent environment. To do so, place these settings into a gradle.properties
file committed to your version control system.
The final configuration taken into account by Gradle is a combination of all Gradle properties set on the command line and your gradle.properties
files. If an option is configured in multiple locations, the first one found in any of these locations wins:
-
command line, as set using
-D
. -
gradle.properties
inGRADLE_USER_HOME
directory. -
gradle.properties
in the project’s directory, then its parent project’s directory up to the build’s root directory. -
gradle.properties
in Gradle installation directory.
Note that the location of the Gradle user home may have been changed beforehand via the -Dgradle.user.home
system property passed on the command line.
The following properties can be used to configure the Gradle build environment:
org.gradle.caching=(true,false)
-
When set to true, Gradle will reuse task outputs from any previous build, when possible, resulting in much faster builds. Learn more about using the build cache. By default, the build cache is not enabled.
org.gradle.caching.debug=(true,false)
-
When set to true, individual input property hashes and the build cache key for each task are logged on the console. Learn more about task output caching. Default is
false
. org.gradle.configureondemand=(true,false)
-
Enables incubating configuration on demand, where Gradle will attempt to configure only necessary projects. Default is
false
. org.gradle.console=(auto,plain,rich,verbose)
-
Customize console output coloring or verbosity. Default depends on how Gradle is invoked. See command-line logging for additional details.
org.gradle.continuous.quietperiod=(# of quiet period millis)
-
When using continuous build, Gradle will wait for the quiet period to pass before triggering another build. Any additional changes within this quiet period restart waiting for the quiet period. Default is
250
milliseconds. org.gradle.daemon=(true,false)
-
When set to
true
the Gradle Daemon is used to run the build. Default istrue
, builds will be run using the daemon. org.gradle.daemon.idletimeout=(# of idle millis)
-
Gradle Daemon will terminate itself after specified number of idle milliseconds. Default is
10800000
(3 hours). org.gradle.debug=(true,false)
-
When set to
true
, Gradle will run the build with remote debugging enabled, listening on port 5005. Note that this is the equivalent of adding-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005
to the JVM command line and will suspend the virtual machine until a debugger is attached. Default isfalse
. org.gradle.debug.host=(host address)
-
Specifies the host address to listen on or connect to when debug is enabled. In the server mode on Java 9 and above, passing
*
for the host will make the server listen on all network interfaces. By default, no host address is passed to JDWP, so on Java 9 and above, the loopback address is used, while earlier versions listen on all interfaces. org.gradle.debug.port=(port number)
-
Specifies the port number to listen on when debug is enabled. Default is
5005
. org.gradle.debug.server=(true,false)
-
If set to
true
and debugging is enabled, Gradle will run the build with the socket-attach mode of the debugger. Otherwise, the socket-listen mode is used. Default istrue
. org.gradle.debug.suspend=(true,false)
-
When set to
true
and debugging is enabled, the JVM running Gradle will suspend until a debugger is attached. Default istrue
. org.gradle.java.home=(path to JDK home)
-
Specifies the Java home for the Gradle build process. The value can be set to either a
jdk
orjre
location, however, depending on what your build does, using a JDK is safer. This does not affect the version of Java used to launch the Gradle client VM (see Environment variables). A reasonable default is derived from your environment (JAVA_HOME
or the path tojava
) if the setting is unspecified. org.gradle.jvmargs=(JVM arguments)
-
Specifies the JVM arguments used for the Gradle Daemon. The setting is particularly useful for configuring JVM memory settings for build performance. This does not affect the JVM settings for the Gradle client VM. The default is
-Xmx512m "-XX:MaxMetaspaceSize=384m"
. org.gradle.logging.level=(quiet,warn,lifecycle,info,debug)
-
When set to quiet, warn, lifecycle, info, or debug, Gradle will use this log level. The values are not case sensitive. See Choosing a log level. The
lifecycle
level is the default. org.gradle.logging.stacktrace=(internal,all,full)
-
Specifies whether stacktraces should be displayed as part of the build result upon an exception. See also the --stacktrace command-line option. When set to
internal
, a stacktrace is present in the output only in case of internal exceptions. When set toall
orfull
, a stacktrace is present in the output for all exceptions and build failures. Usingfull
doesn’t truncate the stacktrace, which leads to a much more verbose output. Default isinternal
. org.gradle.parallel=(true,false)
-
When configured, Gradle will fork up to
org.gradle.workers.max
JVMs to execute projects in parallel. To learn more about parallel task execution, see the section on Gradle build performance. Default isfalse
. org.gradle.priority=(low,normal)
-
Specifies the scheduling priority for the Gradle daemon and all processes launched by it. See also performance command-line options. Default is
normal
. org.gradle.vfs.verbose=(true,false)
-
Configures verbose logging when watching the file system. Default is
false
. org.gradle.vfs.watch=(true,false)
-
Toggles watching the file system. When enabled Gradle re-uses information it collects about the file system between builds. Enabled by default on operating systems where Gradle supports this feature.
org.gradle.warning.mode=(all,fail,summary,none)
-
When set to
all
,summary
ornone
, Gradle will use different warning type display. See Command-line logging options for details. Default issummary
. org.gradle.welcome=(never,once)
-
Controls whether Gradle should print a welcome message. If set to never then the welcome message will be suppressed. If set to once then the message is printed once for each new version of Gradle. Default is
once
. org.gradle.workers.max=(max # of worker processes)
-
When configured, Gradle will use a maximum of the given number of workers. See also performance command-line options. Default is number of CPU processors.
The following examples demonstrate how to use Gradle properties.
gradlePropertiesProp=gradlePropertiesValue
gradleProperties.with.dots=gradlePropertiesDottedValue
// Using the API, provides a lazy Provider<String>
println(providers.gradleProperty("gradlePropertiesProp").get())
// Using Kotlin delegated properties on `settings`
val gradlePropertiesProp: String by settings
println(gradlePropertiesProp)
// Using the API, provides a lazy Provider<String>
println(providers.gradleProperty("gradlePropertiesProp").get())
// Using Kotlin delegated properties on `project`
val gradlePropertiesProp: String by project
println(gradlePropertiesProp)
// Using the API, provides a lazy Provider<String>
println providers.gradleProperty('gradlePropertiesProp').get()
// Using Groovy dynamic names
println gradlePropertiesProp
println settings.gradlePropertiesProp
// Using Groovy dynamic array notation on `settings`
println settings['gradlePropertiesProp']
// Using the API, provides a lazy Provider<String>
println providers.gradleProperty('gradlePropertiesProp').get()
// Using Groovy dynamic names
println gradlePropertiesProp
println project.gradlePropertiesProp
// Using Groovy dynamic array notation on `project`
println project['gradlePropertiesProp']
The Kotlin delegated properties are part of the Gradle Kotlin DSL.
You need to explicitly specify the type as String
.
If you need to branch depending on the presence of the property, you can also use String?
and check for null
.
Note that if a Gradle property has a dot in its name, using the dynamic Groovy names is not possible. You have to use the API or the dynamic array notation instead.
tasks.register<PrintValue>("printProperty") {
// Using the API, provides a lazy Provider<String> wired to a task input
inputValue.set(providers.gradleProperty("gradlePropertiesProp"))
}
tasks.register('printProperty', PrintValue) {
// Using the API, provides a lazy Provider<String> wired to a task input
inputValue = providers.gradleProperty('gradlePropertiesProp')
}
$ gradle -DgradlePropertiesProp=commandLineValue
Note that initialization scripts can’t read Gradle properties directly.
The earliest Gradle properties can be read in initialization scripts is on settingsEvaluated {}
:
settingsEvaluated {
// Using the API, provides a lazy Provider<String>
println(providers.gradleProperty("gradlePropertiesProp").get())
// Using Kotlin delegated properties on `settings`
val gradlePropertiesProp: String by this
println(gradlePropertiesProp)
}
settingsEvaluated { settings ->
// Using the API, provides a lazy Provider<String>
println settings.providers.gradleProperty('gradlePropertiesProp').get()
// Using Groovy dynamic names
println settings.gradlePropertiesProp
// Using Groovy dynamic array notation on `settings`
println settings['gradlePropertiesProp']
}
Properties declared in a gradle.properties
file present in a subproject directory are only available to that project and its children.
System properties
Using the -D
command-line option, you can pass a system property to the JVM which runs Gradle. The -D
option of the gradle
command has the same effect as the -D
option of the java
command.
You can also set system properties in gradle.properties
files with the prefix systemProp.
systemProp.gradle.wrapperUser=myuser systemProp.gradle.wrapperPassword=mypassword
The following system properties are available. Note that command-line options take precedence over system properties.
gradle.wrapperUser=(myuser)
-
Specify user name to download Gradle distributions from servers using HTTP Basic Authentication. Learn more in Authenticated wrapper downloads.
gradle.wrapperPassword=(mypassword)
-
Specify password for downloading a Gradle distribution using the Gradle wrapper.
gradle.user.home=(path to directory)
-
Specify the Gradle user home directory.
https.protocols
-
Specify the supported TLS versions in a comma separated format. For example:
TLSv1.2,TLSv1.3
.
In a multi project build, “systemProp.
” properties set in any project except the root will be ignored. That is, only the root project’s gradle.properties
file will be checked for properties that begin with the “systemProp.
” prefix.
The following examples demonstrate how to use System properties.
systemProp.system=gradlePropertiesValue
// Using the Java API
println(System.getProperty("system"))
// Using the Java API
println(System.getProperty("system"))
// Using the Gradle API, provides a lazy Provider<String>
println(providers.systemProperty("system").get())
// Using the Java API
println(System.getProperty("system"))
// Using the Gradle API, provides a lazy Provider<String>
println(providers.systemProperty("system").get())
// Using the Java API
println System.getProperty('system')
// Using the Java API
println System.getProperty('system')
// Using the Gradle API, provides a lazy Provider<String>
println providers.systemProperty('system').get()
// Using the Java API
println System.getProperty('system')
// Using the Gradle API, provides a lazy Provider<String>
println providers.systemProperty('system').get()
tasks.register<PrintValue>("printProperty") {
// Using the Gradle API, provides a lazy Provider<String> wired to a task input
inputValue.set(providers.systemProperty("system"))
}
tasks.register('printProperty', PrintValue) {
// Using the Gradle API, provides a lazy Provider<String> wired to a task input
inputValue = providers.systemProperty('system')
}
$ gradle -Dsystem=commandLineValue
Environment variables
The following environment variables are available for the gradle
command. Note that command-line options and system properties take precedence over environment variables.
GRADLE_OPTS
-
Specifies JVM arguments to use when starting the Gradle client VM. The client VM only handles command line input/output, so it is rare that one would need to change its VM options. The actual build is run by the Gradle daemon, which is not affected by this environment variable.
GRADLE_USER_HOME
-
Specifies the Gradle user home directory (which defaults to
<home directory of the current user>/.gradle
if not set). JAVA_HOME
-
Specifies the JDK installation directory to use for the client VM. This VM is also used for the daemon, unless a different one is specified in a Gradle properties file with
org.gradle.java.home
.
The following examples demonstrate how to use environment variables.
// Using the Java API
println(System.getenv("ENVIRONMENTAL"))
// Using the Java API
println(System.getenv("ENVIRONMENTAL"))
// Using the Gradle API, provides a lazy Provider<String>
println(providers.environmentVariable("ENVIRONMENTAL").get())
// Using the Java API
println(System.getenv("ENVIRONMENTAL"))
// Using the Gradle API, provides a lazy Provider<String>
println(providers.environmentVariable("ENVIRONMENTAL").get())
// Using the Java API
println System.getenv('ENVIRONMENTAL')
// Using the Java API
println System.getenv('ENVIRONMENTAL')
// Using the Gradle API, provides a lazy Provider<String>
println providers.environmentVariable('ENVIRONMENTAL').get()
// Using the Java API
println System.getenv('ENVIRONMENTAL')
// Using the Gradle API, provides a lazy Provider<String>
println providers.environmentVariable('ENVIRONMENTAL').get()
tasks.register<PrintValue>("printValue") {
// Using the Gradle API, provides a lazy Provider<String> wired to a task input
inputValue.set(providers.environmentVariable("ENVIRONMENTAL"))
}
tasks.register('printValue', PrintValue) {
// Using the Gradle API, provides a lazy Provider<String> wired to a task input
inputValue = providers.environmentVariable('ENVIRONMENTAL')
}
Project properties
Project properties are available on the Project object.
They can be set from the command line using the -P
/ --project-prop
environment option.
$ gradle -PgradlePropertiesProp=commandLineValue
Gradle can also set project properties when it sees specially-named system properties or environment variables. If the environment variable name looks like ORG_GRADLE_PROJECT_prop=somevalue
, then Gradle will set a prop
property on your project object, with the value of somevalue
. Gradle also supports this for system properties, but with a different naming pattern, which looks like org.gradle.project.prop
. Both of the following will set the foo
property on your Project object to "bar"
.
org.gradle.project.foo=bar
ORG_GRADLE_PROJECT_foo=bar
This feature is useful when you don’t have admin rights to a continuous integration server, and you need to set property values that should not be easily visible. Since you cannot use the -P
option in that scenario, nor change the system-level configuration files, the correct strategy is to change the configuration of your continuous integration build job, adding an environment variable setting that matches an expected pattern. This won’t be visible to normal users on the system.
The following examples demonstrate how to use project properties.
// Querying the presence of a project property
if (hasProperty("myProjectProp")) {
// Accessing the value, throws if not present
println(property("myProjectProp"))
}
// Accessing the value of a project property, null if absent
println(findProperty("myProjectProp"))
// Accessing the Map<String, Any?> of project properties
println(properties["myProjectProp"])
// Using Kotlin delegated properties on `project`
val myProjectProp: String by project
println(myProjectProp)
// Querying the presence of a project property
if (hasProperty('myProjectProp')) {
// Accessing the value, throws if not present
println property('myProjectProp')
}
// Accessing the value of a project property, null if absent
println findProperty('myProjectProp')
// Accessing the Map<String, ?> of project properties
println properties['myProjectProp']
// Using Groovy dynamic names, throws if not present
println myProjectProp
The Kotlin delegated properties are part of the Gradle Kotlin DSL.
You need to explicitly specify the type as String
.
If you need to branch depending on the presence of the property, you can also use String?
and check for null
.
Note that if a Project property has a dot in its name, using the dynamic Groovy names is not possible. You have to use the API or the dynamic array notation instead.
tasks.register<PrintValue>("printValue") {
// Eagerly accessing the value of a project property, set as a task input
inputValue.set(project.property("myProjectProp").toString())
}
tasks.register('printValue', PrintValue) {
// Eagerly accessing the value of a project property, set as a task input
inputValue = project.property('myProjectProp')
}
Note
|
If a project property is referenced but does not exist, an exception will be thrown and the build will fail. You should check for existence of optional project properties before you access them using the Project.hasProperty(java.lang.String) method. |
Configuring JVM memory
You can adjust JVM options for Gradle in the following ways:
The org.gradle.jvmargs
Gradle property controls the VM running the build. It defaults to -Xmx512m "-XX:MaxMetaspaceSize=384m"
org.gradle.jvmargs=-Xmx2g -XX:MaxMetaspaceSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
The JAVA_OPTS
environment variable controls the command line client, which is only used to display console output. It defaults to -Xmx64m
JAVA_OPTS="-Xmx64m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8"
Note
|
There is one case where the client VM can also serve as the build VM: If you deactivate the Gradle Daemon and the client VM has the same settings as required for the build VM, the client VM will run the build directly. Otherwise the client VM will fork a new VM to run the actual build in order to honor the different settings. |
Certain tasks, like the test
task, also fork additional JVM processes. You can configure these through the tasks themselves.
They all use -Xmx512m
by default.
plugins {
java
}
tasks.withType<JavaCompile>().configureEach {
options.compilerArgs = listOf("-Xdoclint:none", "-Xlint:none", "-nowarn")
}
plugins {
id 'java'
}
tasks.withType(JavaCompile).configureEach {
options.compilerArgs += ['-Xdoclint:none', '-Xlint:none', '-nowarn']
}
See other examples in the Test API documentation and test execution in the Java plugin reference.
Build scans will tell you information about the JVM that executed the build when you use the --scan
option.
Configuring a task using project properties
It’s possible to change the behavior of a task based on project properties specified at invocation time.
Suppose you’d like to ensure release builds are only triggered by CI. A simple way to handle this is through an isCI
project property.
tasks.register("performRelease") {
val isCI = providers.gradleProperty("isCI")
doLast {
if (isCI.isPresent) {
println("Performing release actions")
} else {
throw InvalidUserDataException("Cannot perform release outside of CI")
}
}
}
tasks.register('performRelease') {
def isCI = providers.gradleProperty("isCI")
doLast {
if (isCI.present) {
println("Performing release actions")
} else {
throw new InvalidUserDataException("Cannot perform release outside of CI")
}
}
}
$ gradle performRelease -PisCI=true --quiet Performing release actions
Accessing the web through a proxy
Configuring a proxy (for downloading dependencies, for example) is done via standard JVM system properties. These properties can be set directly in the build script; for example, setting the HTTP proxy host would be done with System.setProperty('http.proxyHost', 'www.somehost.org')
. Alternatively, the properties can be specified in gradle.properties.
systemProp.http.proxyHost=www.somehost.org systemProp.http.proxyPort=8080 systemProp.http.proxyUser=userid systemProp.http.proxyPassword=password systemProp.http.nonProxyHosts=*.nonproxyrepos.com|localhost
There are separate settings for HTTPS.
systemProp.https.proxyHost=www.somehost.org systemProp.https.proxyPort=8080 systemProp.https.proxyUser=userid systemProp.https.proxyPassword=password # NOTE: this is not a typo. systemProp.http.nonProxyHosts=*.nonproxyrepos.com|localhost
There are separate settings for SOCKS.
systemProp.socksProxyHost=www.somehost.org systemProp.socksProxyPort=1080 systemProp.java.net.socks.username=userid systemProp.java.net.socks.password=password
You may need to set other properties to access other networks. Here are 2 references that may be helpful:
NTLM Authentication
If your proxy requires NTLM authentication, you may need to provide the authentication domain as well as the username and password. There are 2 ways that you can provide the domain for authenticating to a NTLM proxy:
-
Set the
http.proxyUser
system property to a value likedomain/username
. -
Provide the authentication domain via the
http.auth.ntlm.domain
system property.
The Gradle Daemon
A daemon is a computer program that runs as a background process, rather than being under the direct control of an interactive user.
Gradle runs on the Java Virtual Machine (JVM) and uses several supporting libraries with non-trivial initialization time. Startup can be slow. The Gradle Daemon solves this problem.
The Daemon is a long-lived background process that reduces the time it takes to run a build. The Daemon reduces build times by:
-
caching project information across builds
-
running in the background so every Gradle build doesn’t have to wait for JVM startup
-
benefiting from continuous runtime optimization in the JVM
-
watching the file system to calculate exactly what needs to be rebuilt before you run a build
Check Status
To get a list of running Daemons and their statuses, use the --status
command:
$ gradle --status
Sample output:
PID STATUS INFO
28486 IDLE 7.5
34247 BUSY 7.5
Currently, a given Gradle version can only connect to Daemons of the same version. This means the status output only shows Daemons spawned running the same version of Gradle as the current project.
Find all Daemons
If you have the Java Development Kit (JDK) installed, you can view live Daemons with the jps
command.
Live Daemons appear under the name GradleDaemon
. Because this command uses the JDK, you can view Daemons
running any version of Gradle.
Enable
Gradle enables the Daemon by default since Gradle 3.0. If your project doesn’t use the
Daemon, you can enable it for a single build with the --daemon
flag when you run a build:
$ gradle <task> --daemon
This flag overrides any settings that disable the Daemon in your project or user gradle.properties
files.
To enable the Daemon by default in older Gradle versions, add the following setting to the
gradle.properties
file in the project root or your Gradle user home:
org.gradle.daemon=true
Disable
You can disable the Daemon in multiple ways.
Disable for a Build
To disable the Daemon for a single build, pass the --no-daemon
flag when you run a build:
$ gradle <task> --no-daemon
This flag overrides any settings that enable the Daemon in your project or user gradle.properties
files.
Disable for a Project
To disable the Daemon for all builds of a project, add org.gradle.daemon=false
to
the gradle.properties
file in the project root.
Disable for a User
On Windows, this command disables the Daemon for the current user:
(if not exist "%USERPROFILE%/.gradle" mkdir "%USERPROFILE%/.gradle") && (echo. >> "%USERPROFILE%/.gradle/gradle.properties" && echo org.gradle.daemon=false >> "%USERPROFILE%/.gradle/gradle.properties")
On UNIX-like operating systems, the following Bash shell command disables the Daemon for the current user:
mkdir -p ~/.gradle && echo "org.gradle.daemon=false" >> ~/.gradle/gradle.properties
Disable Globally
There are two recommended ways to disable the Daemon globally across an environment:
-
add
org.gradle.daemon=false
to the$GRADLE_USER_HOME
/gradle.properties` file -
add the flag
-Dorg.gradle.daemon=false
to theGRADLE_OPTS
environment variable
Stop
It can be helpful to stop the Daemon when troubleshooting or debugging a failure. Daemons automatically stop given any of the following conditions:
-
available system memory is low
-
the Daemon has been idle for 3 hours
To stop running Daemon processes, use the following command:
$ gradle --stop
This terminates all Daemon processes started with the same version of Gradle used to execute the command.
You can also kill Daemons manually with your operating system. To find the PIDs for all Daemons regardless of Gradle version, see Find all Daemons.
Tools & IDEs
The Gradle Tooling API used by IDEs and other tools to integrate with Gradle always uses the Gradle Daemon to execute builds. If you execute Gradle builds from within your IDE, you already use the Gradle Daemon. There’s no need to enable it for your environment.
Continuous Integration
We recommend using the Daemon for both developer machines and Continuous Integration servers.
Compatibility
Gradle starts a new Daemon if no idle or compatible Daemons exist. The following values determine compatibility:
-
Requested build environment, including the following:
-
Java version
-
JVM attributes
-
JVM properties
-
-
Gradle version
Compatibility is based on exact matches of these values. For example:
-
If a Daemon is available with a Java 8 runtime, but the requested build environment calls for Java 10, then the Daemon is not compatible.
-
If a Daemon is available running Gradle 7.0, but the current build uses Gradle 7.4, then the Daemon is not compatible.
Certain properties of a Java runtime are immutable: they cannot be changed once the JVM has started. The following JVM system properties are immutable:
-
file.encoding
-
user.language
-
user.country
-
user.variant
-
java.io.tmpdir
-
javax.net.ssl.keyStore
-
javax.net.ssl.keyStorePassword
-
javax.net.ssl.keyStoreType
-
javax.net.ssl.trustStore
-
javax.net.ssl.trustStorePassword
-
javax.net.ssl.trustStoreType
-
com.sun.management.jmxremote
The following JVM attributes controlled by startup arguments are also immutable:
-
The maximum heap size (the
-Xmx
JVM argument) -
The minimum heap size (the
-Xms
JVM argument) -
The boot classpath (the
-Xbootclasspath
argument) -
The “assertion” status (the
-ea
argument)
If the requested build environment requirements for any of these properties and attributes differ from the Daemon’s JVM requirements, the Daemon is not compatible.
Note
|
For more information about build environments, see the build environment documentation. |
Performance Impact
The Daemon can reduce build times by 15-75% when you build the same project repeatedly.
Tip
|
To get a sense of the Daemon’s impact on your builds, you can profile your build with --profile .
|
In between builds, the Daemon waits idly for the next build. As a result, your machine only loads Gradle into memory once for multiple builds, instead of once per build. This is a significant performance optimization. But that’s not where it stops.
Runtime Code Optimizations
The JVM gains significant performance from runtime code optimization: optimizations applied to code while it runs. JVM implementations like OpenJDK’s Hotspot progressively optimize code during execution. Subsequent builds can be faster purely due to this optimization process. Experiments with HotSpot show that it takes somewhere between 5 and 10 builds for optimization to stabilize. Thanks to the Daemon, perceived build times can drop dramatically between the first build and tenth builds of a project.
Memory Caching
The Daemon enables in-memory caching across builds. This includes classes for plugins and build scripts. Similarly, the Daemon maintains in-memory caches of build data such as the hashes of task inputs and outputs for incremental builds.
Performance Monitoring
Gradle actively monitors heap usage to detect memory leaks in the Daemon. When a memory leak exhausts available heap space, the Daemon:
-
Finishes the currently running build.
-
Restarts before running the next build.
Gradle enables this monitoring by default.
To disable this monitoring, set the org.gradle.daemon.performance.enable-monitoring
Daemon option to
false
. You can do this on the command line with the following command:
$ gradle <task> -Dorg.gradle.daemon.performance.enable-monitoring=false
Or configure the property in the gradle.properties
file in the project root or your Gradle project home:
org.gradle.daemon.performance.enable-monitoring=false
FAQ
Why is there more than one Daemon process on my machine?
Gradle starts a new Daemon process for your build if no idle Daemon exists with a compatible configuration. For more information about compability, see Compatibility.
How much memory does the Daemon use and can I give it more?
If the requested build environment does not specify a maximum heap size, the Daemon uses up to 512MB of heap. Daemons use the JVM’s default minimum heap size. 512MB is more than enough for most builds. Larger builds with hundreds of subprojects, lots of configuration, and source code may benefit from a larger heap size.
To increase the amount of memory the Daemon can use, specify the appropriate flags as part of the requested build environment. Please see the build environment documentation for details.
What can go wrong with the Daemon?
User build scripts and third party plugins can destabilize the Daemon through memory leaks, poor resource management, and global state corruption. Failing to close files after reading and writing is a common cause. The Microsoft Windows operating system frequently stops Daemon processes destabilized in this way.
Specify the --no-daemon
switch for a build to prevent use of the Daemon.
This can help determine if the Daemon is causing a problem with your build.
Initialization Scripts
Gradle provides a powerful mechanism to allow customizing the build based on the current environment. This mechanism also supports tools that wish to integrate with Gradle.
Note that this is completely different from the “init
” task provided by the “build-init
” plugin (see Build Init Plugin).
Basic usage
Initialization scripts (a.k.a. init scripts) are similar to other scripts in Gradle. These scripts, however, are run before the build starts. Here are several possible uses:
-
Set up enterprise-wide configuration, such as where to find custom plugins.
-
Set up properties based on the current environment, such as a developer’s machine vs. a continuous integration server.
-
Supply personal information about the user that is required by the build, such as repository or database authentication credentials.
-
Define machine specific details, such as where JDKs are installed.
-
Register build listeners. External tools that wish to listen to Gradle events might find this useful.
-
Register build loggers. You might wish to customize how Gradle logs the events that it generates.
One main limitation of init scripts is that they cannot access classes in the buildSrc
project (see Using buildSrc to extract imperative logic for details of this feature).
Using an init script
There are several ways to use an init script:
-
Specify a file on the command line. The command line option is
-I
or--init-script
followed by the path to the script. The command line option can appear more than once, each time adding another init script. The build will fail if any of the files specified on the command line does not exist. -
Put a file called
init.gradle
(orinit.gradle.kts
for Kotlin) in the$GRADLE_USER_HOME/
directory. -
Put a file that ends with
.gradle
(or.init.gradle.kts
for Kotlin) in the$GRADLE_USER_HOME/init.d/
directory. -
Put a file that ends with
.gradle
(or.init.gradle.kts
for Kotlin) in the$GRADLE_HOME/init.d/
directory, in the Gradle distribution. This allows you to package a custom Gradle distribution containing custom build logic and plugins. You can combine this with the Gradle wrapper as a way to make custom logic available to all builds in your enterprise.
If more than one init script is found they will all be executed, in the order specified above. Scripts in a given directory are executed in alphabetical order. This allows, for example, a tool to specify an init script on the command line and the user to put one in their home directory for defining the environment and both scripts will run when Gradle is executed.
Writing an init script
Similar to a Gradle build script, an init script is a Groovy or Kotlin script.
Each init script has a Gradle instance associated with it.
Any property reference and method call in the init script will delegate to this Gradle
instance.
Each init script also implements the Script interface.
Note
|
When writing init scripts, pay attention to the scope of the reference you are trying to access.
For example, properties loaded from |
Configuring projects from an init script
You can use an init script to configure the projects in the build. This works in a similar way to configuring projects in a multi-project build. The following sample shows how to perform extra configuration from an init script before the projects are evaluated. This sample uses this feature to configure an extra repository to be used only for certain environments.
repositories {
mavenCentral()
}
tasks.register("showRepos") {
val repositoryNames = repositories.map { it.name }
doLast {
println("All repos:")
println(repositoryNames)
}
}
allprojects {
repositories {
mavenLocal()
}
}
repositories {
mavenCentral()
}
tasks.register('showRepos') {
def repositoryNames = repositories.collect { it.name }
doLast {
println "All repos:"
println repositoryNames
}
}
allprojects {
repositories {
mavenLocal()
}
}
Output when applying the init script
> gradle --init-script init.gradle.kts -q showRepos
All repos:
[MavenLocal, MavenRepo]
> gradle --init-script init.gradle -q showRepos
All repos:
[MavenLocal, MavenRepo]
External dependencies for the init script
In External dependencies for the build script it was explained how to add external dependencies to a build script. Init scripts can also declare dependencies. You do this with the initscript()
method, passing in a closure which declares the init script classpath.
initscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.apache.commons:commons-math:2.0")
}
}
initscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.apache.commons:commons-math:2.0'
}
}
The closure passed to the initscript()
method configures a ScriptHandler instance.
You declare the init script classpath by adding dependencies to the classpath
configuration.
This is the same way you declare, for example, the Java compilation classpath.
You can use any of the dependency types described in Declaring Dependencies, except project dependencies.
Having declared the init script classpath, you can use the classes in your init script as you would any other classes on the classpath. The following example adds to the previous example, and uses classes from the init script classpath.
import org.apache.commons.math.fraction.Fraction
initscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.apache.commons:commons-math:2.0")
}
}
println(Fraction.ONE_FIFTH.multiply(2))
tasks.register("doNothing")
import org.apache.commons.math.fraction.Fraction
initscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.apache.commons:commons-math:2.0'
}
}
println Fraction.ONE_FIFTH.multiply(2)
tasks.register('doNothing')
Output when applying the init script
> gradle --init-script init.gradle.kts -q doNothing
2 / 5
> gradle --init-script init.gradle -q doNothing
2 / 5
Init script plugins
Similar to a Gradle build script or a Gradle settings file, plugins can be applied on init scripts.
apply<EnterpriseRepositoryPlugin>()
class EnterpriseRepositoryPlugin : Plugin<Gradle> {
companion object {
const val ENTERPRISE_REPOSITORY_URL = "https://repo.gradle.org/gradle/repo"
}
override fun apply(gradle: Gradle) {
// ONLY USE ENTERPRISE REPO FOR DEPENDENCIES
gradle.allprojects {
repositories {
// Remove all repositories not pointing to the enterprise repository url
all {
if (this !is MavenArtifactRepository || url.toString() != ENTERPRISE_REPOSITORY_URL) {
project.logger.lifecycle("Repository ${(this as? MavenArtifactRepository)?.url ?: name} removed. Only $ENTERPRISE_REPOSITORY_URL is allowed")
remove(this)
}
}
// add the enterprise repository
add(maven {
name = "STANDARD_ENTERPRISE_REPO"
url = uri(ENTERPRISE_REPOSITORY_URL)
})
}
}
}
}
repositories{
mavenCentral()
}
data class RepositoryData(val name: String, val url: URI)
tasks.register("showRepositories") {
val repositoryData = repositories.withType<MavenArtifactRepository>().map { RepositoryData(it.name, it.url) }
doLast {
repositoryData.forEach {
println("repository: ${it.name} ('${it.url}')")
}
}
}
apply plugin: EnterpriseRepositoryPlugin
class EnterpriseRepositoryPlugin implements Plugin<Gradle> {
private static String ENTERPRISE_REPOSITORY_URL = "https://repo.gradle.org/gradle/repo"
void apply(Gradle gradle) {
// ONLY USE ENTERPRISE REPO FOR DEPENDENCIES
gradle.allprojects { project ->
project.repositories {
// Remove all repositories not pointing to the enterprise repository url
all { ArtifactRepository repo ->
if (!(repo instanceof MavenArtifactRepository) ||
repo.url.toString() != ENTERPRISE_REPOSITORY_URL) {
project.logger.lifecycle "Repository ${repo.url} removed. Only $ENTERPRISE_REPOSITORY_URL is allowed"
remove repo
}
}
// add the enterprise repository
maven {
name "STANDARD_ENTERPRISE_REPO"
url ENTERPRISE_REPOSITORY_URL
}
}
}
}
}
repositories{
mavenCentral()
}
@Immutable
class RepositoryData {
String name
URI url
}
tasks.register('showRepositories') {
def repositoryData = repositories.collect { new RepositoryData(it.name, it.url) }
doLast {
repositoryData.each {
println "repository: ${it.name} ('${it.url}')"
}
}
}
Output when applying the init script
> gradle --init-script init.gradle.kts -q showRepositories
repository: STANDARD_ENTERPRISE_REPO ('https://repo.gradle.org/gradle/repo')
> gradle --init-script init.gradle -q showRepositories
repository: STANDARD_ENTERPRISE_REPO ('https://repo.gradle.org/gradle/repo')
The plugin in the init script ensures that only a specified repository is used when running the build.
When applying plugins within the init script, Gradle instantiates the plugin and calls the plugin instance’s Plugin.apply(T) method. The gradle
object is passed as a parameter, which can be used to configure all aspects of a build. Of course, the applied plugin can be resolved as an external dependency as described in External dependencies for the init script
Executing Multi-Project Builds
Only the smallest of projects has a single build file and source tree, unless it happens to be a massive, monolithic application. It’s often much easier to digest and understand a project that has been split into smaller, inter-dependent modules. The word “inter-dependent” is important, though, and is why you typically want to link the modules together through a single build.
Gradle supports this scenario through multi-project builds.
For details about authoring multi-project builds, consult the Authoring Multi-Project Builds section of the user manual.
Identifying project structure
To identify the project structure, you can use gradle projects
command.
As an example, let’s use a multi-project build with the following structure:
> gradle -q projects ------------------------------------------------------------ Root project 'multiproject' ------------------------------------------------------------ Root project 'multiproject' +--- Project ':api' +--- Project ':services' | +--- Project ':services:shared' | \--- Project ':services:webservice' \--- Project ':shared' To see a list of the tasks of a project, run gradle <project-path>:tasks For example, try running gradle :api:tasks
From a user’s perspective, multi-project builds are still collections of tasks you can run. The difference is that you may want to control which project’s tasks get executed. The following sections will cover the two options you have for executing tasks in a multi-project build.
Executing tasks by name
The command gradle test
will execute the test
task in any subprojects, relative to the current working directory, that have that task.
If you run the command from the root project directory, you’ll run test
in api, shared, services:shared and services:webservice.
If you run the command from the services project directory, you’ll only execute the task in services:shared and services:webservice.
The basic rule behind Gradle’s behavior is: execute all tasks down the hierarchy which have this name. Only complain if there is no such task found in any of the subprojects traversed.
Note
|
Some tasks selectors, like |
Gradle looks down the hierarchy, starting with the current dir, for tasks with the given name and executes them. One thing is very important to note. Gradle always evaluates every project of the multi-project build and creates all existing task objects. Then, according to the task name arguments and the current directory, Gradle filters the tasks which should be executed. Because of Gradle’s cross project configuration, every project has to be evaluated before any task gets executed.
When you’re using the Gradle wrapper, executing a task for a specific subproject by running Gradle from the subproject’s directory
doesn’t work well because you have to specify the path to the wrapper script if you’re not in the project root.
For example, if want to run build
task for the webservice subproject and you’re in the webservice subproject directory,
you would have to run ../../gradlew build
.
The next section shows how this can be achieved directly from the project’s root directory.
Executing tasks by fully qualified name
You can use a task’s fully qualified name to execute a specific task in a specific subproject.
For example: gradle :services:webservice:build
will run the build
task of the webservice subproject.
The fully qualified name of a task is simply its project path plus the task name.
A project path has the following pattern: it starts with an optional colon, which denotes the root project.
The root project is the only project in a path that is not specified by its name.
The rest of a project path is a colon-separated sequence of project names, where the next project is a subproject of the previous project.
You can see the project paths when running gradle projects
as shown in identifying project structure section.
This approach works for any task, so if you want to know what tasks are in a particular subproject,
just use the tasks
task, e.g. gradle :services:webservice:tasks
.
Regardless of which technique you use to execute tasks, Gradle will take care of building any subprojects that the target depends on. You don’t have to worry about the inter-project dependencies yourself. If you’re interested in how this is configured, you can read about writing multi-project builds later in the user manual.
That’s all you really need to know about multi-project builds as a build user. You can now identify whether a build is a multi-project one and you can discover its structure. And finally, you can execute tasks within specific subprojects.
Multi-Project Building and Testing
The build
task of the Java plugin is typically used to compile, test, and perform code style checks (if the CodeQuality plugin is used) of a single project.
In multi-project builds you may often want to do all of these tasks across a range of projects.
The buildNeeded
and buildDependents
tasks can help with this.
In this example, the :services:person-service
project depends on both the :api
and :shared
projects.
The :api
project also depends on the :shared
project.
Assume you are working on a single project, the :api
project.
You have been making changes, but have not built the entire project since performing a clean.
You want to build any necessary supporting jars, but only perform code quality and unit tests on the project you have changed.
The build
task does this.
gradle :api:build
> gradle :api:build > Task :shared:compileJava > Task :shared:processResources > Task :shared:classes > Task :shared:jar > Task :api:compileJava > Task :api:processResources > Task :api:classes > Task :api:jar > Task :api:assemble > Task :api:compileTestJava > Task :api:processTestResources > Task :api:testClasses > Task :api:test > Task :api:check > Task :api:build BUILD SUCCESSFUL in 0s
If you have just gotten the latest version of source from your version control system which included changes in other projects that :api
depends on, you might want to not only build all the projects you depend on, but test them as well.
The buildNeeded
task also tests all the projects from the project dependencies of the testRuntime configuration.
gradle :api:buildNeeded
> gradle :api:buildNeeded > Task :shared:compileJava > Task :shared:processResources > Task :shared:classes > Task :shared:jar > Task :api:compileJava > Task :api:processResources > Task :api:classes > Task :api:jar > Task :api:assemble > Task :api:compileTestJava > Task :api:processTestResources > Task :api:testClasses > Task :api:test > Task :api:check > Task :api:build > Task :shared:assemble > Task :shared:compileTestJava > Task :shared:processTestResources > Task :shared:testClasses > Task :shared:test > Task :shared:check > Task :shared:build > Task :shared:buildNeeded > Task :api:buildNeeded BUILD SUCCESSFUL in 0s
You also might want to refactor some part of the :api
project that is used in other projects.
If you make these types of changes, it is not sufficient to test just the :api
project, you also need to test all projects that depend on the :api
project.
The buildDependents
task also tests all the projects that have a project dependency (in the testRuntime configuration) on the specified project.
gradle :api:buildDependents
> gradle :api:buildDependents > Task :shared:compileJava > Task :shared:processResources > Task :shared:classes > Task :shared:jar > Task :api:compileJava > Task :api:processResources > Task :api:classes > Task :api:jar > Task :api:assemble > Task :api:compileTestJava > Task :api:processTestResources > Task :api:testClasses > Task :api:test > Task :api:check > Task :api:build > Task :services:person-service:compileJava > Task :services:person-service:processResources > Task :services:person-service:classes > Task :services:person-service:jar > Task :services:person-service:assemble > Task :services:person-service:compileTestJava > Task :services:person-service:processTestResources > Task :services:person-service:testClasses > Task :services:person-service:test > Task :services:person-service:check > Task :services:person-service:build > Task :services:person-service:buildDependents > Task :api:buildDependents BUILD SUCCESSFUL in 0s
Finally, you may want to build and test everything in all projects.
Any task you run in the root project folder will cause that same named task to be run on all the children.
So you can just run gradle build
to build and test all projects.
Build Cache
Tip
|
Want to learn the tips and tricks top engineering teams use to keep builds fast and performant? Register here for our Build Cache Training. |
Overview
The Gradle build cache is a cache mechanism that aims to save time by reusing outputs produced by other builds. The build cache works by storing (locally or remotely) build outputs and allowing builds to fetch these outputs from the cache when it is determined that inputs have not changed, avoiding the expensive work of regenerating them.
A first feature using the build cache is task output caching. Essentially, task output caching leverages the same intelligence as up-to-date checks that Gradle uses to avoid work when a previous local build has already produced a set of task outputs. But instead of being limited to the previous build in the same workspace, task output caching allows Gradle to reuse task outputs from any earlier build in any location on the local machine. When using a shared build cache for task output caching this even works across developer machines and build agents.
Apart from tasks, artifact transforms can also leverage the build cache and re-use their outputs similarly to task output caching.
Tip
|
For a hands-on approach to learning how to use the build cache, start with reading through the use cases for the build cache and the follow up sections. It covers the different scenarios that caching can improve and has detailed discussions of the different caveats you need to be aware of when enabling caching for a build. |
Enable the Build Cache
By default, the build cache is not enabled. You can enable the build cache in a couple of ways:
- Run with
--build-cache
on the command-line -
Gradle will use the build cache for this build only.
- Put
org.gradle.caching=true
in yourgradle.properties
-
Gradle will try to reuse outputs from previous builds for all builds, unless explicitly disabled with
--no-build-cache
.
When the build cache is enabled, it will store build outputs in the Gradle user home. For configuring this directory or different kinds of build caches see Configure the Build Cache.
Task Output Caching
Beyond incremental builds described in up-to-date checks, Gradle can save time by reusing outputs from previous executions of a task by matching inputs to the task. Task outputs can be reused between builds on one computer or even between builds running on different computers via a build cache.
We have focused on the use case where users have an organization-wide remote build cache that is populated regularly by continuous integration builds.
Developers and other continuous integration agents should load cache entries from the remote build cache.
We expect that developers will not be allowed to populate the remote build cache, and all continuous integration builds populate the build cache after running the clean
task.
For your build to play well with task output caching it must work well with the incremental build feature.
For example, when running your build twice in a row all tasks with outputs should be UP-TO-DATE
.
You cannot expect faster builds or correct builds when enabling task output caching when this prerequisite is not met.
Task output caching is automatically enabled when you enable the build cache, see Enable the Build Cache.
What does it look like
Let us start with a project using the Java plugin which has a few Java source files. We run the build the first time.
> gradle --build-cache compileJava :compileJava :processResources :classes :jar :assemble BUILD SUCCESSFUL
We see the directory used by the local build cache in the output. Apart from that the build was the same as without the build cache. Let’s clean and run the build again.
> gradle clean :clean BUILD SUCCESSFUL
> gradle --build-cache assemble :compileJava FROM-CACHE :processResources :classes :jar :assemble BUILD SUCCESSFUL
Now we see that, instead of executing the :compileJava
task, the outputs of the task have been loaded from the build cache.
The other tasks have not been loaded from the build cache since they are not cacheable. This is due to
:classes
and :assemble
being lifecycle tasks and :processResources
and :jar
being Copy-like tasks which are not cacheable since it is generally faster to execute them.
Cacheable tasks
Since a task describes all of its inputs and outputs, Gradle can compute a build cache key that uniquely defines the task’s outputs based on its inputs. That build cache key is used to request previous outputs from a build cache or store new outputs in the build cache. If the previous build outputs have been already stored in the cache by someone else, e.g. your continuous integration server or other developers, you can avoid executing most tasks locally.
The following inputs contribute to the build cache key for a task in the same way that they do for up-to-date checks:
-
The task type and its classpath
-
The names of the output properties
-
The names and values of properties annotated as described in the section called "Custom task types"
-
The names and values of properties added by the DSL via TaskInputs
-
The classpath of the Gradle distribution, buildSrc and plugins
-
The content of the build script when it affects execution of the task
Task types need to opt-in to task output caching using the @CacheableTask annotation. Note that @CacheableTask is not inherited by subclasses. Custom task types are not cacheable by default.
Built-in cacheable tasks
Currently, the following built-in Gradle tasks are cacheable:
-
Java toolchain: JavaCompile, Javadoc
-
Groovy toolchain: GroovyCompile, Groovydoc
-
Scala toolchain: ScalaCompile,
org.gradle.language.scala.tasks.PlatformScalaCompile
(removed), ScalaDoc -
Native toolchain: CppCompile, CCompile, SwiftCompile
-
Testing: Test
-
Code quality tasks: Checkstyle, CodeNarc, Pmd
-
JaCoCo: JacocoReport
-
Other tasks: AntlrTask, ValidatePlugins, WriteProperties
All other built-in tasks are currently not cacheable.
Third party plugins
There are third party plugins that work well with the build cache. The most prominent examples are the Android plugin 3.1+ and the Kotlin plugin 1.2.21+. For other third party plugins, check their documentation to find out whether they support the build cache.
Declaring task inputs and outputs
It is very important that a cacheable task has a complete picture of its inputs and outputs, so that the results from one build can be safely re-used somewhere else.
Missing task inputs can cause incorrect cache hits, where different results are treated as identical because the same cache key is used by both executions. Missing task outputs can cause build failures if Gradle does not completely capture all outputs for a given task. Wrongly declared task inputs can lead to cache misses especially when containing volatile data or absolute paths. (See the section called "Task inputs and outputs" on what should be declared as inputs and outputs.)
Note
|
The task path is not an input to the build cache key. This means that tasks with different task paths can re-use each other’s outputs as long as Gradle determines that executing them yields the same result. |
In order to ensure that the inputs and outputs are properly declared use integration tests (for example using TestKit) to check that a task produces the same outputs for identical inputs and captures all output files for the task. We suggest adding tests to ensure that the task inputs are relocatable, i.e. that the task can be loaded from the cache into a different build directory (see @PathSensitive).
In order to handle volatile inputs for your tasks consider configuring input normalization.
Marking tasks as non-cacheable by default
There are certain tasks that don’t benefit from using the build cache.
One example is a task that only moves data around the file system, like a Copy
task.
You can signify that a task is not to be cached by adding the @DisableCachingByDefault
annotation to it.
You can also give a human-readable reason for not caching the task by default.
The annotation can be used on its own, or together with @CacheableTask
.
Note
|
This annotation is only for documenting the reason behind not caching the task by default. Build logic can override this decision via the runtime API (see below). |
Enable caching of non-cacheable tasks
As we have seen, built-in tasks, or tasks provided by plugins, are cacheable if their class is annotated with the Cacheable
annotation.
But what if you want to make cacheable a task whose class is not cacheable?
Let’s take a concrete example: your build script uses a generic NpmTask
task to create a JavaScript bundle by delegating to NPM (and running npm run bundle
).
This process is similar to a complex compilation task, but NpmTask
is too generic to be cacheable by default: it just takes arguments and runs npm with those arguments.
The inputs and outputs of this task are simple to figure out. The inputs are the directory containing the JavaScript files, and the NPM configuration files. The output is the bundle file generated by this task.
Using annotations
We create a subclass of the NpmTask
and use annotations to declare the inputs and outputs.
When possible, it is better to use delegation instead of creating a subclass.
That is the case for the built in JavaExec
, Exec
, Copy
and Sync
tasks, which have a method on Project
to do the actual work.
If you’re a modern JavaScript developer, you know that bundling can be quite long, and is worth caching. To achieve that, we need to tell Gradle that it’s allowed to cache the output of that task, using the @CacheableTask annotation.
This is sufficient to make the task cacheable on your own machine. However, input files are identified by default by their absolute path. So if the cache needs to be shared between several developers or machines using different paths, that won’t work as expected. So we also need to set the path sensitivity. In this case, the relative path of the input files can be used to identify them.
Note that it is possible to override property annotations from the base class by overriding the getter of the base class and annotating that method.
@CacheableTask // (1)
abstract class BundleTask : NpmTask() {
@get:Internal // (2)
override val args
get() = super.args
@get:InputDirectory
@get:SkipWhenEmpty
@get:PathSensitive(PathSensitivity.RELATIVE) // (3)
abstract val scripts: DirectoryProperty
@get:InputFiles
@get:PathSensitive(PathSensitivity.RELATIVE) // (4)
abstract val configFiles: ConfigurableFileCollection
@get:OutputFile
abstract val bundle: RegularFileProperty
init {
args.addAll("run", "bundle")
bundle.set(projectLayout.buildDirectory.file("bundle.js"))
scripts.set(projectLayout.projectDirectory.dir("scripts"))
configFiles.from(projectLayout.projectDirectory.file("package.json"))
configFiles.from(projectLayout.projectDirectory.file("package-lock.json"))
}
}
tasks.register<BundleTask>("bundle")
@CacheableTask // (1)
abstract class BundleTask extends NpmTask {
@Override @Internal // (2)
ListProperty<String> getArgs() {
super.getArgs()
}
@InputDirectory
@SkipWhenEmpty
@PathSensitive(PathSensitivity.RELATIVE) // (3)
abstract DirectoryProperty getScripts()
@InputFiles
@PathSensitive(PathSensitivity.RELATIVE) // (4)
abstract ConfigurableFileCollection getConfigFiles()
@OutputFile
abstract RegularFileProperty getBundle()
BundleTask() {
args.addAll("run", "bundle")
bundle.set(projectLayout.buildDirectory.file("bundle.js"))
scripts.set(projectLayout.projectDirectory.dir("scripts"))
configFiles.from(projectLayout.projectDirectory.file("package.json"))
configFiles.from(projectLayout.projectDirectory.file("package-lock.json"))
}
}
tasks.register('bundle', BundleTask)
-
(1) Add
@CacheableTask
to enable caching for the task. -
(2) Override the getter of a property of the base class to change the input annotation to
@Internal
. -
(3) (4) Declare the path sensitivity.
Using the runtime API
If for some reason you cannot create a new custom task class, it is also possible to make a task cacheable using the runtime API to declare the inputs and outputs.
For enabling caching for the task you need to use the TaskOutputs.cacheIf() method.
The declarations via the runtime API have the same effect as the annotations described above. Note that you cannot override file inputs and outputs via the runtime API. Input properties can be overridden by specifying the same property name.
tasks.register<NpmTask>("bundle") {
args.set(listOf("run", "bundle"))
outputs.cacheIf { true }
inputs.dir(file("scripts"))
.withPropertyName("scripts")
.withPathSensitivity(PathSensitivity.RELATIVE)
inputs.files("package.json", "package-lock.json")
.withPropertyName("configFiles")
.withPathSensitivity(PathSensitivity.RELATIVE)
outputs.file("$buildDir/bundle.js")
.withPropertyName("bundle")
}
tasks.register('bundle', NpmTask) {
args = ['run', 'bundle']
outputs.cacheIf { true }
inputs.dir(file("scripts"))
.withPropertyName("scripts")
.withPathSensitivity(PathSensitivity.RELATIVE)
inputs.files("package.json", "package-lock.json")
.withPropertyName("configFiles")
.withPathSensitivity(PathSensitivity.RELATIVE)
outputs.file("$buildDir/bundle.js")
.withPropertyName("bundle")
}
Configure the Build Cache
You can configure the build cache by using the Settings.buildCache(org.gradle.api.Action) block in settings.gradle
.
Gradle supports a local
and a remote
build cache that can be configured separately.
When both build caches are enabled, Gradle tries to load build outputs from the local build cache first, and then tries the remote build cache if no build outputs are found.
If outputs are found in the remote cache, they are also stored in the local cache, so next time they will be found locally.
Gradle stores ("pushes") build outputs in any build cache that is enabled and has BuildCache.isPush() set to true
.
By default, the local build cache has push enabled, and the remote build cache has push disabled.
The local build cache is pre-configured to be a DirectoryBuildCache and enabled by default. The remote build cache can be configured by specifying the type of build cache to connect to (BuildCacheConfiguration.remote(java.lang.Class)).
Built-in local build cache
The built-in local build cache, DirectoryBuildCache, uses a directory to store build cache artifacts. By default, this directory resides in the Gradle user home directory, but its location is configurable.
Gradle will periodically clean-up the local cache directory by removing entries that have not been used recently to conserve disk space. How often Gradle will perform this clean-up is configurable as shown in the example below. Note that cache entries are cleaned-up regardless of the project they were produced by. If different projects configure this clean-up to run at different periods, the shortest period will clean-up cache entries for all projects. Therefore it is recommended to configure this setting globally in the init script. The Configuration use-cases section has an example of putting cache configuration in the init script.
For more details on the configuration options refer to the DSL documentation of DirectoryBuildCache. Here is an example of the configuration.
buildCache {
local {
directory = File(rootDir, "build-cache")
removeUnusedEntriesAfterDays = 30
}
}
buildCache {
local {
directory = new File(rootDir, 'build-cache')
removeUnusedEntriesAfterDays = 30
}
}
Remote HTTP build cache
HttpBuildCache provides the ability read to and write from a remote cache via HTTP.
With the following configuration, the local build cache will be used for storing build outputs while the local and the remote build cache will be used for retrieving build outputs.
buildCache {
remote<HttpBuildCache> {
url = uri("https://example.com:8123/cache/")
}
}
buildCache {
remote(HttpBuildCache) {
url = 'https://example.com:8123/cache/'
}
}
When attempting to load an entry, a GET
request is made to https://example.com:8123/cache/«cache-key»
.
The response must have a 2xx
status and the cache entry as the body, or a 404 Not Found
status if the entry does not exist.
When attempting to store an entry, a PUT
request is made to https://example.com:8123/cache/«cache-key»
.
Any 2xx
response status is interpreted as success.
A 413 Payload Too Large
response may be returned to indicate that the payload is larger than the server will accept, which will not be treated as an error.
Specifying access credentials
HTTP Basic Authentication is supported, with credentials being sent preemptively.
buildCache {
remote<HttpBuildCache> {
url = uri("https://example.com:8123/cache/")
credentials {
username = "build-cache-user"
password = "some-complicated-password"
}
}
}
buildCache {
remote(HttpBuildCache) {
url = 'https://example.com:8123/cache/'
credentials {
username = 'build-cache-user'
password = 'some-complicated-password'
}
}
}
Redirects
3xx
redirecting responses will be followed automatically.
Servers must take care when redirecting PUT
requests as only 307
and 308
redirect responses will be followed with a PUT
request.
All other redirect responses will be followed with a GET
request, as per RFC 7231,
without the entry payload as the body.
Network error handling
Requests that fail during request transmission, after having established a TCP connection, will be retried automatically.
This prevents temporary problems, such as connection drops, read or write timeouts, and low level network failures such as a connection resets, causing cache operations to fail and disabling the remote cache for the remainder of the build.
Requests will be retried up to 3 times. If the problem persists, the cache operation will fail and the remote cache will be disabled for the remainder of the build.
Using SSL
By default, use of HTTPS requires the server to present a certificate that is trusted by the build’s Java runtime. If your server’s certificate is not trusted, you can:
-
Update the trust store of your Java runtime to allow it to be trusted
-
Change the build environment to use an alternative trust store for the build runtime
-
Disable the requirement for a trusted certificate
The trust requirement can be disabled by setting HttpBuildCache.isAllowUntrustedServer() to true
.
Enabling this option is a security risk, as it allows any cache server to impersonate the intended server.
It should only be used as a temporary measure or in very tightly controlled network environments.
buildCache {
remote<HttpBuildCache> {
url = uri("https://example.com:8123/cache/")
isAllowUntrustedServer = true
}
}
buildCache {
remote(HttpBuildCache) {
url = 'https://example.com:8123/cache/'
allowUntrustedServer = true
}
}
HTTP expect-continue
Use of HTTP Expect-Continue can be enabled. This causes upload requests to happen in two parts: first a check whether a body would be accepted, then transmission of the body if the server indicates it will accept it.
This is useful when uploading to cache servers that routinely redirect or reject upload requests, as it avoids uploading the cache entry just to have it rejected (e.g. the cache entry is larger than the cache will allow) or redirected. This additional check incurs extra latency when the server accepts the request, but reduces latency when the request is rejected or redirected.
Not all HTTP servers and proxies reliably implement Expect-Continue. Be sure to check that your cache server does support it before enabling.
To enable, set HttpBuildCache.isUseExpectContinue() to true
.
buildCache {
remote<HttpBuildCache> {
url = uri("https://example.com:8123/cache/")
isUseExpectContinue = true
}
}
buildCache {
remote(HttpBuildCache) {
url = 'https://example.com:8123/cache/'
useExpectContinue = true
}
}
Configuration use cases
The recommended use case for the remote build cache is that your continuous integration server populates it from clean builds while developers only load from it. The configuration would then look as follows.
val isCiServer = System.getenv().containsKey("CI")
buildCache {
remote<HttpBuildCache> {
url = uri("https://example.com:8123/cache/")
isPush = isCiServer
}
}
boolean isCiServer = System.getenv().containsKey("CI")
buildCache {
remote(HttpBuildCache) {
url = 'https://example.com:8123/cache/'
push = isCiServer
}
}
It is also possible to configure the build cache from an init script, which can be used from the command line, added to your Gradle user home or be a part of your custom Gradle distribution.
gradle.settingsEvaluated {
buildCache {
// vvv Your custom configuration goes here
remote<HttpBuildCache> {
url = uri("https://example.com:8123/cache/")
}
// ^^^ Your custom configuration goes here
}
}
gradle.settingsEvaluated { settings ->
settings.buildCache {
// vvv Your custom configuration goes here
remote(HttpBuildCache) {
url = 'https://example.com:8123/cache/'
}
// ^^^ Your custom configuration goes here
}
}
Build cache, composite builds and buildSrc
Gradle’s composite build feature allows including other complete Gradle builds into another. Such included builds will inherit the build cache configuration from the top level build, regardless of whether the included builds define build cache configuration themselves or not.
The build cache configuration present for any included build is effectively ignored, in favour of the top level build’s configuration.
This also applies to any buildSrc
projects of any included builds.
The buildSrc
directory is treated as an included build, and as such it inherits the build cache configuration from the top-level build.
Note
|
This configuration precedence does not apply to plugin builds included through |
How to set up an HTTP build cache backend
Gradle provides a Docker image for a build cache node, which can connect with Gradle Enterprise for centralized management. The cache node can also be used without a Gradle Enterprise installation with restricted functionality.
Implement your own Build Cache
Using a different build cache backend to store build outputs (which is not covered by the built-in support for connecting to an HTTP backend) requires implementing your own logic for connecting to your custom build cache backend. To this end, custom build cache types can be registered via BuildCacheConfiguration.registerBuildCacheService(java.lang.Class, java.lang.Class).
Gradle Enterprise includes a high-performance, easy to install and operate, shared build cache backend.
Authoring Gradle Builds
Build Script Basics
This chapter introduces you to the basics of writing Gradle build scripts. It uses toy examples to explain basic functionality of Gradle, which is helpful to get an understanding of the basic concepts. Especially if you move to Gradle from other build tools like Ant and want to understand differences and advantages.
However, to get started with a standard project setup, you don’t even need to go into these concepts in detail. Instead, you can have a quick hands-on introduction, through our step-by-step samples.
Projects, plugins and tasks
Every Gradle build is made up of one or more projects. What a project represents depends on what it is that you are doing with Gradle. For example, a project might represent a library JAR or a web application. It might represent a distribution ZIP assembled from the JARs produced by other projects. A project does not necessarily represent a thing to be built. It might represent a thing to be done, such as deploying your application to staging or production environments. Don’t worry if this seems a little vague for now. Gradle’s build-by-convention support adds a more concrete definition for what a project is.
The work that Gradle can do on a project is defined by one or more tasks. A task represents some atomic piece of work which a build performs. This might be compiling some classes, creating a JAR, generating Javadoc, or publishing some archives to a repository.
Typically, tasks are provided by applying a plugin so that you do not have to define them yourself. Still, to give you an idea of what a task is, we will look at defining some simple tasks in a build with one project in this chapter.
Hello world
You run a Gradle build using the gradle
command. The gradle
command looks for a file called build.gradle.kts
in the current directory.[1] We call this build.gradle.kts
file a build script, although strictly speaking it is a build configuration script, as we will see later. The build script defines a project and its tasks.
To try this out, create the following build script named build.gradle.kts
.
You run a Gradle build using the gradle
command. The gradle
command looks for a file called build.gradle
in the current directory.[2] We call this build.gradle
file a build script, although strictly speaking it is a build configuration script, as we will see later. The build script defines a project and its tasks.
To try this out, create the following build script named build.gradle
.
tasks.register("hello") {
doLast {
println("Hello world!")
}
}
tasks.register('hello') {
doLast {
println 'Hello world!'
}
}
In a command-line shell, move to the containing directory and execute the build script with gradle -q hello
:
Tip
|
What does
-q do?Most of the examples in this user guide are run with the |
gradle -q hello
> gradle -q hello Hello world!
What’s going on here? This build script defines a single task, called hello
, and adds an action to it. When you run gradle hello
, Gradle executes the hello
task, which in turn executes the action you’ve provided. The action is simply a block containing some code to execute.
If you think this looks similar to Ant’s targets, you would be right. Gradle tasks are the equivalent to Ant targets, but as you will see, they are much more powerful. We have used a different terminology than Ant as we think the word task is more expressive than the word target. Unfortunately this introduces a terminology clash with Ant, as Ant calls its commands, such as javac
or copy
, tasks. So when we talk about tasks, we always mean Gradle tasks, which are the equivalent to Ant’s targets. If we talk about Ant tasks (Ant commands), we explicitly say Ant task.
Build scripts are code
Gradle’s build scripts give you the full power of Groovy and Kotlin. As an appetizer, have a look at this:
tasks.register("upper") {
doLast {
val someString = "mY_nAmE"
println("Original: $someString")
println("Upper case: ${someString.toUpperCase()}")
}
}
tasks.register('upper') {
doLast {
String someString = 'mY_nAmE'
println "Original: $someString"
println "Upper case: ${someString.toUpperCase()}"
}
}
gradle -q upper
> gradle -q upper Original: mY_nAmE Upper case: MY_NAME
or
tasks.register("count") {
doLast {
repeat(4) { print("$it ") }
}
}
tasks.register('count') {
doLast {
4.times { print "$it " }
}
}
gradle -q count
> gradle -q count 0 1 2 3
Task dependencies
As you probably have guessed, you can declare tasks that depend on other tasks.
tasks.register("hello") {
doLast {
println("Hello world!")
}
}
tasks.register("intro") {
dependsOn("hello")
doLast {
println("I'm Gradle")
}
}
tasks.register('hello') {
doLast {
println 'Hello world!'
}
}
tasks.register('intro') {
dependsOn tasks.hello
doLast {
println "I'm Gradle"
}
}
gradle -q intro
> gradle -q intro Hello world! I'm Gradle
To add a dependency, the corresponding task does not need to exist.
tasks.register("taskX") {
dependsOn("taskY")
doLast {
println("taskX")
}
}
tasks.register("taskY") {
doLast {
println("taskY")
}
}
tasks.register('taskX') {
dependsOn 'taskY'
doLast {
println 'taskX'
}
}
tasks.register('taskY') {
doLast {
println 'taskY'
}
}
gradle -q taskX
> gradle -q taskX taskY taskX
The dependency of taskX
to taskY
may be declared before taskY
is defined.
Task dependencies are discussed in more detail in Adding dependencies to a task.
Flexible task registration
The power of Groovy or Kotlin can be used for more than defining what a task does. For example, you can use it to register multiple tasks of the same type in a loop.
repeat(4) { counter ->
tasks.register("task$counter") {
doLast {
println("I'm task number $counter")
}
}
}
4.times { counter ->
tasks.register("task$counter") {
doLast {
println "I'm task number $counter"
}
}
}
gradle -q task1
> gradle -q task1 I'm task number 1
Manipulating existing tasks
Once tasks are registered, they can be accessed via an API. For instance, you could use this to dynamically add dependencies to a task, at runtime. Ant doesn’t allow anything like this.
repeat(4) { counter ->
tasks.register("task$counter") {
doLast {
println("I'm task number $counter")
}
}
}
tasks.named("task0") { dependsOn("task2", "task3") }
4.times { counter ->
tasks.register("task$counter") {
doLast {
println "I'm task number $counter"
}
}
}
tasks.named('task0') { dependsOn('task2', 'task3') }
gradle -q task0
> gradle -q task0 I'm task number 2 I'm task number 3 I'm task number 0
Or you can add behavior to an existing task.
tasks.register("hello") {
doLast {
println("Hello Earth")
}
}
tasks.named("hello") {
doFirst {
println("Hello Venus")
}
}
tasks.named("hello") {
doLast {
println("Hello Mars")
}
}
tasks.named("hello") {
doLast {
println("Hello Jupiter")
}
}
tasks.register('hello') {
doLast {
println 'Hello Earth'
}
}
tasks.named('hello') {
doFirst {
println 'Hello Venus'
}
}
tasks.named('hello') {
doLast {
println 'Hello Mars'
}
}
tasks.named('hello') {
doLast {
println 'Hello Jupiter'
}
}
gradle -q hello
> gradle -q hello Hello Venus Hello Earth Hello Mars Hello Jupiter
The calls doFirst
and doLast
can be executed multiple times. They add an action to the beginning or the end of the task’s actions list. When the task executes, the actions in the action list are executed in order.
Using Ant Tasks
Ant tasks are first-class citizens in Gradle. Gradle provides excellent integration for Ant tasks by simply relying on Groovy. Groovy is shipped with the fantastic AntBuilder
. Using Ant tasks from Gradle is as convenient and more powerful than using Ant tasks from a build.xml
file. And it is usable from Kotlin too. From the example below, you can learn how to execute Ant tasks and how to access Ant properties:
tasks.register("loadfile") {
val resourceDirectory = file("./antLoadfileResources")
doLast {
val files = resourceDirectory.listFiles().sorted()
files.forEach { file ->
if (file.isFile) {
ant.withGroovyBuilder {
"loadfile"("srcFile" to file, "property" to file.name)
}
println(" *** ${file.name} ***")
println("${ant.properties[file.name]}")
}
}
}
}
tasks.register('loadfile') {
def resourceDirectory = file('./antLoadfileResources')
doLast {
def files = resourceDirectory.listFiles().sort()
files.each { File file ->
if (file.isFile()) {
ant.loadfile(srcFile: file, property: file.name)
println " *** $file.name ***"
println "${ant.properties[file.name]}"
}
}
}
}
gradle -q loadfile
> gradle -q loadfile *** agile.manifesto.txt *** Individuals and interactions over processes and tools Working software over comprehensive documentation Customer collaboration over contract negotiation Responding to change over following a plan *** gradle.manifesto.txt *** Make the impossible possible, make the possible easy and make the easy elegant. (inspired by Moshe Feldenkrais)
There is lots more you can do with Ant in your build scripts. You can find out more in Ant.
Using methods
Gradle scales in how you can organize your build logic. The first level of organizing your build logic for the example above, is extracting a method.
tasks.register("checksum") {
doLast {
fileList("./antLoadfileResources").forEach { file ->
ant.withGroovyBuilder {
"checksum"("file" to file, "property" to "cs_${file.name}")
}
println("$file.name Checksum: ${ant.properties["cs_${file.name}"]}")
}
}
}
tasks.register("loadfile") {
doLast {
fileList("./antLoadfileResources").forEach { file ->
ant.withGroovyBuilder {
"loadfile"("srcFile" to file, "property" to file.name)
}
println("I'm fond of ${file.name}")
}
}
}
fun fileList(dir: String): List<File> =
file(dir).listFiles { file: File -> file.isFile }.sorted()
tasks.register('checksum') {
doLast {
fileList('./antLoadfileResources').each { File file ->
ant.checksum(file: file, property: "cs_$file.name")
println "$file.name Checksum: ${ant.properties["cs_$file.name"]}"
}
}
}
tasks.register('loadfile') {
doLast {
fileList('./antLoadfileResources').each { File file ->
ant.loadfile(srcFile: file, property: file.name)
println "I'm fond of $file.name"
}
}
}
File[] fileList(String dir) {
file(dir).listFiles({file -> file.isFile() } as FileFilter).sort()
}
gradle -q loadfile
> gradle -q loadfile I'm fond of agile.manifesto.txt I'm fond of gradle.manifesto.txt
Warning
|
Accessing top-level methods and variables is not yet compatible with the configuration cache. |
Later you will see that such methods can be shared among subprojects in multi-project builds. If your build logic becomes more complex, Gradle offers you other very convenient ways to organize it. We have devoted a whole chapter to this. See Organizing Gradle Projects.
Default tasks
Gradle allows you to define one or more default tasks that are executed if no other tasks are specified.
defaultTasks("clean", "run")
tasks.register("clean") {
doLast {
println("Default Cleaning!")
}
}
tasks.register("run") {
doLast {
println("Default Running!")
}
}
tasks.register("other") {
doLast {
println("I'm not a default task!")
}
}
defaultTasks 'clean', 'run'
tasks.register('clean') {
doLast {
println 'Default Cleaning!'
}
}
tasks.register('run') {
doLast {
println 'Default Running!'
}
}
tasks.register('other') {
doLast {
println "I'm not a default task!"
}
}
gradle -q
> gradle -q Default Cleaning! Default Running!
This is equivalent to running gradle clean run
. In a multi-project build every subproject can have its own specific default tasks. If a subproject does not specify default tasks, the default tasks of the parent project are used (if defined).
External dependencies for the build script
Note
|
Instead of manipulating the script classpath directly, it is recommended to apply plugins that come with their own classpath. For custom build logic, the recommendation is to use a custom plugin. |
If your build script needs to use external libraries, you can add them to the script’s classpath in the build script itself. You do this using the buildscript()
method, passing in a block which declares the build script classpath.
buildscript {
repositories {
mavenCentral()
}
dependencies {
"classpath"(group = "commons-codec", name = "commons-codec", version = "1.2")
}
}
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath group: 'commons-codec', name: 'commons-codec', version: '1.2'
}
}
The block passed to the buildscript()
method configures a ScriptHandler instance.
You declare the build script classpath by adding dependencies to the classpath
configuration.
This is the same way you declare, for example, the Java compilation classpath.
You can use any of the dependency types except project dependencies.
Having declared the build script classpath, you can use the classes in your build script as you would any other classes on the classpath. The following example adds to the previous example, and uses classes from the build script classpath.
import org.apache.commons.codec.binary.Base64
buildscript {
repositories {
mavenCentral()
}
dependencies {
"classpath"(group = "commons-codec", name = "commons-codec", version = "1.2")
}
}
tasks.register("encode") {
doLast {
val encodedString = Base64().encode("hello world\n".toByteArray())
println(String(encodedString))
}
}
import org.apache.commons.codec.binary.Base64
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath group: 'commons-codec', name: 'commons-codec', version: '1.2'
}
}
tasks.register('encode') {
doLast {
def byte[] encodedString = new Base64().encode('hello world\n'.getBytes())
println new String(encodedString)
}
}
gradle -q encode
> gradle -q encode aGVsbG8gd29ybGQK
For multi-project builds, the dependencies declared with a project’s buildscript()
method are available to the build scripts of all its sub-projects.
Build script dependencies may be Gradle plugins. Please consult Using Gradle Plugins for more information on Gradle plugins.
Every project automatically has a buildEnvironment
task of type BuildEnvironmentReportTask that can be invoked to report on the resolution of the build script dependencies.
Further Reading
This chapter only scratched the surface with what’s possible. Here are some other topics that may be interesting:
Authoring Tasks
In the introductory tutorial you learned how to create simple tasks. You also learned how to add additional behavior to these tasks later on, and you learned how to create dependencies between tasks. This was all about simple tasks, but Gradle takes the concept of tasks further. Gradle supports tasks that have their own properties and methods. Such tasks are either provided by you or built into Gradle.
Task outcomes
When Gradle executes a task, it can label the task with different outcomes in the console UI and via the Tooling API. These labels are based on if a task has actions to execute, if it should execute those actions, if it did execute those actions and if those actions made any changes.
(no label)
orEXECUTED
-
Task executed its actions.
-
Task has actions and Gradle has determined they should be executed as part of a build.
-
Task has no actions and some dependencies, and any of the dependencies are executed. See also Lifecycle Tasks.
-
UP-TO-DATE
-
Task’s outputs did not change.
-
Task has outputs and inputs and they have not changed. See Incremental Build.
-
Task has actions, but the task tells Gradle it did not change its outputs.
-
Task has no actions and some dependencies, but all of the dependencies are up-to-date, skipped or from cache. See also Lifecycle Tasks.
-
Task has no actions and no dependencies.
-
FROM-CACHE
-
Task’s outputs could be found from a previous execution.
-
Task has outputs restored from the build cache. See Build Cache.
-
SKIPPED
-
Task did not execute its actions.
-
Task has been explicitly excluded from the command-line. See Excluding tasks from execution.
-
Task has an
onlyIf
predicate return false. See Using a predicate.
-
NO-SOURCE
-
Task did not need to execute its actions.
-
Task has inputs and outputs, but no sources. For example, source files are
.java
files for JavaCompile.
-
Defining tasks
We have already seen how to define tasks using strings for task names in this chapter. There are a few variations on this style, which you may need to use in certain situations.
Note
|
The task configuration APIs are described in more detail in the task configuration avoidance chapter. |
tasks.register("hello") {
doLast {
println("hello")
}
}
tasks.register<Copy>("copy") {
from(file("srcDir"))
into(buildDir)
}
tasks.register('hello') {
doLast {
println 'hello'
}
}
tasks.register('copy', Copy) {
from(file('srcDir'))
into(buildDir)
}
We add the tasks to the tasks
collection.
Have a look at TaskContainer for more variations of the register()
method.
In the Kotlin DSL there is also a specific delegated properties syntax that is useful if you need the registered task for further reference.
// Using Kotlin delegated properties
val hello by tasks.registering {
doLast {
println("hello")
}
}
val copy by tasks.registering(Copy::class) {
from(file("srcDir"))
into(buildDir)
}
// Assigning registered tasks to a variable in Groovy
def hello = tasks.register('hello') {
doLast {
println 'hello'
}
}
def copy = tasks.register('copy', Copy) {
from(file('srcDir'))
into(buildDir)
}
Warning
|
If you look at the API of the tasks container you may notice that there are additional methods to create tasks. The use of these methods is discouraged and will be deprecated in future versions. These methods only exist for backward compatibility as they were introduced before task configuration avoidance was added to Gradle. |
Locating tasks
You often need to locate the tasks that you have defined in the build file, for example, to configure them or use them for dependencies. There are a number of ways of doing this. Firstly, just like with defining tasks there are language specific syntaxes for the Groovy and Kotlin DSL:
In general, tasks are available through the tasks
collection.
You should use the methods that return a task provider – register()
or named()
– to make sure you do not break task configuration avoidance.
tasks.register("hello")
tasks.register<Copy>("copy")
println(tasks.named("hello").get().name) // or just 'tasks.hello' if the task was added by a plugin
println(tasks.named<Copy>("copy").get().destinationDir)
tasks.register('hello')
tasks.register('copy', Copy)
println tasks.named('hello').get().name
println tasks.named('copy').get().destinationDir
Tasks of a specific type can also be accessed by using the tasks.withType()
method.
This enables to easily avoid duplication of code and reduce redundancy.
tasks.withType<Tar>().configureEach {
enabled = false
}
tasks.register("test") {
dependsOn(tasks.withType<Copy>())
}
tasks.withType(Tar).configureEach {
enabled = false
}
tasks.register('test') {
dependsOn tasks.withType(Copy)
}
Warning
|
The following shows how to access a task by path. This is not a recommended practice anymore as it breaks task configuration avoidance and project isolation. Dependencies between projects should be declared as dependencies. |
You can access tasks from any project using the task’s path using the tasks.getByPath()
method.
You can call the getByPath()
method with a task name, or a relative path, or an absolute path.
tasks.register("hello")
tasks.register("hello")
println(tasks.getByPath("hello").path)
println(tasks.getByPath(":hello").path)
println(tasks.getByPath("project-a:hello").path)
println(tasks.getByPath(":project-a:hello").path)
tasks.register('hello')
tasks.register('hello')
println tasks.getByPath('hello').path
println tasks.getByPath(':hello').path
println tasks.getByPath('project-a:hello').path
println tasks.getByPath(':project-a:hello').path
gradle -q hello
> gradle -q hello :hello :hello :project-a:hello :project-a:hello
Have a look at TaskContainer for more options for locating tasks.
Configuring tasks
As an example, let’s look at the Copy
task provided by Gradle.
To register a Copy
task for your build, you can declare in your build script:
tasks.register<Copy>("myCopy")
tasks.register('myCopy', Copy)
This registers a copy task with no default behavior. The task can be configured using its API (see Copy). The following examples show several different ways to achieve the same configuration.
Just to be clear, realize that the name of this task is myCopy
, but it is of type Copy
.
You can have multiple tasks of the same type, but with different names.
You’ll find this gives you a lot of power to implement cross-cutting concerns across all tasks of a particular type.
tasks.named<Copy>("myCopy") {
from("resources")
into("target")
include("**/*.txt", "**/*.xml", "**/*.properties")
}
tasks.named('myCopy') {
from 'resources'
into 'target'
include('**/*.txt', '**/*.xml', '**/*.properties')
}
You can also store the task reference in a variable and use to configure the task further at a later point in the script.
// Configure task using Kotlin delegated properties and a lambda
val myCopy by tasks.existing(Copy::class) {
from("resources")
into("target")
}
myCopy {
include("**/*.txt", "**/*.xml", "**/*.properties")
}
// Configure task through a task provider
def myCopy = tasks.named('myCopy') {
from 'resources'
into 'target'
}
myCopy.configure {
include('**/*.txt', '**/*.xml', '**/*.properties')
}
Have a look at TaskContainer for more options for configuring tasks.
Tip
|
If you use the Kotlin DSL and the task you want to configure was added by a plugin, you can use a convenient accessor for the task.
That is, instead of |
You can also use a configuration block when you define a task.
tasks.register<Copy>("copy") {
from("resources")
into("target")
include("**/*.txt", "**/*.xml", "**/*.properties")
}
tasks.register('copy', Copy) {
from 'resources'
into 'target'
include('**/*.txt', '**/*.xml', '**/*.properties')
}
Tip
|
Don’t forget about the build phases
A task has both configuration and actions. When using the |
Passing arguments to a task constructor
As opposed to configuring the mutable properties of a Task
after creation, you can pass argument values to the Task
class’s constructor.
In order to pass values to the Task
constructor, you must annotate the relevant constructor with @javax.inject.Inject
.
@Inject
constructorabstract class CustomTask @Inject constructor(
private val message: String,
private val number: Int
) : DefaultTask()
abstract class CustomTask extends DefaultTask {
private final String message
private final int number
@Inject
CustomTask(String message, int number) {
this.message = message
this.number = number
}
}
You can then create a task, passing the constructor arguments at the end of the parameter list.
tasks.register<CustomTask>("myTask", "hello", 42)
tasks.register('myTask', CustomTask, 'hello', 42)
Note
|
It’s recommended to use the Task Configuration Avoidance APIs to improve configuration time. |
In all circumstances, the values passed as constructor arguments must be non-null.
If you attempt to pass a null
value, Gradle will throw a NullPointerException
indicating which runtime value is null
.
Adding dependencies to a task
There are several ways you can define the dependencies of a task. In Task dependencies you were introduced to defining dependencies using task names. Task names can refer to tasks in the same project as the task, or to tasks in other projects. To refer to a task in another project, you prefix the name of the task with the path of the project it belongs to. The following is an example which adds a dependency from project-a:taskX
to project-b:taskY
:
project("project-a") {
tasks.register("taskX") {
dependsOn(":project-b:taskY")
doLast {
println("taskX")
}
}
}
project("project-b") {
tasks.register("taskY") {
doLast {
println("taskY")
}
}
}
project('project-a') {
tasks.register('taskX') {
dependsOn ':project-b:taskY'
doLast {
println 'taskX'
}
}
}
project('project-b') {
tasks.register('taskY') {
doLast {
println 'taskY'
}
}
}
gradle -q taskX
> gradle -q taskX taskY taskX
Instead of using a task name, you can define a dependency using a TaskProvider
object, as shown in this example:
val taskX by tasks.registering {
doLast {
println("taskX")
}
}
val taskY by tasks.registering {
doLast {
println("taskY")
}
}
taskX {
dependsOn(taskY)
}
def taskX = tasks.register('taskX') {
doLast {
println 'taskX'
}
}
def taskY = tasks.register('taskY') {
doLast {
println 'taskY'
}
}
taskX.configure {
dependsOn taskY
}
gradle -q taskX
> gradle -q taskX taskY taskX
For more advanced uses, you can define a task dependency using a lazy block.
When evaluated, the block is passed the task whose dependencies are being calculated.
The lazy block should return a single Task
or collection of Task
objects, which are then treated as dependencies of the task.
The following example adds a dependency from taskX
to all the tasks in the project whose name starts with lib
:
val taskX by tasks.registering {
doLast {
println("taskX")
}
}
// Using a Gradle Provider
taskX {
dependsOn(provider {
tasks.filter { task -> task.name.startsWith("lib") }
})
}
tasks.register("lib1") {
doLast {
println("lib1")
}
}
tasks.register("lib2") {
doLast {
println("lib2")
}
}
tasks.register("notALib") {
doLast {
println("notALib")
}
}
def taskX = tasks.register('taskX') {
doLast {
println 'taskX'
}
}
// Using a Gradle Provider
taskX.configure {
dependsOn(provider {
tasks.findAll { task -> task.name.startsWith('lib') }
})
}
tasks.register('lib1') {
doLast {
println('lib1')
}
}
tasks.register('lib2') {
doLast {
println('lib2')
}
}
tasks.register('notALib') {
doLast {
println('notALib')
}
}
gradle -q taskX
> gradle -q taskX lib1 lib2 taskX
For more information about task dependencies, see the Task API.
Ordering tasks
In some cases it is useful to control the order in which 2 tasks will execute, without introducing an explicit dependency between those tasks. The primary difference between a task ordering and a task dependency is that an ordering rule does not influence which tasks will be executed, only the order in which they will be executed.
Task ordering can be useful in a number of scenarios:
-
Enforce sequential ordering of tasks: e.g. 'build' never runs before 'clean'.
-
Run build validations early in the build: e.g. validate I have the correct credentials before starting the work for a release build.
-
Get feedback faster by running quick verification tasks before long verification tasks: e.g. unit tests should run before integration tests.
-
A task that aggregates the results of all tasks of a particular type: e.g. test report task combines the outputs of all executed test tasks.
There are two ordering rules available: “must run after” and “should run after”.
When you use the “must run after” ordering rule you specify that taskB
must always run after taskA
, whenever both taskA
and taskB
will be run. This is expressed as taskB.mustRunAfter(taskA)
. The “should run after” ordering rule is similar but less strict as it will be ignored in two situations. Firstly if using that rule introduces an ordering cycle. Secondly when using parallel execution and all dependencies of a task have been satisfied apart from the “should run after” task, then this task will be run regardless of whether its “should run after” dependencies have been run or not. You should use “should run after” where the ordering is helpful but not strictly required.
With these rules present it is still possible to execute taskA
without taskB
and vice-versa.
val taskX by tasks.registering {
doLast {
println("taskX")
}
}
val taskY by tasks.registering {
doLast {
println("taskY")
}
}
taskY {
mustRunAfter(taskX)
}
def taskX = tasks.register('taskX') {
doLast {
println 'taskX'
}
}
def taskY = tasks.register('taskY') {
doLast {
println 'taskY'
}
}
taskY.configure {
mustRunAfter taskX
}
gradle -q taskY taskX
> gradle -q taskY taskX taskX taskY
val taskX by tasks.registering {
doLast {
println("taskX")
}
}
val taskY by tasks.registering {
doLast {
println("taskY")
}
}
taskY {
shouldRunAfter(taskX)
}
def taskX = tasks.register('taskX') {
doLast {
println 'taskX'
}
}
def taskY = tasks.register('taskY') {
doLast {
println 'taskY'
}
}
taskY.configure {
shouldRunAfter taskX
}
gradle -q taskY taskX
> gradle -q taskY taskX taskX taskY
In the examples above, it is still possible to execute taskY
without causing taskX
to run:
gradle -q taskY
> gradle -q taskY taskY
To specify a “must run after” or “should run after” ordering between 2 tasks, you use the Task.mustRunAfter(java.lang.Object...) and Task.shouldRunAfter(java.lang.Object...) methods. These methods accept a task instance, a task name or any other input accepted by Task.dependsOn(java.lang.Object...).
Note that “B.mustRunAfter(A)
” or “B.shouldRunAfter(A)
” does not imply any execution dependency between the tasks:
-
It is possible to execute tasks
A
andB
independently. The ordering rule only has an effect when both tasks are scheduled for execution. -
When run with
--continue
, it is possible forB
to execute in the event thatA
fails.
As mentioned before, the “should run after” ordering rule will be ignored if it introduces an ordering cycle:
val taskX by tasks.registering {
doLast {
println("taskX")
}
}
val taskY by tasks.registering {
doLast {
println("taskY")
}
}
val taskZ by tasks.registering {
doLast {
println("taskZ")
}
}
taskX { dependsOn(taskY) }
taskY { dependsOn(taskZ) }
taskZ { shouldRunAfter(taskX) }
def taskX = tasks.register('taskX') {
doLast {
println 'taskX'
}
}
def taskY = tasks.register('taskY') {
doLast {
println 'taskY'
}
}
def taskZ = tasks.register('taskZ') {
doLast {
println 'taskZ'
}
}
taskX.configure { dependsOn(taskY) }
taskY.configure { dependsOn(taskZ) }
taskZ.configure { shouldRunAfter(taskX) }
gradle -q taskX
> gradle -q taskX taskZ taskY taskX
Adding a description to a task
You can add a description to your task. This description is displayed when executing gradle tasks
.
tasks.register<Copy>("copy") {
description = "Copies the resource directory to the target directory."
from("resources")
into("target")
include("**/*.txt", "**/*.xml", "**/*.properties")
}
tasks.register('copy', Copy) {
description 'Copies the resource directory to the target directory.'
from 'resources'
into 'target'
include('**/*.txt', '**/*.xml', '**/*.properties')
}
Skipping tasks
Gradle offers multiple ways to skip the execution of a task.
Using a predicate
You can use Task.onlyIf
to attach a predicate to a task. The task’s actions are only executed if the predicate evaluates to true. The predicate is passed the task as a parameter, and should return true if the task should execute and false if the task should be skipped. The predicate is evaluated just before the task is executed.
Passing an optional reason string to onlyIf()
is useful for explaining why the task is skipped.
val hello by tasks.registering {
doLast {
println("hello world")
}
}
hello {
val skipProvider = providers.gradleProperty("skipHello")
onlyIf("there is no property skipHello") {
!skipProvider.isPresent()
}
}
def hello = tasks.register('hello') {
doLast {
println 'hello world'
}
}
hello.configure {
def skipProvider = providers.gradleProperty("skipHello")
onlyIf("there is no property skipHello") {
!skipProvider.present
}
}
gradle hello -PskipHello
> gradle hello -PskipHello > Task :hello SKIPPED BUILD SUCCESSFUL in 0s
It is possible to find the reason for a task being skipped by running the build with the --info
logging level.
gradle hello -PskipHello --hello
> gradle hello -PskipHello --info ... > Task :hello SKIPPED Skipping task ':hello' as task onlyIf 'there is no property skipHello' is false. :hello (Thread[included builds,5,main]) completed. Took 0.018 secs. BUILD SUCCESSFUL in 13s
Using StopExecutionException
If the logic for skipping a task can’t be expressed with a predicate, you can use the StopExecutionException. If this exception is thrown by an action, the further execution of this action as well as the execution of any following action of this task is skipped. The build continues with executing the next task.
val compile by tasks.registering {
doLast {
println("We are doing the compile.")
}
}
compile {
doFirst {
// Here you would put arbitrary conditions in real life.
if (true) {
throw StopExecutionException()
}
}
}
tasks.register("myTask") {
dependsOn(compile)
doLast {
println("I am not affected")
}
}
def compile = tasks.register('compile') {
doLast {
println 'We are doing the compile.'
}
}
compile.configure {
doFirst {
// Here you would put arbitrary conditions in real life.
if (true) {
throw new StopExecutionException()
}
}
}
tasks.register('myTask') {
dependsOn('compile')
doLast {
println 'I am not affected'
}
}
gradle -q myTask
> gradle -q myTask I am not affected
This feature is helpful if you work with tasks provided by Gradle. It allows you to add conditional execution of the built-in actions of such a task.[3]
Enabling and disabling tasks
Every task has an enabled
flag which defaults to true
. Setting it to false
prevents the execution of any of the task’s actions. A disabled task will be labelled SKIPPED.
val disableMe by tasks.registering {
doLast {
println("This should not be printed if the task is disabled.")
}
}
disableMe {
enabled = false
}
def disableMe = tasks.register('disableMe') {
doLast {
println 'This should not be printed if the task is disabled.'
}
}
disableMe.configure {
enabled = false
}
gradle disableMe
> gradle disableMe > Task :disableMe SKIPPED BUILD SUCCESSFUL in 0s
Task timeouts
Every task has a timeout
property which can be used to limit its execution time.
When a task reaches its timeout, its task execution thread is interrupted.
The task will be marked as failed. Finalizer tasks will still be run.
If --continue
is used, other tasks can continue running after it.
Tasks that don’t respond to interrupts can’t be timed out.
All of Gradle’s built-in tasks respond to timeouts in a timely manner.
tasks.register("hangingTask") {
doLast {
Thread.sleep(100000)
}
timeout.set(Duration.ofMillis(500))
}
tasks.register("hangingTask") {
doLast {
Thread.sleep(100000)
}
timeout = Duration.ofMillis(500)
}
Task rules
Sometimes you want to have a task whose behavior depends on a large or infinite number value range of parameters. A very nice and expressive way to provide such tasks are task rules:
tasks.addRule("Pattern: ping<ID>") {
val taskName = this
if (startsWith("ping")) {
task(taskName) {
doLast {
println("Pinging: " + (taskName.replace("ping", "")))
}
}
}
}
tasks.addRule("Pattern: ping<ID>") { String taskName ->
if (taskName.startsWith("ping")) {
task(taskName) {
doLast {
println "Pinging: " + (taskName - 'ping')
}
}
}
}
gradle -q pingServer1
> gradle -q pingServer1 Pinging: Server1
The String parameter is used as a description for the rule, which is shown with gradle tasks
.
Rules are not only used when calling tasks from the command line. You can also create dependsOn relations on rule based tasks:
tasks.addRule("Pattern: ping<ID>") {
val taskName = this
if (startsWith("ping")) {
task(taskName) {
doLast {
println("Pinging: " + (taskName.replace("ping", "")))
}
}
}
}
tasks.register("groupPing") {
dependsOn("pingServer1", "pingServer2")
}
tasks.addRule("Pattern: ping<ID>") { String taskName ->
if (taskName.startsWith("ping")) {
task(taskName) {
doLast {
println "Pinging: " + (taskName - 'ping')
}
}
}
}
tasks.register('groupPing') {
dependsOn 'pingServer1', 'pingServer2'
}
gradle -q groupPing
> gradle -q groupPing Pinging: Server1 Pinging: Server2
If you run gradle -q tasks
you won’t find a task named pingServer1
or pingServer2
, but this script is executing logic based on the request to run those tasks.
Finalizer tasks
Finalizer tasks are automatically added to the task graph when the finalized task is scheduled to run.
val taskX by tasks.registering {
doLast {
println("taskX")
}
}
val taskY by tasks.registering {
doLast {
println("taskY")
}
}
taskX { finalizedBy(taskY) }
def taskX = tasks.register('taskX') {
doLast {
println 'taskX'
}
}
def taskY = tasks.register('taskY') {
doLast {
println 'taskY'
}
}
taskX.configure { finalizedBy taskY }
gradle -q taskX
> gradle -q taskX taskX taskY
Finalizer tasks will be executed even if the finalized task fails or if the finalized task is considered up to date.
val taskX by tasks.registering {
doLast {
println("taskX")
throw RuntimeException()
}
}
val taskY by tasks.registering {
doLast {
println("taskY")
}
}
taskX { finalizedBy(taskY) }
def taskX = tasks.register('taskX') {
doLast {
println 'taskX'
throw new RuntimeException()
}
}
def taskY = tasks.register('taskY') {
doLast {
println 'taskY'
}
}
taskX.configure { finalizedBy taskY }
gradle -q taskX
> gradle -q taskX taskX taskY FAILURE: Build failed with an exception. * Where: Build file '/home/user/gradle/samples/build.gradle' line: 4 * What went wrong: Execution failed for task ':taskX'. > java.lang.RuntimeException (no error message) * Try: > Run with --stacktrace option to get the stack trace. > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. * Get more help at https://help.gradle.org BUILD FAILED in 0s
Finalizer tasks are useful in situations where the build creates a resource that has to be cleaned up regardless of the build failing or succeeding. An example of such a resource is a web container that is started before an integration test task and which should be always shut down, even if some of the tests fail.
To specify a finalizer task you use the Task.finalizedBy(java.lang.Object…) method. This method accepts a task instance, a task name, or any other input accepted by Task.dependsOn(java.lang.Object…).
Lifecycle tasks
Lifecycle tasks are tasks that do not do work themselves. They typically do not have any task actions. Lifecycle tasks can represent several concepts:
-
a work-flow step (e.g., run all checks with
check
) -
a buildable thing (e.g., create a debug 32-bit executable for native components with
debug32MainExecutable
) -
a convenience task to execute many of the same logical tasks (e.g., run all compilation tasks with
compileAll
)
The Base Plugin defines several standard lifecycle tasks, such as build
, assemble
, and check
. All the core language plugins, like the Java Plugin, apply the Base Plugin and hence have the same base set of lifecycle tasks.
Unless a lifecycle task has actions, its outcome is determined by its task dependencies. If any of those dependencies are executed, the lifecycle task will be considered EXECUTED
. If all of the task dependencies are up to date, skipped or from cache, the lifecycle task will be considered UP-TO-DATE
.
Summary
If you are coming from Ant, an enhanced Gradle task like Copy seems like a cross between an Ant target and an Ant task. Although Ant’s tasks and targets are really different entities, Gradle combines these notions into a single entity. Simple Gradle tasks are like Ant’s targets, but enhanced Gradle tasks also include aspects of Ant tasks. All of Gradle’s tasks share a common API and you can create dependencies between them. These tasks are much easier to configure than an Ant task. They make full use of the type system, and are more expressive and easier to maintain.
Moved documentation
Some documentation previously appearing in this chapter has been moved to the Incremental Build chapter.
Up-to-date checks (AKA Incremental Build)
Moved to the Incremental Build chapter.
Custom task types
Moved to a section under Incremental Build.
Nested inputs
Moved to a section under Incremental Build.
Runtime validation
Moved to a section under Incremental Build.
Runtime API
Moved to a section under Incremental Build.
Continuous build
Moved to a section under Incremental Build.
Task parallelism
Moved to a section under Incremental Build.
How does it work?
Moved to a section under Incremental Build.
Advanced techniques
Moved to a section under Incremental Build.
Integrate an external tool which does its own up-to-date checking
Moved to a section under Incremental Build.
Stale task outputs
Moved to a section under Incremental Build.
Incremental build
An important part of any build tool is the ability to avoid doing work that has already been done. Consider the process of compilation. Once your source files have been compiled, there should be no need to recompile them unless something has changed that affects the output, such as the modification of a source file or the removal of an output file. And compilation can take a significant amount of time, so skipping the step when it’s not needed saves a lot of time.
Gradle supports this behavior out of the box through a feature called incremental build.
You have almost certainly already seen it in action.
When you run a task and the task is marked with UP-TO-DATE
in the console output, this means incremental build is at work.
How does an incremental build work? How can you make sure your tasks support running incrementally? Let’s take a look.
Task inputs and outputs
In the most common case, a task takes some inputs and generates some outputs. We can consider the process of Java compilation as an example of a task. The Java source files act as inputs of the task, while the generated class files, i.e. the result of the compilation, are the outputs of the task.

An important characteristic of an input is that it affects one or more outputs, as you can see from the previous figure. Different bytecode is generated depending on the content of the source files and the minimum version of the Java runtime you want to run the code on. That makes them task inputs. But whether compilation has 500MB or 600MB of maximum memory available, determined by the memoryMaximumSize
property, has no impact on what bytecode gets generated. In Gradle terminology, memoryMaximumSize
is just an internal task property.
As part of incremental build, Gradle tests whether any of the task inputs or outputs has changed since the last build. If they haven’t, Gradle can consider the task up to date and therefore skip executing its actions. Also note that incremental build won’t work unless a task has at least one task output, although tasks usually have at least one input as well.
What this means for build authors is simple: you need to tell Gradle which task properties are inputs and which are outputs. If a task property affects the output, be sure to register it as an input, otherwise the task will be considered up to date when it’s not. Conversely, don’t register properties as inputs if they don’t affect the output, otherwise the task will potentially execute when it doesn’t need to. Also be careful of non-deterministic tasks that may generate different output for exactly the same inputs: these should not be configured for incremental build as the up-to-date checks won’t work.
Let’s now look at how you can register task properties as inputs and outputs.
Declaring inputs and outputs via annotations
If you’re implementing a custom task as a class, then it takes just two steps to make it work with incremental build:
-
Create typed properties (via getter methods) for each of your task inputs and outputs
-
Add the appropriate annotation to each of those properties
Note
|
Annotations must be placed on getters or on Groovy properties. Annotations placed on setters, or on a Java field without a corresponding annotated getter, are ignored. |
Gradle supports four main categories of inputs and outputs:
-
Simple values
Things like strings and numbers. More generally, a simple value can have any type that implements
Serializable
. -
Filesystem types
These consist of
RegularFile
,Directory
and the standardFile
class but also derivatives of Gradle’s FileCollection type and anything else that can be passed to either the Project.file(java.lang.Object) method — for single file/directory properties — or the Project.files(java.lang.Object...) method. -
Dependency resolution results
This includes the ResolvedArtifactResult type for artifact metadata and the ResolvedComponentResult type for dependency graphs. Note that they are only supported wrapped in a
Provider
. -
Nested values
Custom types that don’t conform to the other two categories but have their own properties that are inputs or outputs. In effect, the task inputs or outputs are nested inside these custom types.
As an example, imagine you have a task that processes templates of varying types, such as FreeMarker, Velocity, Moustache, etc. It takes template source files and combines them with some model data to generate populated versions of the template files.
This task will have three inputs and one output:
-
Template source files
-
Model data
-
Template engine
-
Where the output files are written
When you’re writing a custom task class, it’s easy to register properties as inputs or outputs via annotations. To demonstrate, here is a skeleton task implementation with some suitable inputs and outputs, along with their annotations:
package org.example;
import java.util.HashMap;
import org.gradle.api.DefaultTask;
import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.file.FileSystemOperations;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.*;
import javax.inject.Inject;
public abstract class ProcessTemplates extends DefaultTask {
@Input
public abstract Property<TemplateEngineType> getTemplateEngine();
@InputFiles
public abstract ConfigurableFileCollection getSourceFiles();
@Nested
public abstract TemplateData getTemplateData();
@OutputDirectory
public abstract DirectoryProperty getOutputDir();
@Inject
public abstract FileSystemOperations getFs();
@TaskAction
public void processTemplates() {
// ...
}
}
package org.example;
import org.gradle.api.provider.MapProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Input;
public abstract class TemplateData {
@Input
public abstract Property<String> getName();
@Input
public abstract MapProperty<String, String> getVariables();
}
gradle processTemplates
> gradle processTemplates > Task :processTemplates BUILD SUCCESSFUL in 0s 3 actionable tasks: 3 up-to-date
gradle processTemplates
(run again)> gradle processTemplates > Task :processTemplates UP-TO-DATE BUILD SUCCESSFUL in 0s 3 actionable tasks: 3 up-to-date
There’s plenty to talk about in this example, so let’s work through each of the input and output properties in turn:
-
templateEngine
Represents which engine to use when processing the source templates, e.g. FreeMarker, Velocity, etc. You could implement this as a string, but in this case we have gone for a custom enum as it provides greater type information and safety. Since enums implement
Serializable
automatically, we can treat this as a simple value and use the@Input
annotation, just as we would with aString
property. -
sourceFiles
The source templates that the task will be processing. Single files and collections of files need their own special annotations. In this case, we’re dealing with a collection of input files and so we use the
@InputFiles
annotation. You’ll see more file-oriented annotations in a table later. -
templateData
For this example, we’re using a custom class to represent the model data. However, it does not implement
Serializable
, so we can’t use the@Input
annotation. That’s not a problem as the properties withinTemplateData
— a string and a hash map with serializable type parameters — are serializable and can be annotated with@Input
. We use@Nested
ontemplateData
to let Gradle know that this is a value with nested input properties. -
outputDir
The directory where the generated files go. As with input files, there are several annotations for output files and directories. A property representing a single directory requires
@OutputDirectory
. You’ll learn about the others soon.
These annotated properties mean that Gradle will skip the task if none of the source files, template engine, model data or generated files has changed since the previous time Gradle executed the task. This will often save a significant amount of time. You can learn how Gradle detects changes later.
This example is particularly interesting because it works with collections of source files. What happens if only one source file changes? Does the task process all the source files again or just the modified one? That depends on the task implementation. If the latter, then the task itself is incremental, but that’s a different feature to the one we’re discussing here. Gradle does help task implementers with this via its incremental task inputs feature.
Now that you have seen some of the input and output annotations in practice, let’s take a look at all the annotations available to you and when you should use them. The table below lists the available annotations and the corresponding property type you can use with each one.
Annotation | Expected property type | Description | ||
---|---|---|---|---|
Any |
A simple input value or dependency resolution results |
|||
|
A single input file (not directory) |
|||
|
A single input directory (not file) |
|||
|
An iterable of input files and directories |
|||
|
An iterable of input files and directories that represent a Java classpath. This allows the task to ignore irrelevant changes to the property, such as different names for the same files. It is similar to annotating the property Note: The |
|||
|
An iterable of input files and directories that represent a Java compile classpath. This allows the task to ignore irrelevant changes that do not affect the API of the classes in classpath. See also Using the classpath annotations. The following kinds of changes to the classpath will be ignored:
|
|||
|
A single output file (not directory) |
|||
|
A single output directory (not file) |
|||
|
An iterable or map of output files. Using a file tree turns caching off for the task. |
|||
|
An iterable of output directories. Using a file tree turns caching off for the task. |
|||
|
Specifies one or more files that are removed by this task. Note that a task can define either inputs/outputs or destroyables, but not both. |
|||
|
Specifies one or more files that represent the local state of the task. These files are removed when the task is loaded from cache. |
|||
Any custom type |
A custom type that may not implement |
|||
Any type |
Indicates that the property is neither an input nor an output. It simply affects the console output of the task in some way, such as increasing or decreasing the verbosity of the task. |
|||
Any type |
Indicates that the property is used internally but is neither an input nor an output. |
|||
Any type |
Indicates that the property has been replaced by another and should be ignored as an input or output. |
|||
|
Used with Implies |
|||
|
Used with |
|||
Any type |
Used with any of the property type annotations listed in the Optional API documentation. This annotation disables validation checks on the corresponding property. See the section on validation for more details. |
|||
|
||||
|
Used with |
|||
|
Used with |
Note
|
|
Annotations are inherited from all parent types including implemented interfaces. Property type annotations override any other property type annotation declared in a parent type. This way an @InputFile
property can be turned into an @InputDirectory
property in a child task type.
Annotations on a property declared in a type override similar annotations declared by the superclass and in any implemented interfaces. Superclass annotations take precedence over annotations declared in implemented interfaces.
The Console and Internal annotations in the table are special cases as they don’t declare either task inputs or task outputs. So why use them? It’s so that you can take advantage of the Java Gradle Plugin Development plugin to help you develop and publish your own plugins. This plugin checks whether any properties of your custom task classes lack an incremental build annotation. This protects you from forgetting to add an appropriate annotation during development.
Using dependency resolution results
Dependency resolution results can be consumed as task inputs in two ways. First by consuming the graph of the resolved metadata using ResolvedComponentResult. Second by consuming the flat set of the resolved artifacts using ResolvedArtifactResult.
A resolved graph can be obtained lazily from the incoming resolution result of a Configuration
and wired to an @Input
property:
link:https://docs.gradle.org/8.1.1/samples/writing-tasks/tasks-with-dependency-resolution-result-inputs/common/dependency-reports/src/main/java/com/example/GraphResolvedComponents.java[tag=inputs]
link:https://docs.gradle.org/8.1.1/samples/writing-tasks/tasks-with-dependency-resolution-result-inputs/common/dependency-reports/src/main/java/com/example/DependencyReportsPlugin.java[tag=graphResolvedComponents]
The resolved set of artifacts can be obtained lazily from the incoming artifacts of a Configuration
.
Given the ResolvedArtifactResult
type contains both metadata and file information, instances need to be transformed to metadata only before being wired to an @Input
property:
link:https://docs.gradle.org/8.1.1/samples/writing-tasks/tasks-with-dependency-resolution-result-inputs/common/dependency-reports/src/main/java/com/example/ListResolvedArtifacts.java[tag=inputs]
link:https://docs.gradle.org/8.1.1/samples/writing-tasks/tasks-with-dependency-resolution-result-inputs/common/dependency-reports/src/main/java/com/example/DependencyReportsPlugin.java[tag=listResolvedArtifacts]
Both graph and flat results can be combined and augmented with resolved file information. This is all demonstrated in the Tasks with dependency resolution result inputs sample.
Using the classpath annotations
Besides @InputFiles
, for JVM-related tasks Gradle understands the concept of classpath inputs. Both runtime and compile classpaths are treated differently when Gradle is looking for changes.
As opposed to input properties annotated with @InputFiles
, for classpath properties the order of the entries in the file collection matter.
On the other hand, the names and paths of the directories and jar files on the classpath itself are ignored.
Timestamps and the order of class files and resources inside jar files on a classpath are ignored, too, thus recreating a jar file with different file dates will not make the task out of date.
Runtime classpaths are marked with @Classpath
, and they offer further customization via classpath normalization.
Input properties annotated with @CompileClasspath
are considered Java compile classpaths.
Additionally to the aforementioned general classpath rules, compile classpaths ignore changes to everything but class files. Gradle uses the same class analysis described in Java compile avoidance to further filter changes that don’t affect the class' ABIs.
This means that changes which only touch the implementation of classes do not make the task out of date.
Nested inputs
When analyzing @Nested
task properties for declared input and output sub-properties Gradle uses the type of the actual value.
Hence it can discover all sub-properties declared by a runtime sub-type.
When adding @Nested
to an iterable, each element is treated as a separate nested input.
Each nested input in the iterable is assigned a name, which by default is the dollar sign followed by the index in the iterable, e.g. $2
.
If an element of the iterable implements Named
, then the name is used as property name.
The ordering of the elements in the iterable is crucial for reliable up-to-date checks and caching if not all of the elements implement Named
.
Multiple elements which have the same name are not allowed.
When adding @Nested
to a map, then for each value a nested input is added, using the key as name.
The type and classpath of nested inputs is tracked, too.
This ensures that changes to the implementation of a nested input causes the build to be out of date.
By this it is also possible to add user provided code as an input, e.g. by annotating an @Action
property with @Nested
.
Note that any inputs to such actions should be tracked, either by annotated properties on the action or by manually registering them with the task.
Using nested inputs allows richer modeling and extensibility for tasks, as e.g. shown by Test.getJvmArgumentProviders().
This allows us to model the JaCoCo Java agent, thus declaring the necessary JVM arguments and providing the inputs and outputs to Gradle:
class JacocoAgent implements CommandLineArgumentProvider {
private final JacocoTaskExtension jacoco;
public JacocoAgent(JacocoTaskExtension jacoco) {
this.jacoco = jacoco;
}
@Nested
@Optional
public JacocoTaskExtension getJacoco() {
return jacoco.isEnabled() ? jacoco : null;
}
@Override
public Iterable<String> asArguments() {
return jacoco.isEnabled() ? ImmutableList.of(jacoco.getAsJvmArg()) : Collections.<String>emptyList();
}
}
test.getJvmArgumentProviders().add(new JacocoAgent(extension));
For this to work, JacocoTaskExtension
needs to have the correct input and output annotations.
The approach works for Test JVM arguments, since Test.getJvmArgumentProviders()
is an Iterable
annotated with @Nested
.
There are other task types where this kind of nested inputs are available:
-
JavaExec.getArgumentProviders() - model e.g. custom tools
-
JavaExec.getJvmArgumentProviders() - used for Jacoco Java agent
-
CompileOptions.getCompilerArgumentProviders() - model e.g. annotation processors
-
Exec.getArgumentProviders() - model e.g. custom tools
-
JavaCompile.getOptions().getForkOptions().getJvmArgumentProviders() - model Java compiler daemon command line arguments
-
GroovyCompile.getGroovyOptions().getForkOptions().getJvmArgumentProviders() - model Groovy compiler daemon command line arguments
-
ScalaCompile.getScalaOptions().getForkOptions().getJvmArgumentProviders() - model Scala compiler daemon command line arguments
In the same way, this kind of modelling is available to custom tasks.
Validation at runtime
When executing the build Gradle checks if task types are declared with the proper annotations. It tries to identify problems where e.g. annotations are used on incompatible types, or on setters etc. Any getter not annotated with an input/output annotation is also flagged. These problems then fail the build or are turned into deprecation warnings when the task is executed.
Tasks that have a validation warning are executed without any optimizations. Specifically, they never can be:
-
up-to-date,
-
loaded from or stored in the build cache,
-
executed in parallel with other tasks, even if parallel execution is enabled,
-
executed incrementally.
The in-memory representation of the file system state (Virtual File System) is also invalidated before an invalid task is executed.
Declaring inputs and outputs via the runtime API
Custom task classes are an easy way to bring your own build logic into the arena of incremental build, but you don’t always have that option. That’s why Gradle also provides an alternative API that can be used with any tasks, which we look at next.
When you don’t have access to the source for a custom task class, there is no way to add any of the annotations we covered in the previous section. Fortunately, Gradle provides a runtime API for scenarios just like that. It can also be used for ad-hoc tasks, as you’ll see next.
Declaring inputs and outputs of ad-hoc tasks
This runtime API is provided through a couple of aptly named properties that are available on every Gradle task:
-
Task.getInputs() of type TaskInputs
-
Task.getOutputs() of type TaskOutputs
These objects have methods that allow you to specify files, directories and values which constitute the task’s inputs and outputs. In fact, the runtime API has almost feature parity with the annotations.
It lacks equivalents for
Let’s take the template processing example from before and see how it would look as an ad-hoc task that uses the runtime API:
tasks.register("processTemplatesAdHoc") {
inputs.property("engine", TemplateEngineType.FREEMARKER)
inputs.files(fileTree("src/templates"))
.withPropertyName("sourceFiles")
.withPathSensitivity(PathSensitivity.RELATIVE)
inputs.property("templateData.name", "docs")
inputs.property("templateData.variables", mapOf("year" to "2013"))
outputs.dir(layout.buildDirectory.dir("genOutput2"))
.withPropertyName("outputDir")
doLast {
// Process the templates here
}
}
tasks.register('processTemplatesAdHoc') {
inputs.property('engine', TemplateEngineType.FREEMARKER)
inputs.files(fileTree('src/templates'))
.withPropertyName('sourceFiles')
.withPathSensitivity(PathSensitivity.RELATIVE)
inputs.property('templateData.name', 'docs')
inputs.property('templateData.variables', [year: '2013'])
outputs.dir(layout.buildDirectory.dir('genOutput2'))
.withPropertyName('outputDir')
doLast {
// Process the templates here
}
}
gradle processTemplatesAdHoc
> gradle processTemplatesAdHoc > Task :processTemplatesAdHoc BUILD SUCCESSFUL in 0s 3 actionable tasks: 3 executed
As before, there’s much to talk about. To begin with, you should really write a custom task class for this as it’s a non-trivial implementation that has several configuration options. In this case, there are no task properties to store the root source folder, the location of the output directory or any of the other settings. That’s deliberate to highlight the fact that the runtime API doesn’t require the task to have any state. In terms of incremental build, the above ad-hoc task will behave the same as the custom task class.
All the input and output definitions are done through the methods on inputs
and outputs
, such as property()
, files()
, and dir()
.
Gradle performs up-to-date checks on the argument values to determine whether the task needs to run again or not.
Each method corresponds to one of the incremental build annotations, for example inputs.property()
maps to @Input
and outputs.dir()
maps to @OutputDirectory
.
The files that a task removes can be specified through destroyables.register()
.
tasks.register("removeTempDir") {
val tmpDir = layout.projectDirectory.dir("tmpDir")
destroyables.register(tmpDir)
doLast {
tmpDir.asFile.deleteRecursively()
}
}
tasks.register('removeTempDir') {
def tempDir = layout.projectDirectory.dir('tmpDir')
destroyables.register(tempDir)
doLast {
tempDir.asFile.deleteDir()
}
}
One notable difference between the runtime API and the annotations is the lack of a method that corresponds directly to @Nested
. That’s why the example uses two property()
declarations for the template data, one for each TemplateData
property. You should utilize the same technique when using the runtime API with nested values. Any given task can either declare destroyables or inputs/outputs, but cannot declare both.
Fine-grained configuration
The runtime API methods only allow you to declare your inputs and outputs in themselves. However, the file-oriented ones return a builder — of type TaskInputFilePropertyBuilder — that lets you provide additional information about those inputs and outputs.
You can learn about all the options provided by the builder in its API documentation, but we’ll show you a simple example here to give you an idea of what you can do.
Let’s say we don’t want to run the processTemplates
task if there are no source files, regardless of whether it’s a clean build or not. After all, if there are no source files, there’s nothing for the task to do. The builder allows us to configure this like so:
tasks.register("processTemplatesAdHocSkipWhenEmpty") {
// ...
inputs.files(fileTree("src/templates") {
include("**/*.fm")
})
.skipWhenEmpty()
.withPropertyName("sourceFiles")
.withPathSensitivity(PathSensitivity.RELATIVE)
.ignoreEmptyDirectories()
// ...
}
tasks.register('processTemplatesAdHocSkipWhenEmpty') {
// ...
inputs.files(fileTree('src/templates') {
include '**/*.fm'
})
.skipWhenEmpty()
.withPropertyName('sourceFiles')
.withPathSensitivity(PathSensitivity.RELATIVE)
.ignoreEmptyDirectories()
// ...
}
gradle clean processTemplatesAdHocSkipWhenEmpty
> gradle clean processTemplatesAdHocSkipWhenEmpty > Task :processTemplatesAdHocSkipWhenEmpty NO-SOURCE BUILD SUCCESSFUL in 0s 3 actionable tasks: 2 executed, 1 up-to-date
The TaskInputs.files()
method returns a builder that has a skipWhenEmpty()
method. Invoking this method is equivalent to annotating to the property with @SkipWhenEmpty
.
Now that you have seen both the annotations and the runtime API, you may be wondering which API you should be using. Our recommendation is to use the annotations wherever possible, and it’s sometimes worth creating a custom task class just so that you can make use of them. The runtime API is more for situations in which you can’t use the annotations.
Declaring inputs and outputs for custom task types
Another type of example involves registering additional inputs and outputs for instances of a custom task class.
For example, imagine that the ProcessTemplates
task also needs to read src/headers/headers.txt
(e.g. because it is included from one of the sources).
You’d want Gradle to know about this input file, so that it can re-execute the task whenever the contents of this file change.
With the runtime API you can do just that:
tasks.register<ProcessTemplates>("processTemplatesWithExtraInputs") {
// ...
inputs.file("src/headers/headers.txt")
.withPropertyName("headers")
.withPathSensitivity(PathSensitivity.NONE)
}
tasks.register('processTemplatesWithExtraInputs', ProcessTemplates) {
// ...
inputs.file('src/headers/headers.txt')
.withPropertyName('headers')
.withPathSensitivity(PathSensitivity.NONE)
}
Using the runtime API like this is a little like using doLast()
and doFirst()
to attach extra actions to a task, except in this case we’re attaching information about inputs and outputs.
Warning
|
If the task type is already using the incremental build annotations, registering inputs or outputs with the same property names will result in an error. |
Benefits of declaring task inputs and outputs
Once you declare a task’s formal inputs and outputs, Gradle can then infer things about those properties. For example, if an input of one task is set to the output of another, that means the first task depends on the second, right? Gradle knows this and can act upon it.
We’ll look at this feature next and also some other features that come from Gradle knowing things about inputs and outputs.
Inferred task dependencies
Consider an archive task that packages the output of the processTemplates
task. A build author will see that the archive task obviously requires processTemplates
to run first and so may add an explicit dependsOn
. However, if you define the archive task like so:
tasks.register<Zip>("packageFiles") {
from(processTemplates.map { it.outputDir })
}
tasks.register('packageFiles', Zip) {
from processTemplates.map { it.outputDir }
}
gradle clean packageFiles
> gradle clean packageFiles > Task :processTemplates > Task :packageFiles BUILD SUCCESSFUL in 0s 5 actionable tasks: 4 executed, 1 up-to-date
Gradle will automatically make packageFiles
depend on processTemplates
. It can do this because it’s aware that one of the inputs of packageFiles requires the output of the processTemplates task. We call this an inferred task dependency.
The above example can also be written as
tasks.register<Zip>("packageFiles2") {
from(processTemplates)
}
tasks.register('packageFiles2', Zip) {
from processTemplates
}
gradle clean packageFiles2
> gradle clean packageFiles2 > Task :processTemplates > Task :packageFiles2 BUILD SUCCESSFUL in 0s 5 actionable tasks: 4 executed, 1 up-to-date
This is because the from()
method can accept a task object as an argument. Behind the scenes, from()
uses the project.files()
method to wrap the argument, which in turn exposes the task’s formal outputs as a file collection. In other words, it’s a special case!
Input and output validation
The incremental build annotations provide enough information for Gradle to perform some basic validation on the annotated properties. In particular, it does the following for each property before the task executes:
-
@InputFile
- verifies that the property has a value and that the path corresponds to a file (not a directory) that exists. -
@InputDirectory
- same as for@InputFile
, except the path must correspond to a directory. -
@OutputDirectory
- verifies that the path doesn’t match a file and also creates the directory if it doesn’t already exist.
If one task produces an output in a location and another task consumes that location by referring to it as an input, then Gradle checks that the consumer task depends on the producer task. When the producer and the consumer tasks are executing at the same time, the build fails to avoid capturing an incorrect state.
Such validation improves the robustness of the build, allowing you to identify issues related to inputs and outputs quickly.
You will occasionally want to disable some of this validation, specifically when an input file may validly not exist. That’s why Gradle provides the @Optional
annotation: you use it to tell Gradle that a particular input is optional and therefore the build should not fail if the corresponding file or directory doesn’t exist.
Continuous build
Another benefit of defining task inputs and outputs is continuous build. Since Gradle knows what files a task depends on, it can automatically run a task again if any of its inputs change. By activating continuous build when you run Gradle — through the --continuous
or -t
options — you will put Gradle into a state in which it continually checks for changes and executes the requested tasks when it encounters such changes.
You can find out more about this feature in Continuous build.
Task parallelism
One last benefit of defining task inputs and outputs is that Gradle can use this information to make decisions about how to run tasks when the "--parallel" option is used. For instance, Gradle will inspect the outputs of tasks when selecting the next task to run and will avoid concurrent execution of tasks that write to the same output directory. Similarly, Gradle will use the information about what files a task destroys (e.g. specified by the Destroys
annotation) and avoid running a task that removes a set of files while another task is running that consumes or creates those same files (and vice versa). It can also determine that a task that creates a set of files has already run and that a task that consumes those files has yet to run and will avoid running a task that removes those files in between. By providing task input and output information in this way, Gradle can infer creation/consumption/destruction relationships between tasks and can ensure that task execution does not violate those relationships.
How does it work?
Before a task is executed for the first time, Gradle takes a fingerprint of the inputs. This fingerprint contains the paths of input files and a hash of the contents of each file. Gradle then executes the task. If the task completes successfully, Gradle takes a fingerprint of the outputs. This fingerprint contains the set of output files and a hash of the contents of each file. Gradle persists both fingerprints for the next time the task is executed.
Each time after that, before the task is executed, Gradle takes a new fingerprint of the inputs and outputs. If the new fingerprints are the same as the previous fingerprints, Gradle assumes that the outputs are up to date and skips the task. If they are not the same, Gradle executes the task. Gradle persists both fingerprints for the next time the task is executed.
If the stats of a file (i.e. lastModified
and size
) did not change, Gradle will reuse the file’s fingerprint from the previous run.
That means that Gradle does not detect changes when the stats of a file did not change.
Gradle also considers the code of the task as part of the inputs to the task. When a task, its actions, or its dependencies change between executions, Gradle considers the task as out-of-date.
Gradle understands if a file property (e.g. one holding a Java classpath) is order-sensitive. When comparing the fingerprint of such a property, even a change in the order of the files will result in the task becoming out-of-date.
Note that if a task has an output directory specified, any files added to that directory since the last time it was executed are ignored and will NOT cause the task to be out of date. This is so unrelated tasks may share an output directory without interfering with each other. If this is not the behaviour you want for some reason, consider using TaskOutputs.upToDateWhen(groovy.lang.Closure)
Note also that changing the availability of an unavailable file (e.g. modifying the target of a broken symlink to a valid file, or vice versa), will be detected and handled by up-to-date check.
The inputs for the task are also used to calculate the build cache key used to load task outputs when enabled. For more details see Task output caching.
Note
|
For tracking the implementation of tasks, task actions and nested inputs, Gradle uses the class name and an identifier for the classpath which contains the implementation. There are some situations when Gradle is not able to track the implementation precisely:
When the implementation of a task, task action or a nested input cannot be tracked precisely, Gradle disables any caching for the task. That means that the task will never be up-to-date or loaded from the build cache. |
Advanced techniques
Everything you’ve seen so far in this section will cover most of the use cases you’ll encounter, but there are some scenarios that need special treatment. We’ll present a few of those next with the appropriate solutions.
Adding your own cached input/output methods
Have you ever wondered how the from()
method of the Copy
task works? It’s not annotated with @InputFiles
and yet any files passed to it are treated as formal inputs of the task. What’s happening?
The implementation is quite simple and you can use the same technique for your own tasks to improve their APIs. Write your methods so that they add files directly to the appropriate annotated property. As an example, here’s how to add a sources()
method to the custom ProcessTemplates
class we introduced earlier:
tasks.register<ProcessTemplates>("processTemplates") {
templateEngine.set(TemplateEngineType.FREEMARKER)
templateData.name.set("test")
templateData.variables.set(mapOf("year" to "2012"))
outputDir.set(file(layout.buildDirectory.dir("genOutput")))
sources(fileTree("src/templates"))
}
tasks.register('processTemplates', ProcessTemplates) {
templateEngine = TemplateEngineType.FREEMARKER
templateData.name = 'test'
templateData.variables = [year: '2012']
outputDir = file(layout.buildDirectory.dir('genOutput'))
sources fileTree('src/templates')
}
public abstract class ProcessTemplates extends DefaultTask {
// ...
@SkipWhenEmpty
@InputFiles
@PathSensitive(PathSensitivity.NONE)
public abstract ConfigurableFileCollection getSourceFiles();
public void sources(FileCollection sourceFiles) {
getSourceFiles().from(sourceFiles);
}
// ...
}
gradle processTemplates
> gradle processTemplates > Task :processTemplates BUILD SUCCESSFUL in 0s 3 actionable tasks: 3 executed
In other words, as long as you add values and files to formal task inputs and outputs during the configuration phase, they will be treated as such regardless from where in the build you add them.
If we want to support tasks as arguments as well and treat their outputs as the inputs, we can use the TaskProvider
directly like so:
val copyTemplates by tasks.registering(Copy::class) {
into(file(layout.buildDirectory.dir("tmp")))
from("src/templates")
}
tasks.register<ProcessTemplates>("processTemplates2") {
// ...
sources(copyTemplates)
}
def copyTemplates = tasks.register('copyTemplates', Copy) {
into file(layout.buildDirectory.dir('tmp'))
from 'src/templates'
}
tasks.register('processTemplates2', ProcessTemplates) {
// ...
sources copyTemplates
}
// ...
public void sources(TaskProvider<?> inputTask) {
getSourceFiles().from(inputTask);
}
// ...
gradle processTemplates2
> gradle processTemplates2 > Task :copyTemplates > Task :processTemplates2 BUILD SUCCESSFUL in 0s 4 actionable tasks: 4 executed
This technique can make your custom task easier to use and result in cleaner build files.
As an added benefit, our use of TaskProvider
means that our custom method can set up an inferred task dependency.
One last thing to note: if you are developing a task that takes collections of source files as inputs, like this example, consider using the built-in SourceTask. It will save you having to implement some of the plumbing that we put into ProcessTemplates
.
Linking an @OutputDirectory
to an @InputFiles
When you want to link the output of one task to the input of another, the types often match and a simple property assignment will provide that link. For example, a File
output property can be assigned to a File
input.
Unfortunately, this approach breaks down when you want the files in a task’s @OutputDirectory
(of type File
) to become the source for another task’s @InputFiles
property (of type FileCollection
). Since the two have different types, property assignment won’t work.
As an example, imagine you want to use the output of a Java compilation task — via the destinationDir
property — as the input of a custom task that instruments a set of files containing Java bytecode. This custom task, which we’ll call Instrument
, has a classFiles
property annotated with @InputFiles
. You might initially try to configure the task like so:
plugins {
id("java-library")
}
tasks.register<Instrument>("badInstrumentClasses") {
classFiles.from(fileTree(tasks.compileJava.flatMap { it.destinationDirectory }))
destinationDir.set(file(layout.buildDirectory.dir("instrumented")))
}
plugins {
id 'java-library'
}
tasks.register('badInstrumentClasses', Instrument) {
classFiles.from fileTree(tasks.named('compileJava').flatMap { it.destinationDirectory }) {}
destinationDir = file(layout.buildDirectory.dir('instrumented'))
}
gradle clean badInstrumentClasses
> gradle clean badInstrumentClasses > Task :clean UP-TO-DATE > Task :badInstrumentClasses NO-SOURCE BUILD SUCCESSFUL in 0s 3 actionable tasks: 2 executed, 1 up-to-date
There’s nothing obviously wrong with this code, but you can see from the console output that the compilation task is missing. In this case you would need to add an explicit task dependency between instrumentClasses
and compileJava
via dependsOn
. The use of fileTree()
means that Gradle can’t infer the task dependency itself.
One solution is to use the TaskOutputs.files
property, as demonstrated by the following example:
tasks.register<Instrument>("instrumentClasses") {
classFiles.from(tasks.compileJava.map { it.outputs.files })
destinationDir.set(file(layout.buildDirectory.dir("instrumented")))
}
tasks.register('instrumentClasses', Instrument) {
classFiles.from tasks.named('compileJava').map { it.outputs.files }
destinationDir = file(layout.buildDirectory.dir('instrumented'))
}
gradle clean instrumentClasses
> gradle clean instrumentClasses > Task :clean UP-TO-DATE > Task :compileJava > Task :instrumentClasses BUILD SUCCESSFUL in 0s 5 actionable tasks: 4 executed, 1 up-to-date
Alternatively, you can get Gradle to access the appropriate property itself by using one of project.files()
, project.layout.files()
or project.objects.fileCollection()
in place of project.fileTree()
:
tasks.register<Instrument>("instrumentClasses2") {
classFiles.from(layout.files(tasks.compileJava))
destinationDir.set(file(layout.buildDirectory.dir("instrumented")))
}
tasks.register('instrumentClasses2', Instrument) {
classFiles.from layout.files(tasks.named('compileJava'))
destinationDir = file(layout.buildDirectory.dir('instrumented'))
}
gradle clean instrumentClasses2
> gradle clean instrumentClasses2 > Task :clean UP-TO-DATE > Task :compileJava > Task :instrumentClasses2 BUILD SUCCESSFUL in 0s 5 actionable tasks: 4 executed, 1 up-to-date
Remember that files()
, layout.files()
and objects.fileCollection()
can take tasks as arguments, whereas fileTree()
cannot.
The downside of this approach is that all file outputs of the source task become the input files of the target — instrumentClasses
in this case. That’s fine as long as the source task only has a single file-based output, like the JavaCompile
task. But if you have to link just one output property among several, then you need to explicitly tell Gradle which task generates the input files using the builtBy
method:
tasks.register<Instrument>("instrumentClassesBuiltBy") {
classFiles.from(fileTree(tasks.compileJava.flatMap { it.destinationDirectory }) {
builtBy(tasks.compileJava)
})
destinationDir.set(file(layout.buildDirectory.dir("instrumented")))
}
tasks.register('instrumentClassesBuiltBy', Instrument) {
classFiles.from fileTree(tasks.named('compileJava').flatMap { it.destinationDirectory }) {
builtBy tasks.named('compileJava')
}
destinationDir = file(layout.buildDirectory.dir('instrumented'))
}
gradle clean instrumentClassesBuiltBy
> gradle clean instrumentClassesBuiltBy > Task :clean UP-TO-DATE > Task :compileJava > Task :instrumentClassesBuiltBy BUILD SUCCESSFUL in 0s 5 actionable tasks: 4 executed, 1 up-to-date
You can of course just add an explicit task dependency via dependsOn
, but the above approach provides more semantic meaning, explaining why compileJava
has to run beforehand.
Disabling up-to-date checks
Gradle automatically handles up-to-date checks for output files and directories, but what if the task output is something else entirely? Perhaps it’s an update to a web service or a database table. Or sometimes you have a task which should always run.
That’s where the doNotTrackState()
method on Task
comes in.
One can use this to disable up-to-date checks completely for a task, like so:
tasks.register<Instrument>("alwaysInstrumentClasses") {
classFiles.from(layout.files(tasks.compileJava))
destinationDir.set(file(layout.buildDirectory.dir("instrumented")))
doNotTrackState("Instrumentation needs to re-run every time")
}
tasks.register('alwaysInstrumentClasses', Instrument) {
classFiles.from layout.files(tasks.named('compileJava'))
destinationDir = file(layout.buildDirectory.dir('instrumented'))
doNotTrackState("Instrumentation needs to re-run every time")
}
gradle clean alwaysInstrumentClasses
> gradle clean alwaysInstrumentClasses > Task :compileJava > Task :alwaysInstrumentClasses BUILD SUCCESSFUL in 0s 4 actionable tasks: 1 executed, 3 up-to-date
gradle alwaysInstrumentClasses
> gradle alwaysInstrumentClasses > Task :compileJava UP-TO-DATE > Task :alwaysInstrumentClasses BUILD SUCCESSFUL in 0s 4 actionable tasks: 1 executed, 3 up-to-date
If you are writing your own task that always should run, then you can also use the @UntrackedTask
annotation on the task class instead of calling Task.doNotTrackState()
.
Integrate an external tool which does its own up-to-date checking
Sometimes you want to integrate an external tool like Git or Npm, both of which do their own up-to-date checking.
In that case it doesn’t make much sense for Gradle to also do up-to-date checks.
You can disable Gradle’s up-to-date checks by using the @UntrackedTask
annotation on the task wrapping the tool.
Alternatively, you can use the runtime API method Task.doNotTrackState()
.
For example, let’s say you want to implement a task which clones a Git repository.
@UntrackedTask(because = "Git tracks the state") // (1)
public abstract class GitClone extends DefaultTask {
@Input
public abstract Property<String> getRemoteUri();
@Input
public abstract Property<String> getCommitId();
@OutputDirectory
public abstract DirectoryProperty getDestinationDir();
@TaskAction
public void gitClone() throws IOException {
File destinationDir = getDestinationDir().get().getAsFile().getAbsoluteFile(); // (2)
String remoteUri = getRemoteUri().get();
// Fetch origin or clone and checkout
// ...
}
}
tasks.register<GitClone>("cloneGradleProfiler") {
destinationDir.set(layout.buildDirectory.dir("gradle-profiler")) // (3)
remoteUri.set("https://github.com/gradle/gradle-profiler.git")
commitId.set("d6c18a21ca6c45fd8a9db321de4478948bdf801b")
}
tasks.register("cloneGradleProfiler", GitClone) {
destinationDir = layout.buildDirectory.dir("gradle-profiler") // (3)
remoteUri = "https://github.com/gradle/gradle-profiler.git"
commitId = "d6c18a21ca6c45fd8a9db321de4478948bdf801b"
}
-
Declare the task as untracked.
-
Use the output directory to run the external tool.
-
Add the task and configure the output directory in your build.
Configure input normalization
For up to date checks and the build cache Gradle needs to determine if two task input properties have the same value. In order to do so, Gradle first normalizes both inputs and then compares the result. For example, for a compile classpath, Gradle extracts the ABI signature from the classes on the classpath and then compares signatures between the last Gradle run and the current Gradle run as described in Java compile avoidance.
Normalization applies to all zip files on the classpath (e.g. jars, wars, aars, apks, etc). This allows Gradle to recognize when two zip files are functionally the same, even though the zip files themselves might be slightly different due to metadata (such as timestamps or file order). Normalization applies not only to zip files directly on the classpath, but also to zip files nested inside directories or inside other zip files on the classpath.
It is possible to customize Gradle’s built-in strategy for runtime classpath normalization.
All inputs annotated with @Classpath
are considered to be runtime classpaths.
Let’s say you want to add a file build-info.properties
to all your produced jar files which contains information about the build, e.g. the timestamp when the build started or some ID to identify the CI job that published the artifact.
This file is only for auditing purposes, and has no effect on the outcome of running tests.
Nonetheless, this file is part of the runtime classpath for the test
task and changes on every build invocation.
Therefore, the test
would be never up-to-date or pulled from the build cache.
In order to benefit from incremental builds again, you are able tell Gradle to ignore this file on the runtime classpath at the project level by using Project.normalization(org.gradle.api.Action) (in the consuming project):
normalization {
runtimeClasspath {
ignore("build-info.properties")
}
}
normalization {
runtimeClasspath {
ignore 'build-info.properties'
}
}
If adding such a file to your jar files is something you do for all of the projects in your build, and you want to filter this file for all consumers, you should consider configuring such normalization in a convention plugin to share it between subprojects.
The effect of this configuration would be that changes to build-info.properties
would be ignored for up-to-date checks and build cache key calculations.
Note that this will not change the runtime behavior of the test
task — i.e. any test is still able to load build-info.properties
and the runtime classpath is still the same as before.
Properties file normalization
By default, properties files (i.e. files that end in a .properties
extension) will be normalized to ignore differences in comments, whitespace and the order of properties.
Gradle does this by loading the properties files and only considering the individual properties during up-to-date checks or build cache key calculations.
It is sometimes the case, though, that certain properties have a runtime impact, while others do not. If a property is changing that does not have an impact on the runtime classpath, it may be desirable to exclude it from up-to-date checks and build cache key calculations. However, excluding the entire file would also exclude the properties that do have a runtime impact. In this case, properties can be excluded selectively from any or all properties files on the runtime classpath.
A rule for ignoring properties can be applied to a specific set of files using the patterns described in RuntimeClasspathNormalization. In the event that a file matches a rule, but cannot be loaded as a properties file (e.g. because it is not formatted properly or uses a non-standard encoding), it will be incorporated into the up-to-date or build cache key calculation as a normal file. In other words, if the file cannot be loaded as a properties file, any changes to whitespace, property order, or comments may cause the task to become out-of-date or cause a cache miss.
normalization {
runtimeClasspath {
properties("**/build-info.properties") {
ignoreProperty("timestamp")
}
}
}
normalization {
runtimeClasspath {
properties('**/build-info.properties') {
ignoreProperty 'timestamp'
}
}
}
normalization {
runtimeClasspath {
properties {
ignoreProperty("timestamp")
}
}
}
normalization {
runtimeClasspath {
properties {
ignoreProperty 'timestamp'
}
}
}
Java META-INF
normalization
For files in the META-INF
directory of jar archives it’s not always possible to ignore files completely due to their runtime impact.
Manifest files within META-INF
are normalized to ignore comments, whitespace and order differences.
Manifest attribute names are compared case-and-order insensitively.
Manifest properties files are normalized according to Properties File Normalization.
META-INF
manifest attributesnormalization {
runtimeClasspath {
metaInf {
ignoreAttribute("Implementation-Version")
}
}
}
normalization {
runtimeClasspath {
metaInf {
ignoreAttribute("Implementation-Version")
}
}
}
META-INF
property keysnormalization {
runtimeClasspath {
metaInf {
ignoreProperty("app.version")
}
}
}
normalization {
runtimeClasspath {
metaInf {
ignoreProperty("app.version")
}
}
}
META-INF/MANIFEST.MF
normalization {
runtimeClasspath {
metaInf {
ignoreManifest()
}
}
}
normalization {
runtimeClasspath {
metaInf {
ignoreManifest()
}
}
}
META-INF
normalization {
runtimeClasspath {
metaInf {
ignoreCompletely()
}
}
}
normalization {
runtimeClasspath {
metaInf {
ignoreCompletely()
}
}
}
Providing custom up-to-date logic
Gradle automatically handles up-to-date checks for output files and directories, but what if the task output is something else entirely? Perhaps it’s an update to a web service or a database table. Gradle has no way of knowing how to check whether the task is up to date in such cases.
That’s where the upToDateWhen()
method on TaskOutputs
comes in.
This takes a predicate function that is used to determine whether a task is up to date or not.
For example, you could read the version number of your database schema from the database.
Or, you could check whether a particular record in a database table exists or has changed for example.
Just be aware that up-to-date checks should save you time. Don’t add checks that cost as much or more time than the standard execution of the task. In fact, if a task ends up running frequently anyway, because it’s rarely up to date, then it may not be worth having no up-to-date checks at all as described in Disabling up-to-date checks. Remember that your checks will always run if the task is in the execution task graph.
One common mistake is to use upToDateWhen()
instead of Task.onlyIf()
.
If you want to skip a task on the basis of some condition unrelated to the task inputs and outputs, then you should use onlyIf()
.
For example, in cases where you want to skip a task when a particular property is set or not set.
Stale task outputs
When the Gradle version changes, Gradle detects that outputs from tasks that ran with older versions of Gradle need to be removed to ensure that the newest version of the tasks are starting from a known clean state.
Note
|
Automatic clean-up of stale output directories has only been implemented for the output of source sets (Java/Groovy/Scala compilation). |
Writing Build Scripts
Use a build script to configure a project. Each Gradle project corresponds to a software component
that needs to be built, like a library or an application. Each build script is associated
with an object of type Project.
As the build script executes, it configures this Project
.
Important
|
Build scripts, Settings scripts, and Init scripts
Build scripts define
|
Properties
Many top-level properties and blocks in a build script are part of the Project
API.
The following build script uses the Project.name
property to print the name of the project:
println(name)
println(project.name)
println name
println project.name
gradle -q check
> gradle -q check project-api project-api
Both println
statements print out the same property. The first uses the top-level reference
to the name
property of the Project
object. The other statement uses the project
property
available to any build script, which returns the associated Project
object.
Standard project properties
The Project
object exposes a standard set of properties in your build script.
The following table lists a few commonly used properties:
Name | Type | Default Value |
---|---|---|
|
The |
|
|
|
The name of the project directory. |
|
|
The absolute path of the project. |
|
|
A description for the project. |
|
|
The directory containing the build script. |
|
|
|
|
|
|
|
|
|
|
An |
Important
|
Script with other targets
The build scripts described here target |
The Script API
When Gradle executes a Groovy build script (.gradle
), it compiles the script into a class that
implements Script. As a result, builds scripts
have access to all of the properties and methods declared by the Script
interface.
When Gradle executes a Kotlin build script (.gradle.kts
), it compiles the script into a subclass
of KotlinBuildScript.
As a result, builds scripts have access to all visible properties and functions declared by the KotlinBuildScript
type.
Declare Variables
Build scripts can declare two kinds of variables: local variables and extra properties.
Local Variables
Declare local variables with the val
keyword. Local variables are only visible in the scope where they have been declared. They are a feature of the underlying Kotlin language.
Declare local variables with the def
keyword. Local variables are only visible in the scope where they have been declared. They are a feature of the underlying Groovy language.
val dest = "dest"
tasks.register<Copy>("copy") {
from("source")
into(dest)
}
def dest = 'dest'
tasks.register('copy', Copy) {
from 'source'
into dest
}
Extra Properties
All of Gradle’s enhanced objects, including projects, tasks, and source sets, can hold user-defined properties.
Add, read, and set extra properties via the owning object’s extra
property. Alternatively, you can access extra properties via Kotlin delegated properties using by extra
.
Add, read, and set extra properties via the owning object’s ext
property. Alternatively, you can use an ext
block to add multiple properties at once.
plugins {
id("java-library")
}
val springVersion by extra("3.1.0.RELEASE")
val emailNotification by extra { "build@master.org" }
sourceSets.all { extra["purpose"] = null }
sourceSets {
main {
extra["purpose"] = "production"
}
test {
extra["purpose"] = "test"
}
create("plugin") {
extra["purpose"] = "production"
}
}
tasks.register("printProperties") {
val springVersion = springVersion
val emailNotification = emailNotification
val productionSourceSets = provider {
sourceSets.matching { it.extra["purpose"] == "production" }.map { it.name }
}
doLast {
println(springVersion)
println(emailNotification)
productionSourceSets.get().forEach { println(it) }
}
}
plugins {
id 'java-library'
}
ext {
springVersion = "3.1.0.RELEASE"
emailNotification = "build@master.org"
}
sourceSets.all { ext.purpose = null }
sourceSets {
main {
purpose = "production"
}
test {
purpose = "test"
}
plugin {
purpose = "production"
}
}
tasks.register('printProperties') {
def springVersion = springVersion
def emailNotification = emailNotification
def productionSourceSets = provider {
sourceSets.matching { it.purpose == "production" }.collect { it.name }
}
doLast {
println springVersion
println emailNotification
productionSourceSets.get().each { println it }
}
}
gradle -q printProperties
> gradle -q printProperties 3.1.0.RELEASE build@master.org main plugin
This example adds two extra properties to the project
object via by extra
. Additionally, this example adds a property named purpose
to each source set by setting extra["purpose"]
to null
. Once added, you can read and set all of these properties via extra
.
This example adds two extra properties to the project
object via an ext
block. Additionally, this example adds a property named purpose
to each source set by setting ext.purpose
to null
. Once added, you can read and set all of these properties just like predefined properties.
Gradle requires special syntax for adding a property so it can fail fast. For example, this allows Gradle to recognize when a script attempts to set a property that does not exist. You can access extra properties anywhere where you can access their owning object. This gives extra properties a wider scope than local variables. Subprojects can access extra properties on their parent projects.
For more information about extra properties, see ExtraPropertiesExtension in the API documentation.
Configure Arbitrary Objects
Configure arbitrary objects with configure()
:
class UserInfo(
var name: String? = null,
var email: String? = null
)
tasks.register("configure") {
val user = UserInfo().apply {
name = "Isaac Newton"
email = "isaac@newton.me"
}
doLast {
println(user.name)
println(user.email)
}
}
class UserInfo {
String name
String email
}
tasks.register('configure') {
def user = configure(new UserInfo()) {
name = "Isaac Newton"
email = "isaac@newton.me"
}
doLast {
println user.name
println user.email
}
}
gradle -q configure
> gradle -q configure Isaac Newton isaac@newton.me
Configure Objects in an External Script
You can also configure arbitrary objects using an external script:
Caution
|
Not supported in Kotlin
The Kotlin DSL does not support arbitrary object configuration using an external script. For more information, see gradle/kotlin-dsl#659. |
class UserInfo {
String name
String email
}
tasks.register('configure') {
def userInfo = new UserInfo()
// Apply the script
apply from: 'other.gradle', to: userInfo
doLast {
println userInfo.name
println userInfo.email
}
}
// Set properties.
name = "Isaac Newton"
email = "isaac@newton.me"
gradle -q configure
> gradle -q configure Isaac Newton isaac@newton.me
Helpful Groovy Syntax
Groovy provides many features to help create DSLs. These features can help you write more idiomatic build scripts, plugins, and tasks.
Tip
|
Looking for helpful Kotlin syntax? Check out the Kotlin reference documentation and Kotlin Koans. |
Convenience Methods
Groovy adds convenience methods to the standard Java classes.
For example, Iterable
gets an each
method, which iterates over the elements of the Iterable
:
// Iterable gets an each() method
configurations.runtimeClasspath.each { File f -> println f }
For more information, see the GDK documentation.
Automatic Property Accessors
Groovy automatically converts property references into getter or setter method calls:
// Using a getter method
println project.buildDir
println getProject().getBuildDir()
// Using a setter method
project.buildDir = 'target'
getProject().setBuildDir('target')
Optional Parentheses on Method Calls
Groovy doesn’t require parentheses for method calls:
test.systemProperty 'some.prop', 'value'
test.systemProperty('some.prop', 'value')
List and Map Instantiation Shortcuts
Groovy provides shortcuts to define List
and Map
instances.
For example, consider the “apply
” method, which accepts a map parameter.
When you apply a plugin (apply plugin:'java'
), you don’t use a map literal.
Plugin application uses "named parameters" instead. Named parameters and map
literals use similar syntax, but named parameters don’t use wrapping brackets.
When you call apply
, Groovy converts the named parameter list to a map.
// List literal
test.includes = ['org/gradle/api/**', 'org/gradle/internal/**']
List<String> list = new ArrayList<String>()
list.add('org/gradle/api/**')
list.add('org/gradle/internal/**')
test.includes = list
// Map literal.
Map<String, String> map = [key1:'value1', key2: 'value2']
// Groovy will coerce named arguments
// into a single map argument
apply plugin: 'java'
Last Closure Parameter Syntax
The Gradle DSL uses closures in many places. You can find out more about closures here. When the last parameter of a method is a closure, you can place the closure after the method call:
repositories {
println "in a closure"
}
repositories() { println "in a closure" }
repositories({ println "in a closure" })
Closure Delegates
Each closure has a delegate
object. Groovy uses this delegate to look up variable and method
references to nonlocal variables and closure parameters. Gradle uses this for configuration closures,
where the delegate
object refers to the object being configured.
dependencies {
assert delegate == project.dependencies
testImplementation('junit:junit:4.13')
delegate.testImplementation('junit:junit:4.13')
}
Default Imports
To make build scripts more concise, Gradle automatically adds a set of import statements to scripts.
As a result, instead of writing throw new org.gradle.api.tasks.StopExecutionException()
,
you can write throw new StopExecutionException()
instead.
Gradle implicitly adds the following imports to each script:
import org.gradle.*
import org.gradle.api.*
import org.gradle.api.artifacts.*
import org.gradle.api.artifacts.component.*
import org.gradle.api.artifacts.dsl.*
import org.gradle.api.artifacts.ivy.*
import org.gradle.api.artifacts.maven.*
import org.gradle.api.artifacts.query.*
import org.gradle.api.artifacts.repositories.*
import org.gradle.api.artifacts.result.*
import org.gradle.api.artifacts.transform.*
import org.gradle.api.artifacts.type.*
import org.gradle.api.artifacts.verification.*
import org.gradle.api.attributes.*
import org.gradle.api.attributes.java.*
import org.gradle.api.attributes.plugin.*
import org.gradle.api.cache.*
import org.gradle.api.capabilities.*
import org.gradle.api.component.*
import org.gradle.api.credentials.*
import org.gradle.api.distribution.*
import org.gradle.api.distribution.plugins.*
import org.gradle.api.execution.*
import org.gradle.api.file.*
import org.gradle.api.flow.*
import org.gradle.api.initialization.*
import org.gradle.api.initialization.definition.*
import org.gradle.api.initialization.dsl.*
import org.gradle.api.initialization.resolve.*
import org.gradle.api.invocation.*
import org.gradle.api.java.archives.*
import org.gradle.api.jvm.*
import org.gradle.api.launcher.cli.*
import org.gradle.api.logging.*
import org.gradle.api.logging.configuration.*
import org.gradle.api.model.*
import org.gradle.api.plugins.*
import org.gradle.api.plugins.antlr.*
import org.gradle.api.plugins.catalog.*
import org.gradle.api.plugins.jvm.*
import org.gradle.api.plugins.quality.*
import org.gradle.api.plugins.scala.*
import org.gradle.api.provider.*
import org.gradle.api.publish.*
import org.gradle.api.publish.ivy.*
import org.gradle.api.publish.ivy.plugins.*
import org.gradle.api.publish.ivy.tasks.*
import org.gradle.api.publish.maven.*
import org.gradle.api.publish.maven.plugins.*
import org.gradle.api.publish.maven.tasks.*
import org.gradle.api.publish.plugins.*
import org.gradle.api.publish.tasks.*
import org.gradle.api.reflect.*
import org.gradle.api.reporting.*
import org.gradle.api.reporting.components.*
import org.gradle.api.reporting.dependencies.*
import org.gradle.api.reporting.dependents.*
import org.gradle.api.reporting.model.*
import org.gradle.api.reporting.plugins.*
import org.gradle.api.resources.*
import org.gradle.api.services.*
import org.gradle.api.specs.*
import org.gradle.api.tasks.*
import org.gradle.api.tasks.ant.*
import org.gradle.api.tasks.application.*
import org.gradle.api.tasks.bundling.*
import org.gradle.api.tasks.compile.*
import org.gradle.api.tasks.diagnostics.*
import org.gradle.api.tasks.diagnostics.configurations.*
import org.gradle.api.tasks.incremental.*
import org.gradle.api.tasks.javadoc.*
import org.gradle.api.tasks.options.*
import org.gradle.api.tasks.scala.*
import org.gradle.api.tasks.testing.*
import org.gradle.api.tasks.testing.junit.*
import org.gradle.api.tasks.testing.junitplatform.*
import org.gradle.api.tasks.testing.testng.*
import org.gradle.api.tasks.util.*
import org.gradle.api.tasks.wrapper.*
import org.gradle.api.toolchain.management.*
import org.gradle.authentication.*
import org.gradle.authentication.aws.*
import org.gradle.authentication.http.*
import org.gradle.build.event.*
import org.gradle.buildinit.*
import org.gradle.buildinit.plugins.*
import org.gradle.buildinit.tasks.*
import org.gradle.caching.*
import org.gradle.caching.configuration.*
import org.gradle.caching.http.*
import org.gradle.caching.local.*
import org.gradle.concurrent.*
import org.gradle.external.javadoc.*
import org.gradle.ide.visualstudio.*
import org.gradle.ide.visualstudio.plugins.*
import org.gradle.ide.visualstudio.tasks.*
import org.gradle.ide.xcode.*
import org.gradle.ide.xcode.plugins.*
import org.gradle.ide.xcode.tasks.*
import org.gradle.ivy.*
import org.gradle.jvm.*
import org.gradle.jvm.application.scripts.*
import org.gradle.jvm.application.tasks.*
import org.gradle.jvm.tasks.*
import org.gradle.jvm.toolchain.*
import org.gradle.language.*
import org.gradle.language.assembler.*
import org.gradle.language.assembler.plugins.*
import org.gradle.language.assembler.tasks.*
import org.gradle.language.base.*
import org.gradle.language.base.artifact.*
import org.gradle.language.base.compile.*
import org.gradle.language.base.plugins.*
import org.gradle.language.base.sources.*
import org.gradle.language.c.*
import org.gradle.language.c.plugins.*
import org.gradle.language.c.tasks.*
import org.gradle.language.cpp.*
import org.gradle.language.cpp.plugins.*
import org.gradle.language.cpp.tasks.*
import org.gradle.language.java.artifact.*
import org.gradle.language.jvm.tasks.*
import org.gradle.language.nativeplatform.*
import org.gradle.language.nativeplatform.tasks.*
import org.gradle.language.objectivec.*
import org.gradle.language.objectivec.plugins.*
import org.gradle.language.objectivec.tasks.*
import org.gradle.language.objectivecpp.*
import org.gradle.language.objectivecpp.plugins.*
import org.gradle.language.objectivecpp.tasks.*
import org.gradle.language.plugins.*
import org.gradle.language.rc.*
import org.gradle.language.rc.plugins.*
import org.gradle.language.rc.tasks.*
import org.gradle.language.scala.tasks.*
import org.gradle.language.swift.*
import org.gradle.language.swift.plugins.*
import org.gradle.language.swift.tasks.*
import org.gradle.maven.*
import org.gradle.model.*
import org.gradle.nativeplatform.*
import org.gradle.nativeplatform.platform.*
import org.gradle.nativeplatform.plugins.*
import org.gradle.nativeplatform.tasks.*
import org.gradle.nativeplatform.test.*
import org.gradle.nativeplatform.test.cpp.*
import org.gradle.nativeplatform.test.cpp.plugins.*
import org.gradle.nativeplatform.test.cunit.*
import org.gradle.nativeplatform.test.cunit.plugins.*
import org.gradle.nativeplatform.test.cunit.tasks.*
import org.gradle.nativeplatform.test.googletest.*
import org.gradle.nativeplatform.test.googletest.plugins.*
import org.gradle.nativeplatform.test.plugins.*
import org.gradle.nativeplatform.test.tasks.*
import org.gradle.nativeplatform.test.xctest.*
import org.gradle.nativeplatform.test.xctest.plugins.*
import org.gradle.nativeplatform.test.xctest.tasks.*
import org.gradle.nativeplatform.toolchain.*
import org.gradle.nativeplatform.toolchain.plugins.*
import<