text:
Copyright © 2007-2016 Hans Dockter, Adam Murdoch
Table of Contents
List of Examples
We would like to introduce Gradle to you, a build system that we think is a quantum leap for build technology in the Java (JVM) world. Gradle provides:
A very flexible general purpose build tool like Ant.
Switchable, build-by-convention frameworks a la Maven. But we never lock you in!
Very powerful support for multi-project builds.
Very powerful dependency management (based on Apache Ivy).
Full support for your existing Maven or Ivy repository infrastructure.
Support for transitive dependency management without the need for remote repositories or
pom.xml and
ivy.xml files.
Ant tasks and builds as first class citizens.
Groovy build scripts.
A rich domain model for describing your build.
In Chapter 2, Overview you will find a detailed overview of Gradle. Otherwise, the tutorials are waiting, have fun :)
This user guide, like Gradle itself, is under very active development. Some parts of Gradle aren't documented as completely as they need to be. Some of the content presented won't be entirely clear or will assume that you know more about Gradle than you do. We need your help to improve this user guide. You can find out more about contributing to the documentation at the Gradle web site.
Throughout the user guide, you will find some diagrams that represent dependency relationships between Gradle tasks. These use something analogous to the UML dependency notation, which renders an arrow from one task to the task that the first task depends on.
Here is a list of some of Gradle's features.
At the heart of Gradle lies a rich extensible Domain Specific Language (DSL) based on Groovy. Gradle pushes declarative builds to the next level by providing declarative language elements that you can assemble as you like. Those elements also provide build-by-convention support for Java, Groovy, OSGi, Web and Scala projects. Even more, this declarative language is extensible. Add your own new language elements or enhance the existing ones, thus providing concise, maintainable and comprehensible builds.
The declarative language lies on top of a general purpose task graph, which you can fully leverage in your builds. It provides utmost flexibility to adapt Gradle to your unique needs.
The suppleness and richness of Gradle finally allows you to apply common design principles to your build. For example, it is very easy to compose your build from reusable pieces of build logic. Inline stuff where unnecessary indirections would be inappropriate. Don't be forced to tear apart what belongs together (e.g. in your project hierarchy). Avoid smells like shotgun changes or divergent change that turn your build into a maintenance nightmare. At last you can create a well structured, easily maintained, comprehensible build.
From being a pleasure to be used embedded to its many hooks over the whole lifecycle of build execution, Gradle allows you to monitor and customize its configuration and execution behavior to its very core.
Gradle scales very well. It significantly increases your productivity, from simple single project builds up to huge enterprise multi-project builds. This is true for structuring the build. With the state-of-art incremental build function, this is also true for tackling the performance pain many large enterprise builds suffer from.
Gradle's support for multi-project build is outstanding. Project dependencies are first class citizens. We allow you to model the project relationships in a multi-project build as they really are for your problem domain. Gradle follows your layout not vice versa.
Gradle provides partial builds. If you build a single subproject Gradle takes care of building all the subprojects that subproject depends on. You can also choose to rebuild the subprojects that depend on a particular subproject. Together with incremental builds this is a big time saver for larger builds.
Different teams prefer different ways to manage their external dependencies. Gradle provides convenient support for any strategy. From transitive dependency management with remote Maven and Ivy repositories to jars or directories on the local file system.
Ant tasks are first class citizens. Even more interesting, Ant projects are first class citizens as well. Gradle provides a deep import for any Ant project, turning Ant targets into native Gradle tasks at runtime. You can depend on them from Gradle, you can enhance them from Gradle, you can even declare dependencies on Gradle tasks in your build.xml. The same integration is provided for properties, paths, etc ...
Gradle fully supports your existing Maven or Ivy repository infrastructure for publishing and retrieving
dependencies. Gradle also provides a converter for turning a Maven
pom.xml into a Gradle script.
Runtime imports of Maven projects will come soon.
Gradle can adapt to any structure you have. Therefore you can always develop your Gradle build in the same branch where your production build lives and both can evolve in parallel. We usually recommend to write tests that make sure that the produced artifacts are similar. That way migration is as less disruptive and as reliable as possible. This is following the best-practices for refactoring by applying baby steps.
Gradle's build scripts are written in Groovy, not XML. But unlike other approaches this is not for simply exposing the raw scripting power of a dynamic language. That would just lead to a very difficult to maintain build. The whole design of Gradle is oriented towards being used as a language, not as a rigid framework. And Groovy is our glue that allows you to tell your individual story with the abstractions Gradle (or you) provide. Gradle provides some standard stories but they are not privileged in any form. This is for us a major distinguishing feature compared to other declarative build systems. Our Groovy support is not just sugar coating. The whole Gradle API is fully Groovy-ized. Adding Groovy results in an enjoyable and productive experience.
The Gradle Wrapper allows you to execute Gradle builds on machines where Gradle is not installed. This is useful for example for some continuous integration servers. It is also useful for an open source project to keep the barrier low for building it. The wrapper is also very interesting for the enterprise. It is a zero administration approach for the client machines. It also enforces the usage of a particular Gradle version thus minimizing support issues.
Gradle is an open source project, and is licensed under the ASL.
We think the advantages of an internal DSL (based on a dynamic language) over XML are tremendous when used in build scripts. There are a couple of dynamic languages out there. Why Groovy? The answer lies in the context Gradle is operating in. Although Gradle is a general purpose build tool at its core, its main focus are Java projects. In such projects the team members will be very familiar with Java. We think a build should be as transparent as possible to all team members.
In that case, you might argue why we don't just use Java as the language for build scripts. We think this is a valid question. It would have the highest transparency for your team and the lowest learning curve, but because of the limitations of Java, such a build language would not be as nice, expressive and powerful as it could be. [1] Languages like Python, Groovy or Ruby do a much better job here. We have chosen Groovy as it offers by far the greatest transparency for Java people. Its base syntax is the same as Java's as well as its type system, its package structure and other things. Groovy provides much more on top of that, but with the common foundation of Java.
For Java developers with Python or Ruby knowledge or the desire to learn them, the above arguments don't apply. The Gradle design is well-suited for creating another build script engine in JRuby or Jython. It just doesn't have the highest priority for us at the moment. We happily support any community effort to create additional build script engines.
[1] At http://www.defmacro.org/ramblings/lisp.html you find an interesting article comparing Ant, XML, Java and Lisp. It's funny that the 'if Java had that syntax' syntax in this article is actually the Groovy syntax.
Gradle requires a Java JDK or JRE to be installed, version 7 or higher (to check, use
java -version).
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.
You can download one of the Gradle distributions from the Gradle web site.
The Gradle distribution comes packaged as a ZIP. The full distribution contains:
The Gradle binaries.
The user guide (HTML and PDF).
The DSL reference guide.
The API documentation (Javadoc).
Extensive samples, including the examples referenced in the user guide, along with some complete and more complex builds you can use as a starting point for your own build.
The binary sources. This is for reference only. If you want to build Gradle you need to download the source distribution or checkout the sources from the source repository. See the Gradle web site for details.
For running Gradle, firstly add the environment variable
GRADLE_HOME. This should point to the unpacked files from the
Gradle website. Next add
to your
PATH environment variable.
Usually, this is sufficient to run Gradle.
You run Gradle via the gradle command. To check if Gradle is properly installed just type gradle -v. The output shows the Gradle version and also the local environment configuration (Groovy, JVM version, OS, etc.). The displayed Gradle version should match the distribution you have downloaded.
JVM options for running Gradle can be set via environment variables. You can use either
JAVA_OPTS, or both.
JAVA_OPTS is by convention an environment
variable shared by many Java applications. A typical use case would be to set the HTTP proxy in
and the memory options in
GRADLE_OPTS. Those variables can also be set at the beginning
of the gradle or gradlew script.
Note that it's not currently possible to set JVM options for Gradle on the command line.
This chapter introduces the basics of the Gradle command-line. You run a build using the gradle command, which you have already seen in action in previous chapters.
You can execute multiple tasks in a single build by listing each of the tasks on the command-line. For example,
the command
gradle compile test will execute the
compile and
test tasks. Gradle will execute the tasks in the order that they are listed on the
command-line, and will also execute the dependencies for each task. Each task is executed once only,
regardless of how it came to be included in the build: whether it was specified on the command-line, or as a
dependency of another task, or both. Let's look at an example.
Below four tasks are defined. Both
dist and
test depend on the
compile task. Running
gradle dist test for this build script
results in the
compile task being executed only once.
Example 4.1. Executing multiple tasks
task compile { doLast { println 'compiling source' } } task compileTest(dependsOn: compile) { doLast { println 'compiling unit tests' } } task test(dependsOn: [compile, compileTest]) { doLast { println 'running unit tests' } } task dist(dependsOn: [compile, test]) { doLast { println 'building the distribution' } }
Output of
gradle dist test
> gradle dist test :compile compiling source :compileTest compiling unit tests :test running unit tests :dist building the distribution BUILD SUCCESSFUL Total time: 1 secs
Each task is executed only once, so
gradle test test is exactly the same
gradle test.
You can exclude a task from being executed using the
-x command-line option and providing
the name of the task to exclude. Let's try this with the sample build file above.
Example 4.2. Excluding tasks
Output of
gradle dist -x test
> gradle dist -x test :compile compiling source :dist building the distribution BUILD SUCCESSFUL Total time: 1 secs
You can see from the output of this example, that the
test task is not executed, even
though it is a dependency of the
dist task. You will also notice that the
test task's dependencies, such as
are not executed either. Those dependencies of
test that are required by another task, such as
compile, are still executed.
By default, Gradle will abort execution and fail the build as soon as any task fails. This allows the build to complete sooner, but hides other failures
that would have occurred. In order to discover as many failures as possible in a single build execution, you can use the
--continue option.
When executed with
--continue, Gradle will execute every task to be executed where all of the dependencies for that task completed without failure,
instead of stopping as soon as the first failure is encountered. Each of the encountered failures will be reported at the end of the build.
If a task fails, any subsequent tasks that were depending on it will not be executed, as it is not safe to do so. For example, tests will not run if there is a compilation failure in the code under test; because the test task will depend on the compilation task (either directly or indirectly).
When you specify tasks on the command-line, you don't have to provide the full name of the task. You only need to provide enough of the
task name to uniquely identify the task. For example, in the sample build above, you can execute task
dist by running
gradle d:
Example 4.3. Abbreviated task name
Output of
gradle di
> gradle di :compile compiling source :compileTest compiling unit tests :test running unit tests :dist building the distribution BUILD SUCCESSFUL Total time: 1 secs
You can also abbreviate each word in a camel case task name. For example, you can execute task
by running
gradle compTest or even
gradle cT
Example 4.4. Abbreviated camel case task name
Output of
gradle cT
> gradle cT :compile compiling source :compileTest compiling unit tests BUILD SUCCESSFUL Total time: 1 secs
You can also use these abbreviations with the
-x command-line option.
When you run the gradle command, it looks for a build file in the current directory.
You can use the
-b option to select another build file.
If you use
-b option then
settings.gradle file is not used. Example:
Example 4.5. Selecting the project using a build file
task hello {
doLast {
println "using build file '$buildFile.name' in '$buildFile.parentFile.name'."
Output of
gradle -q -b subdir/myproject.gradle hello
> gradle -q -b subdir/myproject.gradle hello using build file 'myproject.gradle' in 'subdir'.
Alternatively, you can use the
-p option to specify the project directory to use.
For multi-project builds you should use
-p option instead of
-b option.
Example 4.6. Selecting the project using project directory
Output of
gradle -q -p subdir hello
> gradle -q -p subdir hello using build file 'build.gradle' in 'subdir'.
Many tasks, particularly those provided by Gradle itself, support incremental builds.
Such tasks can determine whether they need to run or not based on whether their inputs or outputs have changed since the last
time they ran. You can easily identify tasks that take part in incremental build when Gradle displays
the text
UP-TO-DATE next to their name during a build run.
You may on occasion want to force Gradle to run all the tasks, ignoring any up-to-date checks. If that's the case, simply use
--rerun-tasks option. Here's the output when running a task both without and with
Example 4.7. Forcing tasks to run
Output of
gradle doIt
> gradle doIt :doIt UP-TO-DATE
Output of
gradle --rerun-tasks doIt
> gradle --rerun-tasks doIt :doIt
Note that this will force all required tasks to execute, not just the ones you specify on the command
line. It's a little like running a
clean, but without the build's generated output being deleted.
Gradle provides several built-in tasks which show particular details of your build. This can be useful for understanding the structure and dependencies of your build, and for debugging problems.
In addition to the built-in tasks shown below, you can also use the project report plugin to add tasks to your project which will generate these reports.
gradle projects gives you a list of the sub-projects of the selected project,
displayed in a hierarchy. Here is an example:
Example 4.8. Obtaining information about projects
Output of
gradle -q projects
> gradle -q projects ------------------------------------------------------------ Root project ------------------------------------------------------------ Root project 'projectReports' +--- Project ':api' - The shared API for the application \--- Project ':webapp' - The Web application implementation To see a list of the tasks of a project, run gradle <project-path>:tasks For example, try running gradle :api:tasks
The report shows the description of each project, if specified. You can provide a description for a project
by setting the
description property:
Example 4.9. Providing a description for a project
description = 'The shared API for the application'
gradle tasks gives you a list of the main tasks of the
selected project. This report shows the default tasks for the project, if any, and a description for
each task. Below is an example of this report:
Example 4.10. Obtaining information about tasks
Output of
gradle -q tasks
> gradle -q tasks ------------------------------------------------------------ All tasks runnable from root project ------------------------------------------------------------ Default tasks: dists Build tasks ----------- clean - Deletes the build directory (build) dists - Builds the distribution libs - Builds the JAR Build Setup tasks ----------------- init - Initializes a new Gradle build. [incubating] wrapper - Generates Gradle wrapper files. [incubating] Help tasks ---------- buildEnvironment - Displays all buildscript dependencies declared in root project 'projectReports'. components - Displays the components produced by root project 'projectReports'. [incubating] dependencies - Displays all dependencies declared in root project 'projectReports'. dependencyInsight - Displays the insight into a specific dependency in root project 'projectReports'. dependentComponents - Displays the dependent components of components in root project 'projectReports'. [incubating] help - Displays a help message. model - Displays the configuration model of root project 'projectReports'. [incubating] projects - Displays the sub-projects of root project 'projectReports'. properties - Displays the properties of root project 'projectReports'. tasks - Displays the tasks runnable from root project 'projectReports' (some of the displayed tasks may belong to subprojects). To see all tasks and more detail, run gradle tasks --all To see more detail about a task, run gradle help --task <task>
By default, this report shows only those tasks which have been assigned to a task group. You can do this
by setting the
group property for the task. You can also set the
property, to provide a description to be included in the report.
Example 4.11. Changing the content of the task report
dists { description = 'Builds the distribution' group = 'build' }
You can obtain more information in the task listing using the
--all option. With
this option, the task report lists all tasks in the project, grouped by main task, and the dependencies
for each task. Here is an example:
Example 4.12. Obtaining more information about tasks
Output of
gradle -q tasks --all
> gradle -q tasks --all ------------------------------------------------------------ All tasks runnable from root project ------------------------------------------------------------ Default tasks: dists Build tasks ----------- clean - Deletes the build directory (build) api:clean - Deletes the build directory (build) webapp:clean - Deletes the build directory (build) dists - Builds the distribution [api:libs, webapp:libs] docs - Builds the documentation api:libs - Builds the JAR api:compile - Compiles the source files webapp:libs - Builds the JAR [api:libs] webapp:compile - Compiles the source files Build Setup tasks ----------------- init - Initializes a new Gradle build. [incubating] wrapper - Generates Gradle wrapper files. [incubating] Help tasks ---------- buildEnvironment - Displays all buildscript dependencies declared in root project 'projectReports'. api:buildEnvironment - Displays all buildscript dependencies declared in project ':api'. webapp:buildEnvironment - Displays all buildscript dependencies declared in project ':webapp'. components - Displays the components produced by root project 'projectReports'. [incubating] api:components - Displays the components produced by project ':api'. [incubating] webapp:components - Displays the components produced by project ':webapp'. [incubating] dependencies - Displays all dependencies declared in root project 'projectReports'. api:dependencies - Displays all dependencies declared in project ':api'. webapp:dependencies - Displays all dependencies declared in project ':webapp'. dependencyInsight - Displays the insight into a specific dependency in root project 'projectReports'. api:dependencyInsight - Displays the insight into a specific dependency in project ':api'. webapp:dependencyInsight - Displays the insight into a specific dependency in project ':webapp'. dependentComponents - Displays the dependent components of components in root project 'projectReports'. [incubating] api:dependentComponents - Displays the dependent components of components in project ':api'. [incubating] webapp:dependentComponents - Displays the dependent components of components in project ':webapp'. [incubating] help - Displays a help message. api:help - Displays a help message. webapp:help - Displays a help message. model - Displays the configuration model of root project 'projectReports'. [incubating] api:model - Displays the configuration model of project ':api'. [incubating] webapp:model - Displays the configuration model of project ':webapp'. [incubating] projects - Displays the sub-projects of root project 'projectReports'. api:projects - Displays the sub-projects of project ':api'. webapp:projects - Displays the sub-projects of project ':webapp'. properties - Displays the properties of root project 'projectReports'. api:properties - Displays the properties of project ':api'. webapp:properties - Displays the properties of project ':webapp'. tasks - Displays the tasks runnable from root project 'projectReports' (some of the displayed tasks may belong to subprojects). api:tasks - Displays the tasks runnable from project ':api'. webapp:tasks - Displays the tasks runnable from project ':webapp'.
gradle help --task someTask gives you detailed information about a specific task or multiple
tasks matching the given task name in your multiproject build.
Below is an example of this detailed information:
Example 4.13. Obtaining detailed help for tasks
Output of
gradle -q help --task libs
> gradle -q help --task libs Detailed task information for libs Paths :api:libs :webapp:libs Type Task (org.gradle.api.Task) Description Builds the JAR Group build
This information includes the full task path, the task type, possible commandline options and the description of the given task.
gradle dependencies
gives you a list of the dependencies of the selected project, broken down by configuration. For each
configuration, the direct and transitive dependencies of that configuration are shown in a tree. Below
is an example of this report:
Example 4.14. Obtaining information about dependencies
Output of
gradle -q dependencies api:dependencies webapp:dependencies
> gradle -q dependencies api:dependencies webapp:dependencies ------------------------------------------------------------ Root project ------------------------------------------------------------ No configurations ------------------------------------------------------------ Project :api - The shared API for the application ------------------------------------------------------------ compile \--- org.codehaus.groovy:groovy-all:2.4.7 testCompile \--- junit:junit:4.12 \--- org.hamcrest:hamcrest-core:1.3 ------------------------------------------------------------ Project :webapp - The Web application implementation ------------------------------------------------------------ compile +--- project :api | \--- org.codehaus.groovy:groovy-all:2.4.7 \--- commons-io:commons-io:1.2 testCompile No dependencies
Since a dependency report can get large, it can be useful to restrict the report to a particular configuration.
This is achieved with the optional
--configuration parameter:
Example 4.15. Filtering dependency report by configuration
Output of
gradle -q api:dependencies --configuration testCompile
> gradle -q api:dependencies --configuration testCompile ------------------------------------------------------------ Project :api - The shared API for the application ------------------------------------------------------------ testCompile \--- junit:junit:4.12 \--- org.hamcrest:hamcrest-core:1.3
gradle buildEnvironment visualises the buildscript dependencies of the selected project,
similarly to how
gradle dependencies visualises the dependencies of the software being built.
gradle dependencyInsight
gives you an insight into a particular dependency (or dependencies) that match specified input.
Below is an example of this report:
Example 4.16. Getting the insight into a particular dependency
Output of
gradle -q webapp:dependencyInsight --dependency groovy --configuration compile
> gradle -q webapp:dependencyInsight --dependency groovy --configuration compile org.codehaus.groovy:groovy-all:2.4.7 \--- project :api \--- compile
This task is extremely useful for investigating the dependency resolution,
finding out where certain dependencies are coming from and why certain versions are selected.
For more information please see the
DependencyInsightReportTask class in the API documentation.
The built-in dependencyInsight task is a part of the 'Help' tasks group.
The task needs to be configured with the dependency and the configuration.
The report looks for the dependencies that match the specified dependency spec in the specified configuration.
If Java related plugins are applied, the dependencyInsight task is pre-configured with the 'compile' configuration because typically it's the compile dependencies we are interested in.
You should specify the dependency you are interested in via the command line '--dependency' option.
If you don't like the defaults you may select the configuration via the '--configuration' option.
For more information see the
DependencyInsightReportTask class in the API documentation.
gradle properties gives you a list of the properties of the selected
project. This is a snippet from the output:
Example 4.17. Information about properties
Output of
gradle -q api:properties
> gradle -q api:properties ------------------------------------------------------------ Project :api - The shared API for the application ------------------------------------------------------------ allprojects: [project ':api'] ant: org.gradle.api.internal.project.DefaultAntBuilder@12345 antBuilderFactory: org.gradle.api.internal.project.DefaultAntBuilderFactory@12345 artifacts: org.gradle.api.internal.artifacts.dsl.DefaultArtifactHandler_Decorated@12345 asDynamicObject: DynamicObject for project ':api' baseClassLoaderScope: org.gradle.api.internal.initialization.DefaultClassLoaderScope@12345 buildDir: /home/user/gradle/samples/userguide/tutorial/projectReports/api/build buildFile: /home/user/gradle/samples/userguide/tutorial/projectReports/api/build.gradle
--profile command line option will record some useful timing information
while your build is running and write a report to the
build/reports/profile directory.
The report will be named using the time when the build was run.
This report lists summary times and details for both the configuration phase and task execution. The times for configuration and task execution are sorted with the most expensive operations first. The task execution results also indicate if any tasks were skipped (and the reason) or if tasks that were not skipped did no work.
Builds which utilize a buildSrc directory will generate a second profile report for buildSrc in the
buildSrc/build directory.
Sometimes you are interested in which tasks are executed in which order for a given set of tasks specified on the
command line, but you don't want the tasks to be executed. You can use the
-m option for this.
For example, if you run "
gradle -m clean compile", you'll see all the tasks that would be
executed as part of the
clean and
compile tasks.
This is complementary to the
tasks task, which shows you the tasks which are available for
In this chapter, you have seen some of the things you can do with Gradle from the command-line. You can find out more about the gradle command in Appendix D, Gradle Command Line.
Most tools require installation on your computer before you can use them. If the installation is easy, you may think that's fine. But it can be an unnecessary burden on the users of the build. Equally importantly, will the user install the right version of the tool for the build? What if they're building an old version of the software?
The Gradle Wrapper (henceforth referred to as the "Wrapper") solves both these problems and is the preferred way of starting a Gradle build.
If a Gradle project has set up the Wrapper (and we recommend all projects do so), you can execute the build using one of the following commands from the root of the project:
./gradlew <task> (on Unix-like platforms such as Linux
and Mac OS X)
gradlew <task> (on Windows using the gradlew.bat
batch file)
Each Wrapper is tied to a specific version of Gradle, so when you first run one of the commands above for a given Gradle version, it will download the corresponding Gradle distribution and use it to execute the build.
When importing a Gradle project via its wrapper, your IDE may ask to use the Gradle 'all' distribution. This is perfectly fine and helps the IDE provide code completion for the build files.
Not only does this mean that you don't have to manually install Gradle yourself, but you are also sure
to use the version of Gradle that the build is designed for. This makes your historical builds more
reliable. Just use the appropriate syntax from above whenever you see a command line starting with
gradle ... in the user guide, on Stack Overflow, in articles or wherever.
For completeness sake, and to ensure you don't delete any important files, here are the files and directories in a Gradle project that make up the Wrapper:
gradlew (Unix Shell script)
gradlew.bat (Windows batch file)
gradle/wrapper/gradle-wrapper.jar (Wrapper JAR)
gradle/wrapper/gradle-wrapper.properties (Wrapper properties)
If you're wondering where the Gradle distributions are stored, you'll find them in your user home
directory under
The Wrapper is something you should check into version control. By distributing the Wrapper with your project, anyone can work with it without needing to install Gradle beforehand. Even better, users of the build are guaranteed to use the version of Gradle that the build was designed to work with. Of course, this is also great for continuous integration servers (i.e. servers that regularly build your project) as it requires no configuration on the server.
You install the Wrapper into your project by running the
wrapper task. (This task is always available, even if
you don't add it to your build).
To specify a Gradle version use
--gradle-version on the command-line. By default, the Wrapper will use a
bin distribution.
This is the smallest Gradle distribution. Some tools, like Android Studio and Intellij IDEA, provide additional context information when used with the
all distribution.
You may select a different Gradle distribution type by using
You can also set the URL to download Gradle from directly via
--gradle-distribution-url. If no version or distribution
URL is specified, the Wrapper will be configured to use the gradle version the
wrapper task is executed with.
So if you run the
wrapper task with Gradle 2.4, then the Wrapper configuration will default to version 2.4.
Example 5.1. Running the Wrapper task
Output of
gradle wrapper --gradle-version 2.0
> gradle wrapper --gradle-version 2.0 :wrapper BUILD SUCCESSFUL Total time: 1 secs
The Wrapper can be further customized by adding and configuring a
task in your build script, and then executing it.
After such an execution you find the following new or updated files in your project directory (in case the default configuration of the Wrapper task is used).
Example 5.3. Wrapper generated files
Build layout
simple/ gradlew gradlew.bat gradle/wrapper/ gradle-wrapper.jar gradle-wrapper.properties
All of these files should be submitted to your version control system. This only needs to be done once. After these files have been added to the project, the project should then be built with the added gradlew command. The gradlew command can be used exactly the same way as the gradle command.
If you want to switch to a new version of Gradle you don't need to rerun the
wrapper task. It is good enough
to change the respective entry in the
gradle-wrapper.properties file, but if you want to take
advantage of new functionality in the Gradle wrapper, then you would need to regenerate the wrapper files.
If you run Gradle with gradlew, the Wrapper checks if a Gradle distribution for the Wrapper is available. If so, it delegates to the gradle command of this distribution with all the arguments passed originally to the gradlew command. If it didn't find a Gradle distribution, it will download it first.
When you configure the
Wrapper task, you can specify the Gradle version you wish to use. The gradlew
command will download the appropriate distribution from the Gradle repository.
Alternatively, you can specify the download URL of the Gradle distribution. The gradlew command will use this URL to download
the distribution.
If you specified neither a Gradle version nor download URL, the gradlew command will download whichever version
of Gradle was used to generate the Wrapper files.
For the details on how to configure the Wrapper, see the
Wrapper class in the API documentation.
If you don't want any download to happen when your project is built via gradlew, simply add the Gradle
distribution zip to your version control at the location specified by your Wrapper configuration.
A relative URL is supported - you can specify a distribution file relative to the location of
gradle-wrapper.properties file.
If you build via the Wrapper, any existing Gradle distribution installed on the machine is ignored.
HTTP Basic Authentication should only be used with
HTTPS URLs and not plain
HTTP ones.
With Basic Authentication, the user credentials are sent in clear text.
The Gradle
Wrapper can download Gradle distributions from servers using HTTP Basic Authentication.
This enables you to host the Gradle distribution on a private protected server.
You can specify a username and password in two different ways depending on your use case: as system properties or directly embedded in the
Credentials in system properties take precedence over the ones embedded in
Using system properties can be done in the
.gradle/gradle.properties file in the user's home directory, or by other means, see Section 12.1, "Configuring the build environment via gradle.properties".
Example 5.4. Specifying the HTTP Basic Authentication credentials using system properties
systemProp.gradle.wrapperUser=username systemProp.gradle.wrapperPassword=password
Embedding credentials in the
distributionUrl in the
gradle/wrapper/gradle-wrapper.properties file also works.
Please note that this file is to be committed into your source control system.
Shared credentials embedded in
distributionUrl should only be used in a controlled environment.
Example 5.5. Specifying the HTTP Basic Authentication credentials in
This can be used in conjunction with a proxy, authenticated or not.
See Section 12.3, "Accessing the web via a proxy" for more information on how to configure the
Wrapper to use a proxy.
The Gradle Wrapper allows for verification of the downloaded Gradle distribution via SHA-256 hash sum comparison. This increases security against targeted attacks by preventing a man-in-the-middle attacker from tampering with the downloaded Gradle distribution.
To enable this feature you'll want to first calculate the SHA-256 hash of a known Gradle distribution. You can generate a SHA-256 hash from Linux and OSX or Windows (via Cygwin) with the shasum command.
Example 5.6. Generating a SHA-256 hash
> shasum -a 256 gradle-2.4-all.zip 371cb9fbebbe9880d147f59bab36d61eee122854ef8c9ee1ecf12b82368bcf10 gradle-2.4-all.zip
Add the returned hash sum to the
gradle-wrapper.properties using the
distributionSha256Sum property.
Example 5.7. Configuring SHA-256 checksum verification
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 that require a non-trivial initialization time. As a result, it can sometimes seem a little slow to start. The solution to this problem is the Gradle Daemon: a long-lived background process that executes your builds much more quickly than would otherwise be the case. We accomplish this by avoiding the expensive bootstrapping process as well as leveraging caching, by keeping data about your project in memory. Running Gradle builds with the Daemon is no different than without. Simply configure whether you want to use it or not - everything else is handled transparently by Gradle.
The Daemon is a long-lived process, so not only are we able to avoid the cost of JVM startup for every build, but we are able to cache information about project structure, files, tasks, and more in memory.
The reasoning is simple: improve build speed by reusing computations from previous builds.
However, the benefits are dramatic: we typically measure build times reduced by 15-75% on
subsequent builds. We recommend profiling your build by using
--profile to get
a sense of how much impact the Gradle Daemon can have for you.
The Gradle Daemon is enabled by default starting with Gradle 3.0, so you don't have to do anything to benefit from it.
To get a list of running Gradle Daemons and their statuses use the --status command.
Sample output:
Currently, a given Gradle version can only connect to daemons of the same version. This means the status output will only show Daemons for the version of Gradle being invoked and not for any other versions. Future versions of Gradle will lift this constraint and will show the running Daemons for all versions of Gradle.
The Gradle Daemon is enabled by default, and we recommend always enabling it for developers' machines. There are several ways to disable the Daemon, but the most common one is to add the line
to the file
«USER_HOME»/.gradle/gradle.properties, where
is your home directory. That's typically one of the following, depending on your platform:
C:\Users\<username> (Windows Vista & 7+)
/Users/<username> (Mac OS X)
/home/<username> (Linux)
If that file doesn't exist, just create it using a text editor. You can find details of other ways to disable (and enable) the Daemon in Section 6.5, "FAQ" further down. That section also contains more detailed information on how the Daemon works.
Once you have globally enabled the Daemon in this way, all your builds will take advantage of the speed boost, regardless of the version of Gradle a particular build uses.
At the moment, we recommend that you disable the Daemon for Continuous Integration servers as correctness is usually a priority over speed in CI environments. Using a fresh runtime for each build is more reliable since the runtime is completely isolated from any previous builds. Additionally, since the Daemon primarily acts to reduce build startup times, this isn't as critical in CI as it is on a developer's machine.
As mentioned, the Daemon is a background process. You needn't worry about a build up of Gradle
processes on your machine, though. Every Daemon monitors its memory usage compared to total system memory
and will stop itself if idle when available system memory is low. If you want to explicitly stop running
Daemon processes for any reason, just use the command
gradle --stop.
This will terminate all Daemon processes that were started with the same version of Gradle used to execute the
command. If you have the Java Development Kit (JDK) installed, you can easily verify that a Daemon has stopped by
running the jps command. You'll see any running Daemons listed with the name
There are two recommended ways to disable the Daemon persistently for an environment:
Via environment variables: add the flag
-Dorg.gradle.daemon=false to the
GRADLE_OPTS environment variable
Via properties file: add
org.gradle.daemon=false to the
«GRADLE_USER_HOME»/gradle.properties file
«GRADLE_USER_HOME» defaults to
«USER_HOME»/.gradle, where
«USER_HOME» is the home directory of the current user.
This location can be configured via the
-g and
--gradle-user-home command line switches,
as well as by the
GRADLE_USER_HOME environment variable and
org.gradle.user.home JVM system property.
Both approaches have the same effect.
Which one to use is up to personal preference.
Most Gradle users choose the second option and add the entry to the user
gradle.properties file.
On Windows, this command will disable 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 will disable the Daemon for the current user:
mkdir -p ~/.gradle && echo "org.gradle.daemon=false" >> ~/.gradle/gradle.properties
Once the Daemon is disabled for a build environment in this way, a Gradle Daemon will not be started unless explicitly requested using the
--daemon option.
--daemon and
--no-daemon command line options enable and disable usage of the Daemon for individual build invocations when using the Gradle command line interface.
These command line options have the highest precedence when considering the build environment.
Typically, it is more convenient to enable the Daemon for an environment (e.g. a user account) so that all builds use the Daemon without requiring to remember to supply the
--daemon option.
There are several reasons why Gradle will create a new Daemon, instead of using one that is already running. The basic rule is that Gradle will start a new Daemon if there are no existing idle or compatible Daemons available. Gradle will kill any Daemon that has been idle for 3 hours or more, so you don't have to worry about cleaning them up manually.
An idle Daemon is one that is not currently executing a build or doing other useful work.
A compatible Daemon is one that can (or can be made to) meet the requirements of the requested build environment. The Java runtime used to execute the build is an example aspect of the build environment. Another example is the set of JVM system properties required by the build runtime.
Some aspects of the requested build environment may not be met by an Daemon.
If the Daemon is running with a Java 7 runtime, but the requested environment calls for Java 8, then the Daemon is not compatible and another must be started.
Moreover, certain properties of a Java runtime cannot be changed once the JVM has started.
For example, it is not possible to change the memory allocation (e.g.
-Xmx1024m), default text encoding, default locale, etc of a running JVM.
The "requested build environment" is typically constructed implicitly from aspects of the build client's (e.g. Gradle command line client, IDE etc.) environment and explicitly via command line switches and settings. See Chapter 12, The Build Environment for details on how to specify and control the build environment.
The following JVM system properties are effectively immutable. If the requested build environment requires any of these properties, with a different value than a Daemon's JVM has for this property, the Daemon is not compatible.
The following JVM attributes, controlled by startup arguments, are also effectively immutable. The corresponding attributes of the requested build environment and the Daemon's environment must match exactly in order for a Daemon to be compatible.
The required Gradle version is another aspect of the requested build environment. Daemon processes are coupled to a specific Gradle runtime. Working on multiple Gradle projects during a session that use different Gradle versions is a common reason for having more than one running Daemon process.
If the requested build environment does not specify a maximum heap size, the Daemon will use up to 1GB of heap. It will use your the JVM's default minimum heap size. 1GB is more than enough for most builds. Larger builds with hundreds of subprojects, lots of configuration, and source code may require, or perform better, with more memory.
To increase the amount of memory the Daemon can use, specify the appropriate flags as part of the requested build environment. Please see Chapter 12, The Build Environment for details.
Daemon processes will automatically terminate themselves after 3 hours of inactivity or less.
If you wish to stop a Daemon process before this, you can either kill the process via your operating system or run the
gradle --stop command.
--stop switch causes Gradle to request that all running Daemon processes, of the same Gradle version used to run the command, terminate themselves.
Considerable engineering effort has gone into making the Daemon robust, transparent and unobtrusive during day to day development. However, Daemon processes can occasionally be corrupted or exhausted. A Gradle build executes arbitrary code from multiple sources. While Gradle itself is designed for and heavily tested with the Daemon, user build scripts and third party plugins can destabilize the Daemon process through defects such as memory leaks or global state corruption.
It is also possible to destabilize the Daemon (and build environment in general) by running builds that do not release resources correctly. This is a particularly poignant problem when using Microsoft Windows as it is less forgiving of programs that fail to close files after reading or writing.
Gradle actively monitors heap usage and attempts to detect when a leak is starting to exhaust the available heap space in the daemon. When it detects a
problem, the Gradle daemon will finish the currently running build and proactively restart the daemon on the next build. This monitoring
is enabled by default, but can be disabled by setting the
org.gradle.daemon.performance.enable-monitoring system property to false.
If it is suspected that the Daemon process has become unstable, it can simply be killed.
Recall that the
--no-daemon switch can be specified for a build to prevent use of the Daemon.
This can be useful to diagnose whether or not the Daemon is actually the culprit of a problem.
It is recommended that the Daemon is used in all developer environments. It is recommend to disable the Daemon for Continuous Integration and build server environments.
The Daemon enables faster builds, which is particularly important when a human is sitting in front of the build. For CI builds, stability and predictability is of utmost importance. Using a fresh runtime (i.e. process) for each build is more reliable as the runtime is completely isolated from previous builds.
The Gradle Tooling API (see Chapter 14, Embedding Gradle using the Tooling API), that is used by IDEs and other tools to integrate with Gradle, always use the Gradle Daemon to execute builds. If you are executing Gradle builds from within you're IDE you are using the Gradle Daemon and do not need to enable it for your environment.
However, unless you have explicitly enabled the Gradle Daemon for you environment your builds from the command line will not use the Gradle Daemon.
The Gradle Daemon is a long lived build process. In between builds it waits idly for the next build. This has the obvious benefit of only requiring Gradle to be loaded into memory once for multiple builds, as opposed to once for each build. This in itself is a significant performance optimization, but that's not where it stops.
A significant part of the story for modern JVM performance is runtime code optimization. For example, HotSpot (the JVM implementation provided by Oracle and used as the basis of OpenJDK) applies optimization to code while it is running. The optimization is progressive and not instantaneous. That is, the code is progressively optimized during execution which means that subsequent builds can be faster purely due to this optimization process. Experiments with HotSpot have shown that it takes somewhere between 5 and 10 builds for optimization to stabilize. The difference in perceived build time between the first build and the 10th for a Daemon can be quite dramatic.
The Daemon also allows more effective in memory caching across builds. For example, the classes needed by the build (e.g. plugins, build scripts) can be held in memory between builds. Similarly, Gradle can maintain in-memory caches of build data such as the hashes of task inputs and outputs, used for incremental building.
Currently, the Daemon makes builds faster by effectively supporting in memory caching and by the JVM optimizer making the code faster. In future Gradle versions, the Daemon will become even smarter and perform work preemptively. It could, for example, start downloading dependencies immediately after the build script has been edited under the assumption that the build is about to be run and the newly changed or added dependencies will be required.
There are many other ways in that the Gradle Daemon will enable even faster builds in future Gradle versions.
This chapter introduces some of the basics of dependency management in Gradle.
Very roughly, dependency management is made up of two pieces. Firstly, Gradle needs to know about the things that your project needs to build or run, in order to find them. We call these incoming files the dependencies of the project. Secondly, Gradle needs to build and upload the things that your project produces. We call these outgoing files the publications of the project. Let's look at these two pieces in more detail:
Most projects are not completely self-contained. They need files built by other projects in order to be compiled or tested and so on. For example, in order to use Hibernate in my project, I need to include some Hibernate jars in the classpath when I compile my source. To run my tests, I might also need to include some additional jars in the test classpath, such as a particular JDBC driver or the Ehcache jars.
These incoming files form the dependencies of the project. Gradle allows you to tell it what the dependencies of your project are, so that it can take care of finding these dependencies, and making them available in your build. The dependencies might need to be downloaded from a remote Maven or Ivy repository, or located in a local directory, or may need to be built by another project in the same multi-project build. We call this process dependency resolution.
Note that this feature provides a major advantage over Ant. With Ant, you only have the ability to specify absolute or relative paths to specific jars to load. With Gradle, you simply declare the "names" of your dependencies, and other layers determine where to get those dependencies from. You can get similar behavior from Ant by adding Apache Ivy, but Gradle does it better.
Often, the dependencies of a project will themselves have dependencies. For example, Hibernate core requires several other libraries to be present on the classpath with it runs. So, when Gradle runs the tests for your project, it also needs to find these dependencies and make them available. We call these transitive dependencies.
The main purpose of most projects is to build some files that are to be used outside the project. For example, if your project produces a Java library, you need to build a jar, and maybe a source jar and some documentation, and publish them somewhere.
These outgoing files form the publications of the project. Gradle also takes care of this important work for you. You declare the publications of your project, and Gradle take care of building them and publishing them somewhere. Exactly what "publishing" means depends on what you want to do. You might want to copy the files to a local directory, or upload them to a remote Maven or Ivy repository. Or you might use the files in another project in the same multi-project build. We call this process publication.
Let's look at some dependency declarations. Here's a basic build script:
Example 7.1. Declaring dependencies
apply plugin: 'java' repositories { mavenCentral() } dependencies { compile group: 'org.hibernate', name: 'hibernate-core', version: '3.6.7.Final' testCompile group: 'junit', name: 'junit', version: '4.+' }
What's going on here? This build script says a few things about the project. Firstly, it states that Hibernate core 3.6.7.Final is required to compile the project's production source. By implication, Hibernate core and its dependencies are also required at runtime. The build script also states that any junit >= 4.0 is required to compile the project's tests. It also tells Gradle to look in the Maven central repository for any dependencies that are required. The following sections go into the details.
In Gradle dependencies are grouped into configurations. A configuration is simply a named set of dependencies. We will refer to them as dependency configurations. You can use them to declare the external dependencies of your project. As we will see later, they are also used to declare the publications of your project.
The Java plugin defines a number of standard configurations. These configurations represent the classpaths that the Java plugin uses. Some are listed below, and you can find more details in Table 46.5, "Java plugin - dependency configurations".
The dependencies required to compile the production source of the project.
The dependencies required by the production classes at runtime. By default, also includes the compile time dependencies.
The dependencies required to compile the test source of the project. By default, also includes the compiled production classes and the compile time dependencies.
The dependencies required to run the tests. By default, also includes the compile, runtime and test compile dependencies.
Various plugins add further standard configurations. You can also define your own custom configurations to use in your build. Please see Section 24.3, "Dependency configurations" for the details of defining and customizing dependency configurations.
There are various types of dependencies that you can declare. One such type is an external dependency. This is a dependency on some files built outside the current build, and stored in a repository of some kind, such as Maven central, or a corporate Maven or Ivy repository, or a directory in the local file system.
To define an external dependency, you add it to a dependency configuration:
Example 7.2. Definition of an external dependency
dependencies { compile group: 'org.hibernate', name: 'hibernate-core', version: '3.6.7.Final' }
An external dependency is identified using
name and
version attributes.
Depending on which kind of repository you are using,
group and
version may be optional.
The shortcut form for declaring external dependencies looks like "
Example 7.3. Shortcut definition of an external dependency
dependencies {
compile 'org.hibernate:hibernate-core:3.6.7.Final'
To find out more about defining and working with dependencies, have a look at Section 24.4, "How to declare your dependencies".
How does Gradle find the files for external dependencies? Gradle looks for them in a repository.
A repository is really just a collection of files, organized by
name and
version. Gradle understands several different repository formats, such as Maven and Ivy, and several
different ways of accessing the repository, such as using the local file system or HTTP.
By default, Gradle does not define any repositories. You need to define at least one before you can use external dependencies. One option is use the Maven central repository:
Or Bintray's JCenter:
Or a any other remote Maven repository:
Example 7.6. Usage of a remote Maven repository
repositories {
maven {
url "http://repo.mycompany.com/maven2"
Or a remote Ivy repository:
Example 7.7. Usage of a remote Ivy directory
repositories {
ivy {
url "http://repo.mycompany.com/repo"
You can also have repositories on the local file system. This works for both Maven and Ivy repositories.
Example 7.8. Usage of a local Ivy directory
repositories { ivy { // URL can refer to a local directory url "../local-repo" } }
A project can have multiple repositories. Gradle will look for a dependency in each repository in the order they are specified, stopping at the first repository that contains the requested module.
To find out more about defining and working with repositories, have a look at Section 24.6, "Repositories".
Dependency configurations are also used to publish files.[2] We call these files publication artifacts, or usually just artifacts.
The plugins do a pretty good job of defining the artifacts of a project, so you usually don't need to do anything special to tell Gradle
what needs to be published. However, you do need to tell Gradle where to publish the artifacts. You do this by attaching repositories
to the
uploadArchives task. Here's an example of publishing to a remote Ivy repository:
Example 7.9. Publishing to an Ivy repository
uploadArchives { repositories { ivy { credentials { username "username" password "pw" } url "http://repo.mycompany.com" } } }
Now, when you run
gradle uploadArchives, Gradle will build and upload your Jar.
Gradle will also generate and upload an
ivy.xml as well.
You can also publish to Maven repositories. The syntax is slightly different.[3]
Note that you also need to apply the Maven plugin in order to publish to a Maven repository. when this is in place, Gradle
will generate and upload a
Example 7.10. Publishing to a Maven repository
apply plugin: 'maven' uploadArchives { repositories { mavenDeployer { repository(url: "file://localhost/tmp/myRepo/") } } }
To find out more about publication, have a look at Chapter 31, Publishing artifacts.
For all the details of dependency resolution, see Chapter 24, Dependency Management, and for artifact publication see Chapter 31, Publishing artifacts.
If you are interested in the DSL elements mentioned here, have a look at
Project.repositories{} and
Otherwise, continue on to some of the other tutorials.
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.
Such builds come in all shapes and sizes, but they do have some common characteristics:
settings.gradle file in the root or
directory of the project
build.gradle file in the root or
Child directories that have their own
*.gradle build files
(some multi-project builds may omit child project build scripts)
settings.gradle file tells Gradle how the project and subprojects are
structured. Fortunately, you don't have to read this file simply to learn what the project structure
is as you can run the command
gradle projects. Here's the output from using
that command on the Java multiproject build in the Gradle samples:
Example 8.1. Listing the projects in a build
Output of
gradle -q projects
> gradle -q projects ------------------------------------------------------------ Root project ------------------------------------------------------------ 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
This tells you that multiproject has three immediate child projects:
api, services and shared. The
services project then has its own children, shared and
webservice. These map to the directory structure, so it's easy to find them.
For example, you can find webservice in
By default, Gradle uses the name of the directory it finds the
settings.gradle as the name of the root project.
This usually doesn't cause problems since all developers check out the same directory name when working on a project.
On Continuous Integration servers, like Jenkins, the directory name may be auto-generated and not match the name in your VCS.
For that reason, it's recommended that you always set the root project name to something predictable, even in single project builds.
You can configure the root project name by setting
Each project will usually have its own build file, but that's not necessarily the case. In the above example, the services project is just a container or grouping of other subprojects. There is no build file in the corresponding directory. However, multiproject does have one for the root project.
The root
build.gradle is often used to share common configuration between the
child projects, for example by applying the same sets of plugins and dependencies to all the child
projects. It can also be used to configure individual subprojects when it is preferable to have
all the configuration in one place. This means you should always check the root build file when
discovering how a particular subproject is being configured.
Another thing to bear in mind is that the build files might not be called
build.gradle. Many projects will name the build files after the subproject names,
such as
api.gradle and
services.gradle from the previous
example. Such an approach helps a lot in IDEs because it's tough to work out which
build.gradle file out of twenty possibilities is the one you want to open. This
little piece of magic is handled by the
settings.gradle file, but as a build user
you don't need to know the details of how it's done. Just have a look through the child project
directories to find the files with the
.gradle suffix.
Once you know what subprojects are available, the key question for a build user is how to execute the tasks within the project.
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. You have two options here:
Change to the directory corresponding to the subproject you're interested in and
just execute
gradle <task> as normal.
Use a qualified task name from any directory, although this is usually done from
the root. For example:
gradle :services:webservice:build will build the
webservice subproject and any subprojects it depends on.
The first approach is similar to the single-project use case, but Gradle works slightly differently
in the case of a multi-project build. The command
gradle test will execute the
test task in any subprojects, relative to the current working directory, that have
that task. So if you run the command from the root project directory, you'll run
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
For more control over what gets executed, use qualified names (the second approach mentioned). These are paths just like directory paths, but use ':' instead of '/' or '\'. If the path begins with a ':', then the path is resolved relative to the root project. In other words, the leading ':' represents the root project itself. All other colons are path separators.
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 guide.
There's one last thing to note. When you're using the Gradle wrapper, the first approach 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 you're in the webservice subproject directory, you would have to run
../../gradlew build.
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.
Continuous build is an incubating feature. This means that it is incomplete and not yet at regular Gradle production quality. This also means that this Gradle User Guide chapter is a work in progress.
Typically, you ask Gradle to perform a single build by way of specifying tasks that Gradle should execute. Gradle will determine the the actual set of tasks that need to be executed to satisfy the request, execute them all, and then stop doing work until the next request. A continuous build differs in that Gradle will keep satisfying the initial build request (until instructed to stop) by executing the build when it is detected that the result of the previous build is now out of date. For example, if your build compiles Java source files to Java class files, a continuous build would automatically initiate a compile when the source files change. Continuous build is useful for many scenarios.
A continuous build can be started by supplying either the
--continuous or
-t switches to Gradle,
along with the list of tasks, switches and arguments that define the work to do.
For example,
gradle build --continuous.
This will have the same effect as running
gradle build, but instead of Gradle exiting when done, it will wait for changes to the build inputs.
When a change occurs,
gradle build will be automatically executed again and the process repeats.
If Gradle is attached to an interactive input source, such as a terminal, the continuous build can be exited by pressing
CTRL-D (On Microsoft Windows, it is required to also press
ENTER or
RETURN after
If Gradle is not attached to an interactive input source (e.g. is running as part of a script), the build process must be terminated (e.g. using the
kill command or similar).
If the build is being executed via the Tooling API, the build can be cancelled using the Tooling API's cancellation mechanism.
Task implementations declare their file system inputs by annotating their properties with
InputFiles and other similar annotations.
Please see Section 18.9, "Up-to-date checks (AKA Incremental Build)" for more information.
At this time, only changes to task inputs are noticed. Gradle will start watching for changes just before the task starts to execute. No other changes will initiate a build. For example, changes to build scripts and build logic will not initiate build. Likewise, changes to files that are read during the configuration of the build, not the execution, will not initiate a build. In order to incorporate such changes, the continuous build must be restarted manually.
Consider a typical build using the Java plugin, using the conventional filesystem layout.
The following diagram visualizes the task graph for
gradle build:
The following key tasks of the graph use files in the corresponding directories as inputs:
Assuming that the initial build is successful (i.e. the
build task and its dependencies complete without error),
changes to files in, or the addition/remove of files from, the locations listed above will initiate a new build.
If a change is made to a Java source file in
src/main/java, the build will fire and all tasks will be scheduled.
Gradle's incremental build support ensures that only the tasks that are actually affected by the change are executed.
If the change to the main Java source causes compilation to fail, subsequent changes to the test source in
src/test/java will not initiate a new build.
As the test source depends on the main source, there is no point building until the main source has changed, potentially fixing the compilation error.
After each build, only the inputs of the tasks that actually executed will be monitored for changes.
Continuous build is in no way coupled to compilation.
It works for all types of tasks.
For example, the
processResources task copies and processes the files from
src/main/resources for inclusion in the built JAR.
As such, a change to any file in this directory will also initiate a build.
There are several issues to be aware with the current implementation of continuous build. These are likely to be addressed in future Gradle releases.
Gradle starts watching for changes just before a task executes. If a task modifies its own inputs while executing, Gradle will detect the change and trigger a new build. If every time the task executes, the inputs are modified again, the build will be triggered again. This isn't unique to continuous build. A task that modifies its own inputs will never be considered up-to-date when run "normally" without continuous build.
If your build enters a build cycle like this, you can track down the task by looking at the list of files reported changed by Gradle.
After identifying the file(s) that are changed during each build, you should look for a task that has that file as an input.
In some cases, it may be obvious (e.g., a Java file is compiled with
In other cases, you can use
--info logging to find the task that is out-of-date due to the identified files.
Due to class access restrictions related to Java 9, Gradle cannot set some operating system specific options, which means that:
On Mac OS X, Gradle will poll for file changes every 10 seconds instead of every 2 seconds.
On Windows, Gradle must use individual file watches (like on Linux/Mac OS), which may cause continuous build to no longer work on very large projects.
The JDK file watching facility relies on inefficient file system polling on Mac OS X (see: JDK-7133447). This can significantly delay notification of changes on large projects with many source files.
Additionally, the watching mechanism may deadlock under heavy load on Mac OS X (see: JDK-8079620). This will manifest as Gradle appearing not to notice file changes. If you suspect this is occurring, exit continuous build and start again.
On Linux, OpenJDK's implementation of the file watch service can sometimes miss file system events (see: JDK-8145981).
Creating or removing symbolic link to files will initiate a build.
Modifying the target of a symbolic link will not cause a rebuild.
Creating or removing symbolic links to directories will not cause rebuilds.
Creating new files in the target directory of a symbolic link will not cause a rebuild.
Deleting the target directory will not cause a rebuild.
Composite build is an incubating feature. While useful for many use cases, there are bugs to be discovered, rough edges to smooth, and enhancements we plan to make. Thanks for trying it out!
A composite build is simply a build that includes other builds. In many ways a composite build is similar to a Gradle multi-project build, except that instead of including single
projects, complete
builds are included.
combine builds that are usually developed independently, for instance when trying out a bug fix in a library that your application uses
decompose a large multi-project build into smaller, more isolated chunks that can be worked in independently or together as needed
A build that is included in a composite build is referred to, naturally enough, as an "included build". Included builds do not share any configuration with the composite build, or the other included builds. Each included build is configured and executed in isolation.
Included builds interact with other builds via
dependency substitution. If any build in the composite has a dependency that can be satisfied by the included build, then that dependency will be replaced by a project dependency on the included build.
By default, Gradle will attempt to determine the dependencies that can be substituted by an included build. However for more flexibility, it is possible to explicitly declare these substitutions if the default ones determined by Gradle are not correct for the composite. See Section 10.3, "Declaring the dependencies substituted by an included build".
As well as consuming outputs via project dependencies, a composite build can directly declare task dependencies on included builds. Included builds are isolated, and are not able to declare task dependencies on the composite build or on other included builds. See Section 10.4, "Depending on tasks in an included build".
The following examples demonstrate the various ways that 2 Gradle builds that are normally developed separately can be combined into a composite build. For these examples, the
my-utils multi-project build produces 2 different java libraries (
number-utils and
string-utils), and the
my-app build produces an executable using functions from those libraries.
my-app build does not have direct dependencies on
my-utils. Instead, it declares binary dependencies on the libraries produced by
Example 10.1. Dependencies of my-app
apply plugin: 'java' apply plugin: 'application' apply plugin: 'idea' group "org.sample" version "1.0" mainClassName = "org.sample.myapp.Main" dependencies { compile "org.sample:number-utils:1.0" compile "org.sample:string-utils:1.0" } repositories { jcenter() }
Note: The code for this example can be found at
samples/compositeBuilds/basic in the '-all' distribution of Gradle.
--include-build command-line argument turns the executed build into a composite, substituting dependencies from the included build into the executed build.
Example 10.2. Declaring a command-line composite
Output of
gradle --include-build ../my-utils run
> gradle --include-build ../my-utils run [composite-build] Configuring build: /home/user/gradle/samples/compositeBuilds/basic/my-utils :compileJava :my-utils:number-utils:compileJava :my-utils:number-utils:processResources UP-TO-DATE :my-utils:number-utils:classes :my-utils:number-utils:jar :my-utils:string-utils:compileJava :my-utils:string-utils:processResources UP-TO-DATE :my-utils:string-utils:classes :my-utils:string-utils:jar :processResources UP-TO-DATE :classes :run The answer is 42 BUILD SUCCESSFUL
It's possible to make the above arrangement persistent, by using
Settings.includeBuild(java.lang.Object) to declare the included build in the
settings.gradle file.
settings.gradle file can be used to add subprojects and included builds at the same time. Included builds are added by location. See the examples below for more details.
One downside of the above approach is that it requires you to modify an existing build, rendering it less useful as a standalone build. One way to avoid this is to define a separate composite build, whose only purpose is to combine otherwise separate builds.
Example 10.3. Declaring a separate composite
rootProject.name='adhoc' includeBuild '../my-app' includeBuild '../my-utils'
In this scenario, the 'main' build that is executed is the composite, and it doesn't define any useful tasks to execute itself. In order to execute the 'run' task in the 'my-app' build, the composite build must define a delegating task.
Example 10.4. Depending on task from included build
task run { dependsOn gradle.includedBuild('my-app').task(':run') }
More details tasks that depend on included build tasks below.
Most builds can be included into a composite, however there are some limitations.
Every included build:
must have a
settings.gradle file.
must not itself be a composite build.
must not have a
rootProject.name the same as another included build.
must not have a
rootProject.name the same as a top-level project of the composite build.
must not have a
rootProject.name the same as the composite build
By default, Gradle will configure each included build in order to determine the dependencies it can provide. The algorithm for doing this is very simple: Gradle will inspect the group and name for the projects in the included build, and substitute project dependencies for any external dependency matching
There are cases when the default substitutions determined by Gradle are not sufficient, or they are not correct for a particular composite. For these cases it is possible to explicitly declare the substitutions for an included build. Take for example a single-project build 'unpublished', that produces a java utility library but does not declare a value for the group attribute:
When this build is included in a composite, it will attempt to substitute for the dependency module "undefined:unpublished" ("undefined" being the default value for
project.group, and 'unpublished' being the root project name). Clearly this isn't going to be very useful in a composite build.
To use the unpublished library unmodified in a composite build, the composing build can explicitly declare the substitutions that it provides:
Example 10.6. Declaring the substitutions for an included build
rootProject.name = 'app' includeBuild('../anonymous-library') { dependencySubstitution { substitute module('org.sample:number-utils') with project(':') } }
With this configuration, the "my-app" composite build will substitute any dependency on
org.sample:number-utils with a dependency on the root project of "unpublished".
Many builds that use the
uploadArchives task to publish artifacts will function automatically as an included build, without declared substitutions. Here are some common cases where declared substitutions are required:
When the
archivesBaseName property is used to set the name of the published artifact.
When a configuration other than
default is published: this usually means a task other than
uploadArchives is used.
When the
MavenPom.addFilter() is used to publish artifacts that don't match the project name.
When the
maven-publish or
ivy-publish plugins are used for publishing, and the publication coordinates don't match
Some builds won't function correctly when included in a composite, even when dependency substitutions are explicitly declared. This limitation is due to the fact that a project dependency that is substituted will always point to the
default configuration of the target project. Any time that the artifacts and dependencies specified for the default configuration of a project don't match what is actually published to a repository, then the composite build may exhibit different behaviour.
Here are some cases where the publish module metadata may be different from the project default configuration:
When a configuration other than
default is published.
When the
maven-publish or
ivy-publish plugins are used.
When the
POM or
ivy.xml file is tweaked as part of publication.
Builds using these features function incorrectly when included in a composite build. We plan to improve this in the future.
While included builds are isolated from one another and cannot declare direct dependencies, a composite build is able to declare task dependencies on it's included builds. The included builds are accessed using
Gradle.getIncludedBuilds() or
Gradle.includedBuild(java.lang.String), and a task reference is obtained via the
IncludedBuild.task(java.lang.String) method.
Using these APIs, it is possible to declare a dependency on a task in a particular included build, or tasks with a certain path in all or some of the included builds.
Example 10.7. Depending on a single task from an included build
task run { dependsOn gradle.includedBuild('my-app').task(':run') }
Example 10.8. Depending on a tasks with path in all included builds
task publishDeps {
dependsOn gradle.includedBuilds*.task(':uploadArchives')
We think composite builds are pretty useful already. However, there are some things that don't yet work the way we'd like, and other improvements that we think will make things work even better.
Limitations of the current implementation include:
No support for included builds that have publications that don't mirror the project default configuration. See Section 10.3.2, "Cases where composite build substitutions won't work".
No native support for composite builds in IntelliJ IDEA or Eclipse Buildship. Generating IDEA metadata with
gradle idea is supported.
Native builds are not supported. (Binary dependencies are not yet supported for native builds).
Improvements we have planned for upcoming releases include:
Better detection of dependency substitution, for build that publish with custom coordinates, builds that produce multiple components, etc. This will reduce the cases where dependency substitution needs to be explicitly declared for an included build.
The ability to target a task or tasks in an included build directly from the command line. We are currently exploring syntax options for allowing this functionality, which will remove many cases where a delegating task is required in the composite.
Execution of included builds in parallel.
Detection of changes to included builds when running with continuous build (
Making the implicit
buildSrc project an included build.
Supporting composite-of-composite builds.
In addition to supporting a traditional command line interface, Gradle offers a graphical user interface. This is a stand alone user interface that can be launched with the --gui option.
Note that this command blocks until the Gradle GUI is closed. Under *nix it is probably preferable to run this as a background task (gradle --gui&)
If you run this from your Gradle project working directory, you should see a tree of tasks.
It is preferable to run this command from your Gradle project directory so that the settings of the UI will be stored in your project directory. However, you can run it then change the working directory via the Setup tab in the UI.
The UI displays 4 tabs along the top and an output window along the bottom.
The Task Tree shows a hierarchical display of all projects and their tasks. Double clicking a task executes it.
There is also a filter so that uncommon tasks can be hidden. You can toggle the filter via the Filter button. Editing the filter allows you to configure which tasks and projects are shown. Hidden tasks show up in red. Note: newly created tasks will show up by default (versus being hidden by default).
The Task Tree context menu provides the following options:
Execute ignoring dependencies. This does not require dependent projects to be rebuilt (same as the -a option).
Add tasks to the favorites (see Favorites tab)
Hide the selected tasks. This adds them to the filter.
Edit the build.gradle file. Note: this requires Java 1.6 or higher and requires that you have .gradle files associated in your OS.
The Favorites tab is a good place to store commonly-executed commands. These can be complex commands (anything that's legal to Gradle) and you can provide them with a display name. This is useful for creating, say, a custom build command that explicitly skips tests, documentation, and samples that you could call "fast build".
You can reorder favorites to your liking and even export them to disk so they can imported by others. If you edit them, you are given options to "Always Show Live Output". This only applies if you have "Only Show Output When Errors Occur". This override always forces the output to be shown.
The Command Line tab is where you can execute a single Gradle command directly. Just enter whatever you would normally enter after 'gradle' on the command line. This also provides a place to try out commands before adding them to favorites.
The Setup tab allows configuration of some general settings.
Current Directory
Defines the root directory of your Gradle project (typically where build.gradle is located).
Stack Trace Output
This determines how much information to write out in stack traces when errors occur. Note: if you specify a stack trace level on either the Command Line or Favorites tab, it will override this stack trace level.
Only Show Output When Errors Occur
Enabling this option hides any output when a task is executed unless the build fails.
Use Custom Gradle Executor - Advanced feature
This provides you with an alternate way to launch Gradle commands. This is useful if your project requires some extra setup that is done inside another batch file or shell script (such as specifying an init script).
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,
certain settings like JVM memory settings, Java home, daemon on/off
can be more useful if they can be versioned with the project in your VCS so that
the entire team can work with a consistent environment.
Setting up a consistent environment for your build is as simple as placing these settings into a
gradle.properties file.
The configuration is applied in following order
(if an option is configured in multiple locations the last one wins):
gradle.properties in project build dir.
gradle.properties in
gradle user home.
-Dsome.property is set on the command line.
When setting these properties you should keep in mind that Gradle requires a Java JDK or JRE of version 7 or higher to run.
The following properties can be used to configure the Gradle build environment:
When set to
true the Gradle daemon is used to run the build.
For local developer builds this is our favorite property. The developer environment is optimized for speed and feedback
so we nearly always run Gradle jobs with the daemon.
We don't run CI builds with the daemon (i.e. a long running process)
as the CI environment is optimized for consistency and reliability.
Specifies the Java home for the Gradle build process.
The value can be set to either a
jdk or
jre location,
however, depending on what your build does,
jdk is safer.
A reasonable default is used if the setting is unspecified.
Specifies the jvmargs used for the daemon process. The setting is particularly useful for tweaking memory settings. At the moment the default settings are pretty generous with regards to memory.
Enables new incubating mode that makes Gradle selective when configuring projects. Only relevant projects are configured which results in faster builds for large multi-projects. See the section called "Configuration on demand".
When configured, Gradle will run in incubating parallel mode.
When configured, Gradle will use a maximum of the given number of workers. See
--max-workers for details.
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.
When set to false, Gradle will not monitor the memory usage of running daemons. See Section 6.5.5, "What can go wrong with Daemon?".
Many settings (like the Java version and maximum heap size) can only be specified when launching a new JVM for the build process. This means that Gradle
must launch a separate JVM process to execute the build after parsing the various
gradle.properties files.
When running with the daemon, a JVM with the correct parameters is started once and reused for each daemon build execution.
When Gradle is executed without the daemon, then a new JVM must be launched for every build execution,
unless the JVM launched by the Gradle start script happens to have the same parameters.
This launching of an extra JVM on every build execution is quite expensive, which is why if you are
setting either
org.gradle.java.home or
we highly recommend that you use the Gradle Daemon. See Chapter 6, The Gradle Daemon for more details.
Gradle offers a variety of ways to add properties to your build. With 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 add properties to your project objects using properties files. You can place a
gradle.properties file in the Gradle user home directory (defined by the
GRADLE_USER_HOME" environment variable, which if not set defaults to
) or in your project directory. For
multi-project builds you can place
gradle.properties files in any subproject directory.
The properties set in a
gradle.properties file can be accessed via the project object. The
properties file in the user's home directory has precedence over property files in the project directories.
You can also add properties directly to your project object via the
-P command line option.
Gradle can also set project properties when it sees specially-named system properties or
environment variables. This feature is very 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, typically for security reasons.
In that situation, you can't use the
-P option, and you can't 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.
If the environment variable name looks like
then Gradle will set a
prop property on your project object, with the value
somevalue. Gradle also supports this for
system properties, but with a different naming pattern, which looks like
You can also set system properties in the
gradle.properties file. If a property
name in such a file has the prefix "
systemProp.", like "
then the property and its value will be set as a system property, without the prefix. 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.
Example 12.1. Setting properties with a gradle.properties file
gradlePropertiesProp=gradlePropertiesValue sysProp=shouldBeOverWrittenBySysProp envProjectProp=shouldBeOverWrittenByEnvProp systemProp.system=systemValue
task printProps {
doLast {
println commandLineProjectProp
println gradlePropertiesProp
println systemProjectProp
println envProjectProp
println System.properties['system']
Output of
gradle -q -PcommandLineProjectProp=commandLineProjectPropValue -Dorg.gradle.project.systemProjectProp=systemPropertyValue printProps
> gradle -q -PcommandLineProjectProp=commandLineProjectPropValue -Dorg.gradle.project.systemProjectProp=systemPropertyValue printProps commandLineProjectPropValue gradlePropertiesValue systemPropertyValue envPropertyValue systemValue
You can access a project property in your build script simply by using its name as you would use a
variable. If this property does not exist, an exception will be thrown and the build will fail. If your
build script relies on optional properties the user might set, perhaps in a
gradle.properties file,
you need to check for existence before you access them. You can do this by using the method
which returns
Configuring an HTTP or HTTPS 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 a gradle.properties file, either in the build's root
directory or in the Gradle home directory.
Example 12.2. Configuring an HTTP proxy
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.
Example 12.3. Configuring an HTTPS proxy
systemProp.https.proxyHost=www.somehost.org systemProp.https.proxyPort=8080 systemProp.https.proxyUser=userid systemProp.https.proxyPassword=password systemProp.http.nonProxyHosts=*.nonproxyrepos.com|localhost
We could not find a good overview for all possible proxy settings. One place to look are the constants in a file from the Ant project. Here's a link to the Subversion view. The other is a Networking Properties page from the JDK docs. If anyone knows of a better overview, please let us know via the mailing list.
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:
http.proxyUser system property to a value like
http.auth.ntlm.domain system property.
[4] Jenkins, Teamcity, or Bamboo are some CI servers which offer this functionality.
This chapter is currently a work in progress.
When using Gradle (or any software package), you can run into problems. You may not understand how to use a particular feature, or you may encounter a defect. Or, you may have a general question about Gradle.
This chapter gives some advice for troubleshooting problems and explains how to get help with your problems.
If you are encountering problems, one of the first things to try is using the very latest release of Gradle. New versions of Gradle are released frequently with bug fixes and new features. The problem you are having may have been fixed in a new release.
If you are using the Gradle Daemon, try temporarily disabling the daemon (you can pass the command line switch
More information about troubleshooting the daemon process is located in Chapter 6, The Gradle Daemon.
The place to go for help with Gradle is http://forums.gradle.org. The Gradle Forums is the place where you can report problems and ask questions of the Gradle developers and other community members.
If something's not working for you, posting a question or problem report to the forums is the fastest way to get help. It's also the place to post improvement suggestions or new ideas. The development team frequently posts news items and announces releases via the forum, making it a great way to stay up to date with the latest Gradle developments.
Gradle provides a programmatic API called the Tooling API, which you can use for embedding Gradle into your own software. This API allows you to execute and monitor builds and to query Gradle about the details of a build. The main audience for this API is IDE, CI server, other UI authors; however, the API is open for anyone who needs to embed Gradle in their application.
A fundamental characteristic of the Tooling API is that it operates in a version independent way. This means that you can use the same API to work with builds that use different versions of Gradle, including versions that are newer or older than the version of the Tooling API that you are using. The Tooling API is Gradle wrapper aware and, by default, uses the same Gradle version as that used by the wrapper-powered build.
Some features that the Tooling API provides:
The Tooling API always uses the Gradle daemon. This means that subsequent calls to the Tooling API, be it model building requests or task executing requests will be executed in the same long-living process. Chapter 6, The Gradle Daemon contains more details about the daemon, specifically information on situations when new daemons are forked.
As the Tooling API is an interface for developers, the Javadoc is the main documentation for it. We provide several samples that live
samples/toolingApi in your Gradle distribution. These samples specify all of the required dependencies for the Tooling API with examples for
querying information from Gradle builds and executing tasks from the Tooling API.
To use the Tooling API, add the following repository and dependency declarations to your build script:
Example 14.1. Using the tooling API
repositories { maven { url 'https://repo.gradle.org/gradle/libs-releases' } } dependencies { compile "org.gradle:gradle-tooling-api:${toolingApiVersion}" // The tooling API need an SLF4J implementation available at runtime, replace this with any other implementation runtime 'org.slf4j:slf4j-simple:1.7.10' }
The main entry point to the Tooling API is the
GradleConnector. You can navigate from there to find code samples and
explore the available Tooling API models.
You can use
GradleConnector.connect() to create a
ProjectConnection connects to a single Gradle project. Using the connection you can execute tasks, tests and retrieve models relative to this project.
The current version of the Tooling API supports running builds using Gradle versions 1.2 and later.
You should note that not all features of the Tooling API are available for all versions of Gradle. For example, build cancellation is only available when a build uses Gradle 2.1 and later. Refer to the documentation for each class and method for more details.
The current Gradle version can be used from Tooling API versions 2.0 or later.
The Tooling API requires Java 7 or later. The Gradle version used by builds may have additional Java version requirements.
Everything in Gradle sits on top of two basic concepts: projects 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.
Each project is made up of 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.
For now, we will look at defining some simple tasks in a build with one project. Later chapters will look at working with multiple projects and more about working with projects and tasks.
You run a Gradle build using the gradle command. The gradle command
looks for a file called
build.gradle in the current directory.
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
In a command-line shell, move to the containing directory and execute the build script with
gradle -q hello:
-q do?
Most of the examples in this user guide are run with the
-q command-line option.
This suppresses Gradle's log messages, so that only the output of the tasks is shown. This keeps the example
output in this user guide a little clearer. You don't need to use this option if you don't want to.
See Chapter 23, Logging for more details about the command-line options which affect Gradle's output.
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
closure containing some Groovy 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.
This functionality is deprecated and will be removed in Gradle 5.0 without replacement.
Use the methods
Task.doFirst(org.gradle.api.Action) and
to define an action instead, as demonstrated by the rest of the examples in this chapter.
There is a shorthand way to define a task like our
hello task above, which is more
Again, this defines a task called
hello with a single closure to execute.
<< operator is simply an alias for
Gradle's build scripts give you the full power of Groovy. As an appetizer, have a look at this:
Example 15.4. Using Groovy in Gradle's tasks
task upper { doLast { String someString = 'mY_nAmE' println "Original: " + someString println "Upper case: " + someString.toUpperCase() } }
Output of
gradle -q upper
> gradle -q upper Original: mY_nAmE Upper case: MY_NAME
Example 15.5. Using Groovy in Gradle's tasks
task count { doLast { 4.times { print "$it " } } }
Output of
gradle -q count
> gradle -q count 0 1 2 3
As you probably have guessed, you can declare tasks that depend on other tasks.
Example 15.6. Declaration of task that depends on other task
task hello { doLast { println 'Hello world!' } } task intro(dependsOn: hello) { doLast { println "I'm Gradle" } }
Output of
gradle -q intro
> gradle -q intro Hello world! I'm Gradle
To add a dependency, the corresponding task does not need to exist.
Example 15.7. Lazy dependsOn - the other task does not exist (yet)
task taskX(dependsOn: 'taskY') { doLast { println 'taskX' } } task taskY { doLast { println 'taskY' } }
Output of
gradle -q taskX
> gradle -q taskX taskY taskX
The dependency of
taskX to
taskY is declared before
taskY is defined. This is very important for multi-project builds. Task dependencies are
discussed in more detail in Section 18.4, "Adding dependencies to a task".
Please notice that you can't use shortcut notation (see Section 15.8, "Shortcut notations") when referring to a task that is not yet defined.
The power of Groovy can be used for more than defining what a task does. For example, you can also use it to dynamically create tasks.
Example 15.8. Dynamic creation of a task
4.times { counter -> task "task$counter" { doLast { println "I'm task number $counter" } } }
Output of
gradle -q task1
> gradle -q task1 I'm task number 1
Once tasks are created 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.
Example 15.9. Accessing a task via API - adding a dependency
4.times { counter -> task "task$counter" { doLast { println "I'm task number $counter" } } } task0.dependsOn task2, task3
Output of
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.
Example 15.10. Accessing a task via API - adding behaviour
task hello { doLast { println 'Hello Earth' } } hello.doFirst { println 'Hello Venus' } hello.doLast { println 'Hello Mars' } hello { doLast { println 'Hello Jupiter' } }
Output of
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.
There is a convenient notation for accessing an existing task. Each task is available as a property of the build script:
Example 15.11. Accessing task as a property of the build script
task hello { doLast { println 'Hello world!' } } hello.doLast { println "Greetings from the $hello.name task." }
Output of
gradle -q hello
> gradle -q hello Hello world! Greetings from the hello task.
This enables very readable code, especially when using the tasks provided by the plugins,
like the
compile task.
You can add your own properties to a task. To add a property named
ext.myProperty to an initial value. From that point on, the property can be read
and set like a predefined task property.
Example 15.12. Adding extra properties to a task
task myTask {
ext.myProperty = "myValue"
task printTaskProperties {
doLast {
println myTask.myProperty
Output of
gradle -q printTaskProperties
> gradle -q printTaskProperties myValue
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
file. From the example below, you can learn how to execute Ant tasks and how to access Ant properties:
Example 15.13. Using AntBuilder to execute ant.loadfile target
task loadfile { doLast { def files = file('../antLoadfileResources').listFiles().sort() files.each { File file -> if (file.isFile()) { ant.loadfile(srcFile: file, property: file.name) println " *** $file.name ***" println "${ant.properties[file.name]}" } } } }
Output of
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 Chapter 20, Using Ant from Gradle.
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.
Example 15.14. Using methods to organize your build logic
task checksum { doLast { fileList('../antLoadfileResources').each { File file -> ant.checksum(file: file, property: "cs_$file.name") println "$file.name Checksum: ${ant.properties["cs_$file.name"]}" } } } task 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() }
Output of
gradle -q loadfile
> gradle -q loadfile I'm fond of agile.manifesto.txt I'm fond of gradle.manifesto.txt
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 Chapter 42, Organizing Build Logic.
Gradle allows you to define one or more default tasks that are executed if no other tasks are specified.
Example 15.15. Defining a default task
defaultTasks 'clean', 'run' task clean { doLast { println 'Default Cleaning!' } } task run { doLast { println 'Default Running!' } } task other { doLast { println "I'm not a default task!" } }
Output of
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).
As we later describe in full detail (see Chapter 21, The Build Lifecycle), Gradle has a configuration phase and an execution phase. After the configuration phase, Gradle knows all tasks that should be executed. Gradle offers you a hook to make use of this information. A use-case for this would be to check if the release task is among the tasks to be executed. Depending on this, you can assign different values to some variables.
In the following example, execution of the
distribution and
release tasks results in different value of the
version variable.
Example 15.16. Different outcomes of build depending on chosen tasks
task distribution { doLast { println "We build the zip with version=$version" } } task release(dependsOn: 'distribution') { doLast { println 'We release now' } } gradle.taskGraph.whenReady {taskGraph -> if (taskGraph.hasTask(release)) { version = '1.0' } else { version = '1.0-SNAPSHOT' } }
Output of
gradle -q distribution
> gradle -q distribution We build the zip with version=1.0-SNAPSHOT
Output of
gradle -q release
> gradle -q release We build the zip with version=1.0 We release now
The important thing is that
whenReady affects the release task before the release task is executed.
This works even when the release task is not the primary task (i.e., the task passed to the gradle command).
In this chapter, we have had a first look at tasks. But this is not the end of the story for tasks. If you want to jump into more of the details, have a look at Chapter 18, More about Tasks.
Otherwise, continue on to the tutorials in Chapter 45, Java Quickstart and Chapter 7, Dependency Management Basics.
[5] There are command line switches to change this behavior. See Appendix D, Gradle Command Line)
The Build Init plugin is currently incubating. Please be aware that the DSL and other configuration may change in later Gradle versions.
The Gradle Build Init plugin can be used to bootstrap the process of creating a new Gradle build. It supports creating brand new projects of different types as well as converting existing builds (e.g. An Apache Maven build) to be Gradle builds.
Gradle plugins typically need to be
to a project before they can be used (see Section 26.3, "Using plugins").
The Build Init plugin is an automatically applied plugin, which means you do not need to apply it explicitly.
To use the plugin, simply execute the task named
where you would like to create the Gradle build.
There is no need to create a "stub"
file in order to apply the plugin.
It also leverages the
task from the Wrapper plugin (see Chapter 22, Wrapper Plugin),
which means that the Gradle Wrapper will also be installed into the project.
The plugin adds the following tasks to the project:
supports different build setup types. The type is specified by supplying a
argument value. For example, to create a Java library project simply execute:
gradle init --type java-library.
If a
parameter is not supplied, Gradle will attempt to infer the type from the environment. For example, it will
infer a type value of "
pom" if it finds a
to convert to a Gradle build.
If the type could not be inferred, the type "
basic" will be used.
All build setup types include the setup of the Gradle Wrapper.
The "
pom" type can be used to convert an Apache Maven build to a Gradle build.
This works by converting the POM to one or more Gradle files.
It is only able to be used if there is a valid "
file in the directory that the
init task is invoked in or, if invoked via
the "
-p" commandline option, in the specified project directory.
This "
pom" type will be automatically inferred if such a file exists.
The Maven conversion implementation was inspired by the maven2gradle tool that was originally developed by Gradle community members.
The conversion process has the following features:
The "
java-library" build init type is not inferable. It must be explicitly specified.
It has the following features:
java" plugin
jcenter" dependency repository
Alternative test framework can be specified by supplying a
--test-framework argument value.
To use a different test framework, execute one of the following commands:
The "
scala-library" build init type is not inferable. It must be explicitly specified.
It has the following features:
scala" plugin
jcenter" dependency repository
The "
groovy-library" build init type is not inferable. It must be explicitly specified.
It has the following features:
groovy" plugin
jcenter" dependency repository
This chapter looks at some of the details of writing a build script.
Gradle provides a domain specific language, or DSL, for describing builds. This build language is based on Groovy, with some additions to make it easier to describe a build.
A build script can contain any Groovy language element. [6] Gradle assumes that each build script is encoded using UTF-8.
In the tutorial in Chapter 45, Java Quickstart we used, for example, the
apply() method. Where does this method come from? We said earlier that the build script
defines a project in Gradle. For each project in the build, Gradle creates an object of type
Project and associates this
Project object with
the build script. As the build script executes, it configures this
Project object:
Don't forget that your build script is simply Groovy code that drives the Gradle API. And the
Project interface is your starting point for accessing everything
in the Gradle API. So, if you're wondering what 'tags' are available in your build script, you can
start with the documentation for the
Project interface.
Any method you call in your build script which is not defined
in the build script, is delegated to the
Project object.
Any property you access in your build script, which is not defined
in the build script, is delegated to the
Project object.
Let's try this out and try to access the
name property of the
Project object.
Example 17.1. Accessing property of the Project object
println name println project.name
Output of
gradle -q check
> gradle -q check projectApi projectApi
println statements print out the same property. The first uses auto-delegation to
Project object, for properties not defined in the build script. The other
statement uses the
project property available to any build script, which returns the
Project object. Only if you define a property or a method which has the
same name as a member of the
Project object, would you need to use the
Project object provides some standard properties, which are available in
your build script. The following table lists a few of the commonly used ones.
Table 17.1. Project Properties
|Default Value
Project instance
|The name of the project directory.
|The absolute path of the project.
|A description for the project.
|The directory containing the build script.
AntBuilder instance
When Gradle executes a script, it compiles the script into a class which implements
This means that all of the properties and methods declared by the
Script interface
are available in your script.
There are two kinds of variables that can be declared in a build script: local variables and extra properties.
Local variables are declared with the
def keyword. They are only visible in the scope where they have been declared.
Local variables are a feature of the underlying Groovy language.
Example 17.2. Using local variables
def dest = "dest" task copy(type: Copy) { from "source" into dest }
All enhanced objects in Gradle's domain model can hold extra user-defined properties. This includes, but is not limited to, projects, tasks,
and source sets. Extra properties can be added, read and set via the owning object's
ext property. Alternatively, an
ext block can be used to add multiple properties at once.
Example 17.3. Using extra properties
apply plugin: "java" ext { springVersion = "3.1.0.RELEASE" emailNotification = "firstname.lastname@example.com" } sourceSets.all { ext.purpose = null } sourceSets { main { purpose = "production" } test { purpose = "test" } plugin { purpose = "production" } } task printProperties { doLast { println springVersion println emailNotification sourceSets.matching { it.purpose == "production" }.each { println it.name } } }
Output of
gradle -q printProperties
> gradle -q printProperties 3.1.0.RELEASE firstname.lastname@example.com main plugin
In this example, an
ext block adds two extra properties to the
project object. Additionally,
a property named
purpose is added to each source set by setting
ext.purpose to
null (
null is a permissible value). Once the properties have been added, they can be read and set like
predefined properties.
By requiring special syntax for adding a property, Gradle can fail fast when an attempt is made to set a (predefined or extra) property but the property is misspelled or does not exist. Extra properties can be accessed from anywhere their owning object can be accessed, giving them a wider scope than local variables. Extra properties on a project are visible from its subprojects.
For further details on extra properties and their API, see the
ExtraPropertiesExtension class in the API documentation.
You can configure arbitrary objects in the following very readable way.
Example 17.4. Configuring arbitrary objects
task configure { doLast { def pos = configure(new java.text.FieldPosition(10)) { beginIndex = 1 endIndex = 5 } println pos.beginIndex println pos.endIndex } }
Output of
gradle -q configure
> gradle -q configure 1 5
You can also configure arbitrary objects using an external script.
Example 17.5. Configuring arbitrary objects using a script
task configure { doLast { def pos = new java.text.FieldPosition(10) // Apply the script apply from: 'other.gradle', to: pos println pos.beginIndex println pos.endIndex } }
// Set properties. beginIndex = 1 endIndex = 5
Output of
gradle -q configure
> gradle -q configure 1 5
The Groovy language provides plenty of features for creating DSLs, and the Gradle build language takes advantage of these. Understanding how the build language works will help you when you write your build script, and in particular, when you start to write custom plugins and tasks.
Groovy adds lots of useful methods to the standard Java classes. For example,
Iterable gets
each method, which iterates over the elements of the
Example 17.6. Groovy JDK methods
// Iterable gets an each() method
configurations.runtime.each { File f -> println f }
Have a look at http://groovy-lang.org/gdk.html for more details.
Groovy automatically converts a property reference into a call to the appropriate getter or setter method.
Example 17.7. Property accessors
// Using a getter method println project.buildDir println getProject().getBuildDir() // Using a setter method project.buildDir = 'target' getProject().setBuildDir('target')
Parentheses are optional for method calls.
Example 17.8. Method call without parentheses
test.systemProperty 'some.prop', 'value' test.systemProperty('some.prop', 'value')
Groovy provides some shortcuts for defining
List and
instances. Both kinds of literals are straightforward, but map literals have some interesting twists.
For instance, the "
apply" method (where you typically apply plugins) actually
takes a map parameter. However, when you have a line like "
apply plugin:'java'", you
aren't actually using a map literal, you're actually using "named parameters", which have almost exactly
the same syntax as a map literal (without the wrapping brackets). That named parameter list gets converted
to a map when the method is called, but it doesn't start out as a map.
Example 17.9. List and map literals
// 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'
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:
Example 17.10. Closure as method parameter
repositories { println "in a closure" } repositories() { println "in a closure" } repositories({ println "in a closure" })
Each closure has a
delegate object, which Groovy uses to look up variable and method
references which are not local variables or parameters of the closure. Gradle uses this for
configuration closures, where the
delegate object is set to
the object to be configured.
Example 17.11. Closure delegates
dependencies { assert delegate == project.dependencies testCompile('junit:junit:4.12') delegate.testCompile('junit:junit:4.12') }
To make build scripts more concise, Gradle automatically adds a set of import statements to the Gradle scripts.
This means that instead of using
throw new org.gradle.api.tasks.StopExecutionException()
you can just type
throw new StopExecutionException()
Listed below are the imports added to each script:
Figure 17.1. gradle-imports
[6] Any language element except for statement labels.
In the introductory tutorial (Chapter 15, Build Script Basics) 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 enhanced tasks, which are tasks that have their own properties and methods. This is really different from what you are used to with Ant targets. Such enhanced tasks are either provided by you or built into Gradle.
We have already seen how to define tasks using a keyword style in Chapter 15, Build Script Basics. There are a few variations on this style, which you may need to use in certain situations. For example, the keyword style does not work in expressions.
Example 18.1. Defining tasks
task(hello) { doLast { println "hello" } } task(copy, type: Copy) { from(file('srcDir')) into(buildDir) }
You can also use strings for the task names:
Example 18.2. Defining tasks - using strings for task names
task('hello') { doLast { println "hello" } } task('copy', type: Copy) { from(file('srcDir')) into(buildDir) }
There is an alternative syntax for defining tasks, which you may prefer to use:
Example 18.3. Defining tasks with alternative syntax
tasks.create(name: 'hello') { doLast { println "hello" } } tasks.create(name: 'copy', type: Copy) { from(file('srcDir')) into(buildDir) }
Here we add tasks to the
tasks collection. Have a look at
TaskContainer for more variations of the
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, each task is available as a property of the project, using the task name as the property name:
Example 18.4. Accessing tasks as properties
task hello println hello.name println project.hello.name
Tasks are also available through the
tasks collection.
Example 18.5. Accessing tasks via tasks collection
task hello
println tasks.hello.name
println tasks['hello'].name
You can access tasks from any project using the task's path using the
method. You can call the
getByPath() method with a task name, or a relative path, or an
absolute path.
Example 18.6. Accessing tasks by path
project(':projectA') { task hello } task hello println tasks.getByPath('hello').path println tasks.getByPath(':hello').path println tasks.getByPath('projectA:hello').path println tasks.getByPath(':projectA:hello').path
Output of
gradle -q hello
> gradle -q hello :hello :hello :projectA:hello :projectA:hello
Have a look at
TaskContainer for more options for locating tasks.
As an example, let's look at the
Copy task provided by Gradle. To create a
Copy task for your build, you can declare in your build script:
This creates a copy task with no default behavior.
The task can be configured using its API (see
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.
Example 18.8. Configuring a task - various ways
Copy myCopy = task(myCopy, type: Copy) myCopy.from 'resources' myCopy.into 'target' myCopy.include('**/*.txt', '**/*.xml', '**/*.properties')
This is similar to the way we would configure objects in Java. You have to repeat the context
myCopy) in the configuration statement every time. This is a redundancy and not very
nice to read.
There is another way of configuring a task. It also preserves the context and it is arguably the most readable. It is usually our favorite.
Example 18.9. Configuring a task - with closure
task myCopy(type: Copy) myCopy { from 'resources' into 'target' include('**/*.txt', '**/*.xml', '**/*.properties') }
This works for any task. Line 3 of the example is just a shortcut for the
tasks.getByName() method. It is important to note that if you pass a closure to the
getByName() method, this closure is applied to configure the task, not when
the task executes.
You can also use a configuration closure when you define a task.
Example 18.10. Defining a task with closure
task copy(type: Copy) { from 'resources' into 'target' include('**/*.txt', '**/*.xml', '**/*.properties') }
A task has both configuration and actions.
When using the
<<, you are simply using a shortcut to define an action.
Code defined in the configuration section of your task will get executed during the configuration phase of the build regardless of what task was targeted.
See Chapter 21, The Build Lifecycle for more details about the build lifecycle.
There are several ways you can define the dependencies of a task. In
Section 15.5, "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
Example 18.11. Adding dependency on task from another project
project('projectA') { task taskX(dependsOn: ':projectB:taskY') { doLast { println 'taskX' } } } project('projectB') { task taskY { doLast { println 'taskY' } } }
Output of
gradle -q taskX
> gradle -q taskX taskY taskX
Instead of using a task name, you can define a dependency using a
Task object, as shown in this example:
Example 18.12. Adding dependency using task object
task taskX { doLast { println 'taskX' } } task taskY { doLast { println 'taskY' } } taskX.dependsOn taskY
Output of
gradle -q taskX
> gradle -q taskX taskY taskX
For more advanced uses, you can define a task dependency using a closure. When evaluated, the closure is
passed the task whose dependencies are being calculated. The closure 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
to all the tasks in the project whose name starts with
Example 18.13. Adding dependency using closure
task taskX { doLast { println 'taskX' } } taskX.dependsOn { tasks.findAll { task -> task.name.startsWith('lib') } } task lib1 { doLast { println 'lib1' } } task lib2 { doLast { println 'lib2' } } task notALib { doLast { println 'notALib' } }
Output of
gradle -q taskX
> gradle -q taskX lib1 lib2 taskX
For more information about task dependencies, see the
Task API.
Task ordering is an incubating feature. Please be aware that this feature may change in later Gradle versions.
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:
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.
Example 18.14. Adding a 'must run after' task ordering
task taskX { doLast { println 'taskX' } } task taskY { doLast { println 'taskY' } } taskY.mustRunAfter taskX
Output of
gradle -q taskY taskX
> gradle -q taskY taskX taskX taskY
Example 18.15. Adding a 'should run after' task ordering
task taskX { doLast { println 'taskX' } } task taskY { doLast { println 'taskY' } } taskY.shouldRunAfter taskX
Output of
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:
Example 18.16. Task ordering does not imply task execution
Output of
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
Note that "
B.mustRunAfter(A)" or "
B.shouldRunAfter(A)" does not imply any execution dependency between the tasks:
A and
B independently. The ordering rule only has an effect when both tasks are scheduled for execution.
--continue, it is possible for
B to execute in the event that
A fails.
As mentioned before, the "should run after" ordering rule will be ignored if it introduces an ordering cycle:
Example 18.17. A 'should run after' task ordering is ignored if it introduces an ordering cycle
task taskX { doLast { println 'taskX' } } task taskY { doLast { println 'taskY' } } task taskZ { doLast { println 'taskZ' } } taskX.dependsOn taskY taskY.dependsOn taskZ taskZ.shouldRunAfter taskX
Output of
gradle -q taskX
> gradle -q taskX taskZ taskY taskX
You can add a description to your task. This description is displayed when executing
gradle tasks.
Example 18.18. Adding a description to a task
task copy(type: Copy) { description 'Copies the resource directory to the target directory.' from 'resources' into 'target' include('**/*.txt', '**/*.xml', '**/*.properties') }
Sometimes you want to replace a task. For example, if you want to exchange a task added by the Java plugin with a custom task of a different type. You can achieve this with:
Example 18.19. Overwriting a task
task copy(type: Copy)
task copy(overwrite: true) {
doLast {
println('I am the new one.')
Output of
gradle -q copy
> gradle -q copy I am the new one.
This will replace a task of type
Copy with the task you've defined, because it
uses the same name. When you define the new task, you have to set the
overwrite property
to true. Otherwise Gradle throws an exception, saying that a task with that name already exists.
Gradle offers multiple ways to skip the execution of a task.
You can use the
onlyIf() method to attach a predicate to a task. The task's
actions are only executed if the predicate evaluates to true. You implement the predicate as a closure.
The closure 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 due
to be executed.
Example 18.20. Skipping a task using a predicate
task hello { doLast { println 'hello world' } } hello.onlyIf { !project.hasProperty('skipHello') }
Output of
gradle hello -PskipHello
> gradle hello -PskipHello :hello SKIPPED BUILD SUCCESSFUL Total time: 1 secs
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.
Example 18.21. Skipping tasks with StopExecutionException
task compile { doLast { println 'We are doing the compile.' } } compile.doFirst { // Here you would put arbitrary conditions in real life. // But this is used in an integration test so we want defined behavior. if (true) { throw new StopExecutionException() } } task myTask(dependsOn: 'compile') { doLast { println 'I am not affected' } }
Output of
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. [7]
Every task has an
flag which defaults to
true. Setting it to
false prevents the
execution of any of the task's actions.
Example 18.22. Enabling and disabling tasks
task disableMe {
doLast {
println 'This should not be printed if the task is disabled.'
disableMe.enabled = false
Output of
gradle disableMe
> gradle disableMe :disableMe SKIPPED BUILD SUCCESSFUL Total time: 1 secs
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 it calls incremental build.
You have almost certainly already seen it in action: it's active nearly[8] every time the
UP-TO-DATE text appears next to the name of a
task when you run a build.
How does incremental build work? And what does it take to make use of it in your own tasks? Let's take a look.
In the most common case, a task takes some inputs and generates some outputs. If we use the compilation example from earlier, we can see that the source files are the inputs and, in the case of Java, the generated class files are the outputs. Other inputs might include things like whether debug information should be included.
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 the compilation runs in a
forked process or not, determined by the
fork property, has no
impact on what bytecode gets generated. In Gradle terminology,
is just an internal task property.
As part of incremental build, Gradle tests whether any of the task inputs or outputs have 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.
If you're implementing a custom task as a class, then it takes just two steps to make it work with incremental build:
Gradle supports three main categories of inputs and outputs:
Simple values
Things like strings and numbers. More generally, a simple value can have
any type that implements
Filesystem types
These consist of the standard
File class but also
derivatives of Gradle's
type and anything else that can be passed to either the
method - for single file/directory properties - or the
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:
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:
Example 18.23. Custom task class
package org.example; import java.io.File; import java.util.HashMap; import org.gradle.api.*; import org.gradle.api.file.*; import org.gradle.api.tasks.*; public class ProcessTemplates extends DefaultTask { private TemplateEngineType templateEngine; private FileCollection sourceFiles; private TemplateData templateData; private File outputDir; @Input public TemplateEngineType getTemplateEngine() { return this.templateEngine; } @InputFiles public FileCollection getSourceFiles() { return this.sourceFiles; } @Nested public TemplateData getTemplateData() { return this.templateData; } @OutputDirectory public File getOutputDir() { return this.outputDir; } // + setter methods for the above - assume we?ve defined them @TaskAction public void processTemplates() { // ... } }
package org.example; import java.util.HashMap; import java.util.Map; import org.gradle.api.tasks.Input; public class TemplateData { private String name; private Map<String, String> variables; public TemplateData(String name, Map<String, String> variables) { this.name = name; this.variables = new HashMap<>(variables); } @Input public String getName() { return this.name; } @Input public Map<String, String> getVariables() { return this.variables; } }
Output of
gradle processTemplates
> gradle processTemplates :processTemplates BUILD SUCCESSFUL
Output of
gradle processTemplates
> gradle processTemplates :processTemplates UP-TO-DATE BUILD SUCCESSFUL
There's plenty to talk about in this example, so let's work through each of the input and output properties in turn:
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
annotation, just as we would with a
String property.
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.
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 within
TemplateData - a string and a hash map
with serializable type parameters - are serializable and can be annotated with
@Input. We use
@Nested on
templateData to let Gradle know that this is a value with
nested input properties.
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 have 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.
Table 18.1. Incremental build annotations
|Expected property type
|Any serializable type
|A simple input value
|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 with `@OrderSensitive` and `@PathSensitive(RELATIVE)`, but it will also ignore the names of JAR files directly added to the classpath.
|A single output file (not directory)
|A single output directory (not file)
|An iterable of output files (no directories)
|An iterable of output directories (no files)
|Any custom type
|A custom type that may not implement
Serializable but does
have at least one field or property marked with one of the annotations in this
table. It could even be another
|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.
|Used with
@InputFiles or
to tell Gradle to skip the task if the corresponding iterable of files or
directory are empty.
|Used with any of the other incremental build 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
@InputFiles or
to tell Gradle that a change in the order of the files should mark the task
|Used with any input file property to tell Gradle to only consider the given
part of the file paths as important. For example, if a property is annotated
@PathSensitive(PathSensitivity.NAME_ONLY), then
moving the files around without changing their contents will not make the
task out-of-date.
|* In fact,
can be any type accepted by
Iterable<File> can be any type accepted by
This includes instances of
Callable, such as closures,
allowing for lazy evaluation of the property values. Be aware that the types
FileCollection and
FileTree are
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.
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.
This runtime API is provided through a couple of aptly named properties that are available on every Gradle task:
Task.getInputs() of type
Task.getOutputs() of type
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. All it lacks is validation of whether declared files are actually files and declared directories are directories. Nor will it create output directories if they don't exist. But that's it.
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:
Example 18.24. Ad-hoc task
task processTemplatesAdHoc { inputs.property("engine", TemplateEngineType.FREEMARKER) inputs.files(fileTree("src/templates")) inputs.property("templateData.name", "docs") inputs.property("templateData.variables", [year: 2013]) outputs.dir("$buildDir/genOutput2") doLast { // Process the templates here } }
Output of
gradle processTemplatesAdHoc
> gradle processTemplatesAdHoc :processTemplatesAdHoc BUILD SUCCESSFUL
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
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
The only difference is that the
dir() and
dirs() methods don't validate
the type of file object at the given path (file or directory), unlike the annotations.
One notable difference between the runtime API and the annotations is the
lack of a method that corresponds directly to
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
Another type of example involves adding input and output definitions to
instances of a custom task class that lacks the requisite annotations. For
example, imagine that the
ProcessTemplates task is provided
by a plugin and that it's missing the incremental build annotations. In order
to make up for that deficiency, you can use the runtime API:
Example 18.25. Using runtime API with custom task type
task processTemplatesRuntime(type: ProcessTemplatesNoAnnotations) { templateEngine = TemplateEngineType.FREEMARKER sourceFiles = fileTree("src/templates") templateData = new TemplateData("test", [year: 2014]) outputDir = file("$buildDir/genOutput3") inputs.property("engine",templateEngine) inputs.files(sourceFiles) inputs.property("templateData.name", templateData.name) inputs.property("templateData.variables", templateData.variables) outputs.dir(outputDir) }
Output of
gradle processTemplatesRuntime
> gradle processTemplatesRuntime :processTemplatesRuntime BUILD SUCCESSFUL
Output of
gradle processTemplatesRuntime
> gradle processTemplatesRuntime :processTemplatesRuntime UP-TO-DATE BUILD SUCCESSFUL
As you can see, we can both configure the tasks properties and use those
properties as arguments to the incremental build runtime API. 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. Note that if the
task type is already using the incremental build annotations, the runtime API
will add inputs and outputs rather than replace them.
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 let's
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:
Example 18.26. Using skipWhenEmpty() via the runtime API
task processTemplatesRuntimeConf(type: ProcessTemplatesNoAnnotations) { // ... sourceFiles = fileTree("src/templates") { include "**/*.fm" } inputs.files(sourceFiles).skipWhenEmpty() // ... }
Output of
gradle clean processTemplatesRuntimeConf
> gradle clean processTemplatesRuntimeConf :processTemplatesRuntimeConf UP-TO-DATE BUILD SUCCESSFUL
So the
TaskInputs.files() method returns a builder that has
skipWhenEmpty() method. By calling this method, we tell
Gradle that the task should be considered up to date if the corresponding input
file collection is empty, even if the task hasn't run before.
Prior to Gradle 3.0, you had to use the
TaskInputs.sourceDir() methods to get the same behavior
as with
skipWhenEmpty(). These methods are now deprecated and
should not be used with Gradle 3.0 and above.
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.
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.
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:
Example 18.27. Inferred task dependency via task outputs
task packageFiles(type: Zip) { from processTemplates.outputs }
Output of
gradle clean packageFiles
> gradle clean packageFiles :processTemplates :packageFiles BUILD SUCCESSFUL
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
Example 18.28. Inferred task dependency via a task argument
task packageFiles2(type: Zip) { from processTemplates }
Output of
gradle clean packageFiles2
> gradle clean packageFiles2 :processTemplates :packageFiles2 BUILD SUCCESSFUL
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!
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
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.
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.
One last 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 Chapter 9, Continuous build.
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.
Have you ever wondered how the
from() method of the
Copy task works? It's not annotated with
and yet any files passed to it are treated as formal inputs of the task. What's
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
class we introduced earlier:
Example 18.29. Declaring a method to add task inputs
public class ProcessTemplates extends DefaultTask { // ... private FileCollection sourceFiles = getProject().files(); @SkipWhenEmpty @InputFiles @PathSensitive(PathSensitivity.NONE) public FileCollection getSourceFiles() { return this.sourceFiles; } public void sources(FileCollection sourceFiles) { this.sourceFiles = this.sourceFiles.plus(sourceFiles); } // ... }
task processTemplates(type: ProcessTemplates) { templateEngine = TemplateEngineType.FREEMARKER templateData = new TemplateData("test", [year: 2012]) outputDir = file("$buildDir/genOutput") sources fileTree("src/templates") }
Output of
gradle processTemplates
> gradle processTemplates :processTemplates BUILD SUCCESSFUL
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
project.files() method like so:
Example 18.30. Declaring a method to add a task as an input
// ... public void sources(Task inputTask) { this.sourceFiles = this.sourceFiles.plus(getProject().files(inputTask)); } // ...
task copyTemplates(type: Copy) { into "$buildDir/tmp" from "src/templates" } task processTemplates2(type: ProcessTemplates) { // ... sources copyTemplates }
Output of
gradle processTemplates2
> gradle processTemplates2 :copyTemplates :processTemplates2 BUILD SUCCESSFUL
This technique can make your custom task easier to use and result in cleaner build
files. As an added benefit, our use of
getProject().files() 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
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
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
destinationDir property - as the input of a custom task that
instruments a set of files containing Java bytecode. This custom task, which we'll
Instrument, has a
classFiles property
annotated with
@InputFiles. You might initially try to configure
the task like so:
Example 18.31. Failed attempt at setting up an inferred task dependency
apply plugin: "java" task badInstrumentClasses(type: Instrument) { classFiles = fileTree(compileJava.destinationDir) destinationDir = file("$buildDir/instrumented") }
Output of
gradle clean badInstrumentClasses
> gradle clean badInstrumentClasses :clean UP-TO-DATE :badInstrumentClasses UP-TO-DATE BUILD SUCCESSFUL
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:
Example 18.32. Setting up an inferred task dependency between output dir and input files
task instrumentClasses(type: Instrument) {
classFiles = compileJava.outputs.files
destinationDir = file("$buildDir/instrumented")
Output of
gradle clean instrumentClasses
> gradle clean instrumentClasses :clean :compileJava :instrumentClasses BUILD SUCCESSFUL
Alternatively, you can get Gradle to access the appropriate property itself by using the
project.files() method in place of
Example 18.33. Setting up an inferred task dependency with files()
task instrumentClasses2(type: Instrument) {
classFiles = files(compileJava)
destinationDir = file("$buildDir/instrumented")
Output of
gradle clean instrumentClasses2
> gradle clean instrumentClasses2 :clean :compileJava :instrumentClasses2 BUILD SUCCESSFUL
Remember that
files() 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:
Example 18.34. Setting up an inferred task dependency with builtBy()
task instrumentClassesBuiltBy(type: Instrument) {
classFiles = fileTree(compileJava.destinationDir) {
builtBy compileJava
destinationDir = file("$buildDir/instrumented")
Output of
gradle clean instrumentClassesBuiltBy
> gradle clean instrumentClassesBuiltBy :clean :compileJava :instrumentClassesBuiltBy BUILD SUCCESSFUL
You can of course just add an explicit task dependency via
but the above approach provides more semantic meaning, explaining why
compileJava has to run beforehand.
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
comes in. This takes a predicate function that is used to determine whether a task is up to
date or not. One use case is to disable up-to-date checks completely for a task, like so:
Example 18.35. Ignoring up-to-date checks
task alwaysInstrumentClasses(type: Instrument) {
classFiles = files(compileJava)
destinationDir = file("$buildDir/instrumented")
outputs.upToDateWhen { false }
Output of
gradle clean alwaysInstrumentClasses
> gradle clean alwaysInstrumentClasses :compileJava :alwaysInstrumentClasses BUILD SUCCESSFUL
Output of
gradle alwaysInstrumentClasses
> gradle alwaysInstrumentClasses :compileJava UP-TO-DATE :alwaysInstrumentClasses BUILD SUCCESSFUL
{ false } closure ensures that
will always perform the copy, irrespective of whether there is no change in the inputs or
You can of course put more complex logic into the closure. 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 an up-to-date check at all. 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
For example, in cases where you want to skip a task when a particular property is set or
not set.
Before a task is executed for the first time, Gradle takes a snapshot of the inputs. This snapshot 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 snapshot of the outputs. This snapshot contains the set of output files and a hash of the contents of each file. Gradle persists both snapshots for the next time the task is executed.
Each time after that, before the task is executed, Gradle takes a new snapshot of the inputs and outputs. If the new snapshots are the same as the previous snapshots, 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 snapshots for the next time the task is executed.
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 snapshot 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
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:
Example 18.36. Task rule
tasks.addRule("Pattern: ping<ID>") { String taskName -> if (taskName.startsWith("ping")) { task(taskName) { doLast { println "Pinging: " + (taskName - 'ping') } } } }
Output of
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:
Example 18.37. Dependency on rule based tasks
tasks.addRule("Pattern: ping<ID>") { String taskName -> if (taskName.startsWith("ping")) { task(taskName) { doLast { println "Pinging: " + (taskName - 'ping') } } } } task groupPing { dependsOn pingServer1, pingServer2 }
Output of
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.
Finalizers tasks are an incubating feature (see Section C.1.2, "Incubating").
Finalizer tasks are automatically added to the task graph when the finalized task is scheduled to run.
Example 18.38. Adding a task finalizer
task taskX { doLast { println 'taskX' } } task taskY { doLast { println 'taskY' } } taskX.finalizedBy taskY
Output of
gradle -q taskX
> gradle -q taskX taskX taskY
Finalizer tasks will be executed even if the finalized task fails.
Example 18.39. Task finalizer for a failing task
task taskX { doLast { println 'taskX' throw new RuntimeException() } } task taskY { doLast { println 'taskY' } } taskX.finalizedBy taskY
Output of
gradle -q taskX
> gradle -q taskX taskX taskY
On the other hand, finalizer tasks are not executed if the finalized task didn't do any work, for example if it is considered up to date or if a dependent task fails.
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
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.
[7] You might be wondering why there is neither an import for the
nor do we access it via its fully qualified name. The reason is, that Gradle adds a set of default imports
to your script (see Section 17.8, "Default imports").
[8] You will also see
UP-TO-DATE next to tasks that have no actions,
even though that's nothing to do with incremental build.
Most builds work with files. Gradle adds some concepts and APIs to help you achieve this.
You can locate a file relative to the project directory using the
Project.file(java.lang.Object) method.
Example 19.1. Locating files
// Using a relative path File configFile = file('src/config.xml') // Using an absolute path configFile = file(configFile.absolutePath) // Using a File object with a relative path configFile = file(new File('src/config.xml'))
You can pass any object to the
file() method, and it will attempt to convert the value
to an absolute
File object. Usually, you would pass it a
String or
File instance. If this path is an absolute path, it is used
to construct a
File instance. Otherwise, a
File instance is
constructed by prepending the project directory path to the supplied path. The
method also understands URLs, such as
Using this method is a useful way to convert some user provided value into an absolute
It is preferable to using
new File(somePath), as
file() always evaluates
the supplied path relative to the project directory, which is fixed, rather than the current working
directory, which can change depending on how the user runs Gradle.
A file collection is simply a set of files. It is represented by the
FileCollection interface. Many objects in the Gradle API implement
this interface. For example, dependency configurations implement
One way to obtain a
FileCollection instance is to use the
Project.files(java.lang.Object[]) method. You can pass this method any number of
objects, which are then converted into a set of
File objects. The
files() method accepts any type of object as its parameters. These are evaluated relative
to the project directory, as per the
file() method, described in Section 19.1, "Locating files".
You can also pass collections, iterables, maps and arrays to the
files() method. These are flattened
and the contents converted to
File instances.
Example 19.2. Creating a file collection
FileCollection collection = files('src/file1.txt', new File('src/file2.txt'), ['src/file3.txt', 'src/file4.txt'])
A file collection is iterable, and can be converted to a number of other types using the
operator. You can also add 2 file collections together using the
+ operator, or subtract one
file collection from another using the
- operator.
Here are some examples of what you can do with a file collection.
Example 19.3. Using a file collection
// Iterate over the files in the collection collection.each { File file -> println file.name } // Convert the collection to various types Set set = collection.files Set set2 = collection as Set List list = collection as List String path = collection.asPath File file = collection.singleFile File file2 = collection as File // Add and subtract collections def union = collection + files('src/file3.txt') def different = collection - files('src/file3.txt')
You can also pass the
files() method a closure or a
instance. This is called when the contents of the collection are queried, and its return value is converted
to a set of
File instances. The return value can be an object of any of the types
supported by the
files() method. This is a simple way to 'implement' the
FileCollection interface.
Example 19.4. Implementing a file collection
task list { doLast { File srcDir // Create a file collection using a closure collection = files { srcDir.listFiles() } srcDir = file('src') println "Contents of $srcDir.name" collection.collect { relativePath(it) }.sort().each { println it } srcDir = file('src2') println "Contents of $srcDir.name" collection.collect { relativePath(it) }.sort().each { println it } } }
Output of
gradle -q list
> gradle -q list Contents of src src/dir1 src/file1.txt Contents of src2 src2/dir1 src2/dir2
Some other types of things you can pass to
These are flattened and the contents included in the file collection.
The output files of the task are included in the file collection.
The output files of the TaskOutputs are included in the file collection.
It is important to note that the content of a file collection is evaluated lazily, when it is needed.
This means you can, for example, create a
FileCollection that represents files which
will be created in the future by, say, some task.
A file tree is a collection of files arranged in a hierarchy. For example, a file tree
might represent a directory tree or the contents of a ZIP file. It is represented
by the
FileTree interface. The
FileTree interface
FileCollection, so you can treat a file tree exactly the same way as you would a
file collection. Several objects in Gradle implement the
FileTree interface, such as
source sets.
One way to obtain a
FileTree instance is to use the
Project.fileTree(java.util.Map) method.
This creates a
FileTree defined with a base directory, and optionally some Ant-style
include and exclude patterns.
Example 19.5. Creating a file tree
// Create a file tree with a base directory FileTree tree = fileTree(dir: 'src/main') // Add include and exclude patterns to the tree tree.include '**/*.java' tree.exclude '**/Abstract*' // Create a tree using path tree = fileTree('src').include('**/*.java') // Create a tree using closure tree = fileTree('src') { include '**/*.java' } // Create a tree using a map tree = fileTree(dir: 'src', include: '**/*.java') tree = fileTree(dir: 'src', includes: ['**/*.java', '**/*.xml']) tree = fileTree(dir: 'src', include: '**/*.java', exclude: '**/*test*/**')
You use a file tree in the same way you use a file collection. You can also visit the contents of the tree, and select a sub-tree using Ant-style patterns:
Example 19.6. Using a file tree
// Iterate over the contents of a tree tree.each {File file -> println file } // Filter a tree FileTree filtered = tree.matching { include 'org/gradle/api/**' } // Add trees together FileTree sum = tree + fileTree(dir: 'src/test') // Visit the elements of the tree tree.visit {element -> println "$element.relativePath => $element.file" }
You can use the contents of an archive, such as a ZIP or TAR file, as a file tree. You do this using
Project.zipTree(java.lang.Object) and
Project.tarTree(java.lang.Object) methods. These methods return a
instance which you can use like any other file tree or file collection. For example, you can use it to expand
the archive by copying the contents, or to merge some archives into another.
Example 19.7. Using an archive as a file tree
// Create a ZIP file tree using path FileTree zip = zipTree('someFile.zip') // Create a TAR file tree using path FileTree tar = tarTree('someFile.tar') //tar tree attempts to guess the compression based on the file extension //however if you must specify the compression explicitly you can: FileTree someTar = tarTree(resources.gzip('someTar.ext'))
Many objects in Gradle have properties which accept a set of input files. For example, the
JavaCompile task has a
source property,
which defines the source files to compile. You can set the value of this property using any of the types
supported by the files() method, which was shown above.
This means you can set the property using, for example, a
FileCollection or even a closure.
Here are some examples:
Usually, there is a method with the same name as the property, which appends to the set of files. Again, this method accepts any of the types supported by the files() method.
Example 19.8. Specifying a set of files
task compile(type: JavaCompile) // Use a File object to specify the source directory compile { source = file('src/main/java') } // Use a String path to specify the source directory compile { source = 'src/main/java' } // Use a collection to specify multiple source directories compile { source = ['src/main/java', '../shared/java'] } // Use a FileCollection (or FileTree in this case) to specify the source files compile { source = fileTree(dir: 'src/main/java').matching { include 'org/gradle/api/**' } } // Using a closure to specify the source files. compile { source = { // Use the contents of each zip file in the src dir file('src').listFiles().findAll {it.name.endsWith('.zip')}.collect { zipTree(it) } } }
compile { // Add some source directories use String paths source 'src/main/java', 'src/main/groovy' // Add a source directory using a File object source file('../shared/java') // Add some source directories using a closure source { file('src/test/').listFiles() } }
You can use the
Copy task to copy files. The copy task is very flexible, and allows
you to, for example, filter the contents of the files as they are copied, and map to the file names.
To use the
Copy task, you must provide a set of source files to copy, and a destination directory to copy
the files to. You may also specify how to transform the files as they are copied. You do all this using a
copy spec. A copy spec is represented by the
CopySpec interface. The
Copy task implements this interface.
You specify the source files using the
method. To specify the destination directory, use the
Example 19.9. Copying files using the copy task
task copyTask(type: Copy) { from 'src/main/webapp' into 'build/explodedWar' }
from() method accepts any of the arguments that the
files() method does. When an argument resolves to a directory,
everything under that directory (but not the directory itself) is recursively copied into the destination
directory. When an argument resolves to a file, that file is copied into the destination directory.
When an argument resolves to a non-existing file, that argument is ignored. If the argument is
a task, the output files (i.e. the files the task creates) of the task are copied and the task is automatically
added as a dependency of the
Copy task.
into() accepts
any of the arguments that the file() method does. Here is another
Example 19.10. Specifying copy task source files and destination directory
task anotherCopyTask(type: Copy) { // Copy everything under src/main/webapp from 'src/main/webapp' // Copy a single file from 'src/staging/index.html' // Copy the output of a task from copyTask // Copy the output of a task using Task outputs explicitly. from copyTaskWithPatterns.outputs // Copy the contents of a Zip file from zipTree('src/main/assets.zip') // Determine the destination directory later into { getDestDir() } }
You can select the files to copy using Ant-style include or exclude patterns, or using a closure:
Example 19.11. Selecting the files to copy
task copyTaskWithPatterns(type: Copy) { from 'src/main/webapp' into 'build/explodedWar' include '**/*.html' include '**/*.jsp' exclude { details -> details.file.name.endsWith('.html') && details.file.text.contains('staging') } }
You can also use the
Project.copy(org.gradle.api.Action) method to copy files. It works the
same way as the task with some major limitations though. First, the
copy() is not incremental
(see Section 18.9, "Up-to-date checks (AKA Incremental Build)").
Example 19.12. Copying files using the copy() method without up-to-date check
task copyMethod { doLast { copy { from 'src/main/webapp' into 'build/explodedWar' include '**/*.html' include '**/*.jsp' } } }
Secondly, the
copy() method can not honor task dependencies when a task is used as a copy source
(i.e. as an argument to
from()) because it's a method and not a task.
As such, if you are using the
copy() method as part of a task action, you must explicitly
declare all inputs and outputs in order to get the correct behavior.
Example 19.13. Copying files using the copy() method with up-to-date check
task copyMethodWithExplicitDependencies{ // up-to-date check for inputs, plus add copyTask as dependency inputs.file copyTask outputs.dir 'some-dir' // up-to-date check for outputs doLast{ copy { // Copy the output of copyTask from copyTask into 'some-dir' } } }
It is preferable to use the
Copy task wherever possible, as it supports incremental building and task dependency inference
without any extra effort on your part. The
copy() method can be used to copy files as part of a task's
implementation. That is, the copy method is intended to be used by custom tasks (see Chapter 39, Writing Custom Task Classes) that need to copy files
as part of their function. In such a scenario, the custom task should sufficiently declare the inputs/outputs relevant to the copy action.
Example 19.14. Renaming files as they are copied
task rename(type: Copy) { from 'src/main/webapp' into 'build/explodedWar' // Use a closure to map the file name rename { String fileName -> fileName.replace('-staging-', '') } // Use a regular expression to map the file name rename '(.+)-staging-(.+)', '$1$2' rename(/(.+)-staging-(.+)/, '$1$2') }
Example 19.15. Filtering files as they are copied
import org.apache.tools.ant.filters.FixCrLfFilter import org.apache.tools.ant.filters.ReplaceTokens task filter(type: Copy) { from 'src/main/webapp' into 'build/explodedWar' // Substitute property tokens in files expand(copyright: '2009', version: '2.3.1') expand(project.properties) // Use some of the filters provided by Ant filter(FixCrLfFilter) filter(ReplaceTokens, tokens: [copyright: '2009', version: '2.3.1']) // Use a closure to filter each line filter { String line -> "[$line]" } // Use a closure to remove lines filter { String line -> line.startsWith('-') ? null : line } filteringCharset = 'UTF-8' }
When you use the
ReplaceTokens class with the "filter" operation, the result is a template engine that replaces tokens of the form "@tokenName@" (the Apache Ant-style token) with a set of given values. The "expand" operation does the same thing except it treats the source files as
Groovy templates
in which tokens take the form "${tokenName}". Be aware that you may need to escape parts of your source files when
using this option, for example if it contains literal "$" or "<%" strings.
It's a good practice to specify the charset when reading and writing the file, using the
property. If not specified, the JVM default charset is used, which might not match with the actual charset of the files
to filter, and might be different from one machine to another.
Copy specs form a hierarchy. A copy spec inherits its destination path, include patterns, exclude patterns, copy actions, name mappings and filters.
Example 19.16. Nested copy specs
task nestedSpecs(type: Copy) { into 'build/explodedWar' exclude '**/*staging*' from('src/dist') { include '**/*.html' } into('libs') { from configurations.runtime } }
Sync task extends the
Copy task. When it
executes, it copies the source files into the destination directory, and then removes any files from the
destination directory which it did not copy. This can be useful for doing things such as installing your
application, creating an exploded copy of your archives, or maintaining a copy of the project's dependencies.
Here is an example which maintains a copy of the project's runtime dependencies in the
Example 19.17. Using the Sync task to copy dependencies
task libs(type: Sync) {
from configurations.runtime
into "$buildDir/libs"
A project can have as many JAR archives as you want. You can also add WAR, ZIP and TAR archives to your project.
Archives are created using the various archive tasks:
War, and
They all work the same way, so let's look at how you create a ZIP file.
Example 19.18. Creating a ZIP archive
apply plugin: 'java' task zip(type: Zip) { from 'src/dist' into('libs') { from configurations.runtime } }
The Java plugin adds a number of default values for the archive tasks. You can use the archive tasks without using the Java plugin, if you like. You will need to provide values for some additional properties.
The archive tasks all work exactly the same way as the
Copy task, and implement the same
CopySpec interface. As with the
Copy task, you specify the input
files using the
from() method, and can optionally specify where they end up in the
archive using the
into() method. You can filter the contents of file, rename files, and
all the other things you can do with a copy spec.
The format of
is used for generated archive file names.
For example:
Example 19.19. Creation of ZIP archive
apply plugin: 'java' version = 1.0 task myZip(type: Zip) { from 'somedir' } println myZip.archiveName println relativePath(myZip.destinationDir) println relativePath(myZip.archivePath)
Output of
gradle -q myZip
> gradle -q myZip zipProject-1.0.zip build/distributions build/distributions/zipProject-1.0.zip
This adds a
Zip archive task with the name
myZip which produces
ZIP file
zipProject-1.0.zip. It is important to distinguish between the name of the archive task
and the name of the archive generated by the archive task. The default name for archives can be
changed with the
archivesBaseName project property. The name of the archive can also be
changed at any time later on.
There are a number of properties which you can set on an archive task. These are listed below in Table 19.1, "Archive tasks - naming properties". You can, for example, change the name of the archive:
Example 19.20. Configuration of archive task - custom archive name
apply plugin: 'java' version = 1.0 task myZip(type: Zip) { from 'somedir' baseName = 'customName' } println myZip.archiveName
Output of
gradle -q myZip
> gradle -q myZip customName-1.0.zip
You can further customize the archive names:
Example 19.21. Configuration of archive task - appendix & classifier
apply plugin: 'java' archivesBaseName = 'gradle' version = 1.0 task myZip(type: Zip) { appendix = 'wrapper' classifier = 'src' from 'somedir' } println myZip.archiveName
Output of
gradle -q myZip
> gradle -q myZip gradle-wrapper-1.0-src.zip
Table 19.1. Archive tasks - naming properties
|Property name
|Default value
If any of these properties is empty the trailing
|The base file name of the generated archive
|The absolute path of the generated archive.
|Depends on the archive type. JARs and WARs go into
ZIPs and TARs go into
|The directory to generate the archive into
|The base name portion of the archive file name.
|The appendix portion of the archive file name.
|The version portion of the archive file name.
|The classifier portion of the archive file name,
|Depends on the archive type, and for TAR files, the compression type as well:
tgz or
|The extension of the archive file name.
You can use the
Project.copySpec(org.gradle.api.Action) method to share content between archives.
Often you will want to publish an archive, so that it is usable from another project. This process is described in Chapter 31, Publishing artifacts
Gradle provides excellent integration with Ant. You can use individual Ant tasks or entire Ant builds in your Gradle builds. In fact, you will find that it's far easier and more powerful using Ant tasks in a Gradle build script, than it is to use Ant's XML format. You could even use Gradle simply as a powerful Ant task scripting tool.
Ant can be divided into two layers. The first layer is the Ant language. It provides the syntax for the
build.xml file, the handling of the targets, special constructs like macrodefs, and so on.
In other words, everything except the Ant tasks and types. Gradle understands this language, and allows you to
import your Ant
build.xml directly into a Gradle project. You can then use the targets of
your Ant build as if they were Gradle tasks.
The second layer of Ant is its wealth of Ant tasks and types, like
copy or
jar. For this layer Gradle provides integration
simply by relying on Groovy, and the fantastic
Finally, since build scripts are Groovy scripts, you can always execute an Ant build as an external process.
Your build script may contain statements like:
"ant clean compile".execute().
You can use Gradle's Ant integration as a path for migrating your build from Ant to Gradle. For example, you could start by importing your existing Ant build. Then you could move your dependency declarations from the Ant script to your build file. Finally, you could move your tasks across to your build file, or replace them with some of Gradle's plugins. This process can be done in parts over time, and you can have a working Gradle build during the entire process.
In your build script, a property called
ant is provided by Gradle. This is a reference
to an
AntBuilder instance. This
AntBuilder is used to
access Ant tasks, types and properties from your build script. There is a very simple mapping from Ant's
build.xml format to Groovy, which is explained below.
You execute an Ant task by calling a method on the
AntBuilder instance. You use the task
name as the method name. For example, you execute the Ant
echo task by calling the
ant.echo() method. The attributes of the Ant task are passed as Map parameters to the
method. Below is an example of the
echo task. Notice that we can also mix
Groovy code and the Ant task markup. This can be extremely powerful.
Example 20.1. Using an Ant task
task hello {
doLast {
String greeting = 'hello from Ant'
ant.echo(message: greeting)
Output of
gradle hello
> gradle hello :hello [ant:echo] hello from Ant BUILD SUCCESSFUL Total time: 1 secs
You pass nested text to an Ant task by passing it as a parameter of the task method call. In this example, we
pass the message for the
echo task as nested text:
Example 20.2. Passing nested text to an Ant task
task hello {
doLast {
ant.echo('hello from Ant')
Output of
gradle hello
> gradle hello :hello [ant:echo] hello from Ant BUILD SUCCESSFUL Total time: 1 secs
You pass nested elements to an Ant task inside a closure. Nested elements are defined in the same way as tasks, by calling a method with the same name as the element we want to define.
Example 20.3. Passing nested elements to an Ant task
task zip { doLast { ant.zip(destfile: 'archive.zip') { fileset(dir: 'src') { include(name: '**.xml') exclude(name: '**.java') } } } }
You can access Ant types in the same way that you access tasks, using the name of the type as the
method name. The method call returns the Ant data type, which you can then use directly in your build script. In the
following example, we create an Ant
path object, then iterate over the contents of it.
Example 20.4. Using an Ant type
task list { doLast { def path = ant.path { fileset(dir: 'libs', includes: '*.jar') } path.list().each { println it } } }
More information about
AntBuilder can be found in 'Groovy in Action' 8.4 or at the
Groovy Wiki
To make custom tasks available in your build,
you can use the
taskdef (usually easier) or
typedef Ant task,
just as you would in a
build.xml file. You can then refer to the custom Ant task as you
would a built-in Ant task.
Example 20.5. Using a custom Ant task
task check { doLast { ant.taskdef(resource: 'checkstyletask.properties') { classpath { fileset(dir: 'libs', includes: '*.jar') } } ant.checkstyle(config: 'checkstyle.xml') { fileset(dir: 'src') } } }
You can use Gradle's dependency management to assemble the classpath to use for the custom tasks. To do this, you need to define a custom configuration for the classpath, then add some dependencies to the configuration. This is described in more detail in Section 24.4, "How to declare your dependencies".
Example 20.6. Declaring the classpath for a custom Ant task
configurations { pmd } dependencies { pmd group: 'pmd', name: 'pmd', version: '4.2.5' }
To use the classpath configuration, use the
asPath property of the custom configuration.
Example 20.7. Using a custom Ant task and dependency management together
task check { doLast { ant.taskdef(name: 'pmd', classname: 'net.sourceforge.pmd.ant.PMDTask', classpath: configurations.pmd.asPath) ant.pmd(shortFilenames: 'true', failonruleviolation: 'true', rulesetfiles: file('pmd-rules.xml').toURI().toString()) { formatter(type: 'text', toConsole: 'true') fileset(dir: 'src') } } }
You can use the
ant.importBuild() method to import an Ant build into your Gradle
project. When you import an Ant build, each Ant target is treated as a Gradle task. This means you can
manipulate and execute the Ant targets in exactly the same way as Gradle tasks.
Example 20.8. Importing an Ant build
ant.importBuild 'build.xml'
<project> <target name="hello"> <echo>Hello, from Ant</echo> </target> </project>
Output of
gradle hello
> gradle hello :hello [ant:echo] Hello, from Ant BUILD SUCCESSFUL Total time: 1 secs
You can add a task which depends on an Ant target:
Example 20.9. Task that depends on Ant target
ant.importBuild 'build.xml' task intro(dependsOn: hello) { doLast { println 'Hello, from Gradle' } }
Output of
gradle intro
> gradle intro :hello [ant:echo] Hello, from Ant :intro Hello, from Gradle BUILD SUCCESSFUL Total time: 1 secs
Or, you can add behaviour to an Ant target:
Example 20.10. Adding behaviour to an Ant target
ant.importBuild 'build.xml' hello { doLast { println 'Hello, from Gradle' } }
Output of
gradle hello
> gradle hello :hello [ant:echo] Hello, from Ant Hello, from Gradle BUILD SUCCESSFUL Total time: 1 secs
It is also possible for an Ant target to depend on a Gradle task:
Example 20.11. Ant target that depends on Gradle task
ant.importBuild 'build.xml' task intro { doLast { println 'Hello, from Gradle' } }
<project> <target name="hello" depends="intro"> <echo>Hello, from Ant</echo> </target> </project>
Output of
gradle hello
> gradle hello :intro Hello, from Gradle :hello [ant:echo] Hello, from Ant BUILD SUCCESSFUL Total time: 1 secs
Sometimes it may be necessary to "rename" the task generated for an Ant target to avoid a naming collision with existing Gradle tasks.
To do this, use the
AntBuilder.importBuild(java.lang.Object, org.gradle.api.Transformer) method.
Example 20.12. Renaming imported Ant targets
ant.importBuild('build.xml') { antTargetName -> 'a-' + antTargetName }
<project> <target name="hello"> <echo>Hello, from Ant</echo> </target> </project>
Output of
gradle a-hello
> gradle a-hello :a-hello [ant:echo] Hello, from Ant BUILD SUCCESSFUL Total time: 1 secs
Note that while the second argument to this method should be a
Transformer, when programming in Groovy we can simply
use a closure instead of an anonymous inner class (or similar) due to Groovy's support for automatically coercing closures to single-abstract-method types.
There are several ways to set an Ant property, so that the property can be used by Ant tasks. You can
set the property directly on the
AntBuilder instance. The Ant properties are also
available as a Map which you can change. You can also use the Ant
property task.
Below are some examples of how to do this.
Example 20.13. Setting an Ant property
ant.buildDir = buildDir ant.properties.buildDir = buildDir ant.properties['buildDir'] = buildDir ant.property(name: 'buildDir', location: buildDir)
<echo>buildDir = ${buildDir}</echo>
Many Ant tasks set properties when they execute. There are several ways to get the value of these properties.
You can get the property directly from the
AntBuilder instance. The Ant properties are
also available as a Map. Below are some examples.
Example 20.14. Getting an Ant property
<property name="antProp" value="a property defined in an Ant build"/>
println ant.antProp
println ant.properties.antProp
println ant.properties['antProp']
There are several ways to set an Ant reference:
Example 20.15. Setting an Ant reference
ant.path(id: 'classpath', location: 'libs') ant.references.classpath = ant.path(location: 'libs') ant.references['classpath'] = ant.path(location: 'libs')
<path refid="classpath"/>
There are several ways to get an Ant reference:
Example 20.16. Getting an Ant reference
<path id="antPath" location="libs"/>
println ant.references.antPath
println ant.references['antPath']
Gradle maps Ant message priorities to Gradle log levels so that messages logged from Ant appear in the Gradle output. By default, these are mapped as follows:
Table 20.1. Ant message priority mapping
|Ant Message Priority
|Gradle Log Level
The default mapping of Ant message priority to Gradle log level can sometimes be problematic. For example,
there is no message priority that maps directly to the
LIFECYCLE log level, which is the
default for Gradle. Many Ant tasks log messages at the INFO priority, which means to expose
those messages from Gradle, a build would have to be run with the log level set to
potentially logging much more output than is desired.
Conversely, if an Ant task logs messages at too high of a level, to suppress those messages would require the
build to be run at a higher log level, such as
QUIET. However, this could result in other,
desirable output being suppressed.
To help with this, Gradle allows the user to fine tune the Ant logging and control the mapping of message priority
to Gradle log level. This is done by setting the priority that should map to the default Gradle
log level using the
AntBuilder.setLifecycleLogLevel(java.lang.String) method. When this value
is set, any Ant message logged at the configured priority or above will be logged at least at
Any Ant message logged below this priority will be logged at most at
For example, the following changes the mapping such that Ant INFO priority messages are exposed at the
log level.
Example 20.17. Fine tuning Ant logging
ant.lifecycleLogLevel = "INFO" task hello { doLast { ant.echo(level: "info", message: "hello from info priority!") } }
Output of
gradle hello
> gradle hello :hello [ant:echo] hello from info priority! BUILD SUCCESSFUL Total time: 1 secs
On the other hand, if the
lifecycleLogLevel was set to ERROR, Ant messages logged
at the WARN priority would no longer be logged at the
WARN log level. They would now
be logged at the
INFO level and would be suppressed by default.
The Ant integration is provided by
[9] In Groovy you can execute Strings. To learn more about executing external processes with Groovy have a look in 'Groovy in Action' 9.3.2 or at the Groovy wiki
We said earlier that the core of Gradle is a language for dependency based programming. In Gradle terms this means that you can define tasks and dependencies between tasks. Gradle guarantees that these tasks are executed in the order of their dependencies, and that each task is executed only once. These tasks form a Directed Acyclic Graph. There are build tools that build up such a dependency graph as they execute their tasks. Gradle builds the complete dependency graph before any task is executed. This lies at the heart of Gradle and makes many things possible which would not be possible otherwise.
Your build scripts configure this dependency graph. Therefore they are strictly speaking build configuration scripts.
A Gradle build has three distinct phases.
Gradle supports single and multi-project builds. During the initialization phase, Gradle
determines which projects are going to take part in the build, and creates a
Project instance for each of these projects.
During this phase the project objects are configured. The build scripts of all projects which are part of the build are executed. Gradle 1.4 introduced an incubating opt-in feature called configuration on demand. In this mode, Gradle configures only relevant projects (see the section called "Configuration on demand").
Gradle determines the subset of the tasks, created and configured during the configuration phase, to be executed. The subset is determined by the task name arguments passed to the gradle command and the current directory. Gradle then executes each of the selected tasks.
Beside the build script files, Gradle defines a settings file. The settings file is determined by Gradle
via a naming convention. The default name for this file is
settings.gradle. Later in
this chapter we explain how Gradle looks for a settings file.
The settings file is executed during the initialization phase. A multiproject build must have a
file in the root project of the multiproject hierarchy. It is required because the
settings file defines which projects are taking part in the multi-project build (see
Chapter 25, Multi-project Builds). For a single-project build, a settings file is optional.
Besides defining the included projects, you might need it to add libraries to your build script classpath (see Chapter 42, Organizing Build Logic). Let's first do some introspection with a single project
Example 21.1. Single project build
println 'This is executed during the initialization phase.'
println 'This is executed during the configuration phase.' task configured { println 'This is also executed during the configuration phase.' } task test { doLast { println 'This is executed during the execution phase.' } } task testBoth { doFirst { println 'This is executed first during the execution phase.' } doLast { println 'This is executed last during the execution phase.' } println 'This is executed during the configuration phase as well.' }
Output of
gradle test testBoth
> gradle test testBoth This is executed during the initialization phase. This is executed during the configuration phase. This is also executed during the configuration phase. This is executed during the configuration phase as well. :test This is executed during the execution phase. :testBoth This is executed first during the execution phase. This is executed last during the execution phase. BUILD SUCCESSFUL Total time: 1 secs
For a build script, the property access and method calls are delegated to a project object. Similarly
property access and method calls within the settings file is delegated to a settings object. Look at
Settings class in the API documentation for more information.
A multi-project build is a build where you build more than one project during a single execution of Gradle. You have to declare the projects taking part in the multiproject build in the settings file. There is much more to say about multi-project builds in the chapter dedicated to this topic (see Chapter 25, Multi-project Builds).
Multi-project builds are always represented by a tree with a single root. Each element in the tree
represents a project. A project has a path which denotes the position
of the project in the multi-project build tree. In most cases the project path is consistent with
the physical location of the project in the file system. However, this behavior is configurable.
The project tree is created in the
settings.gradle file.
By default it is assumed that the location of the settings
file is also the location of the root project. But you can redefine the location of the root project
in the settings file.
In the settings file you can use a set of methods to build the project tree. Hierarchical and flat physical layouts get special support.
Example 21.2. Hierarchical layout
include 'project1', 'project2:child', 'project3:child1'
include method takes project paths as arguments.
The project path is assumed to be equal to the relative physical file system path.
For example, a path 'services:api' is mapped by default to a folder 'services/api'
(relative from the project root). You only need to specify the leaves of the tree.
This means that the inclusion of the path 'services:hotels:api' will result in creating 3 projects:
'services', 'services:hotels' and 'services:hotels:api'.
includeFlat method takes directory names as an argument. These directories
need to exist as siblings of the root project directory. The location of these directories
are considered as child projects of the root project in the multi-project tree.
The multi-project tree created in the settings file is made up of so called project descriptors. You can modify these descriptors in the settings file at any time. To access a descriptor you can do:
Using this descriptor you can change the name, project directory and build file of a project.
Example 21.4. Modification of elements of the project tree
println rootProject.name
println project(':projectA').name
rootProject.name = 'main' project(':projectA').projectDir = new File(settingsDir, '../my-project-a') project(':projectA').buildFileName = 'projectA.gradle'
Look at the
ProjectDescriptor class in the API documentation for more information.
How does Gradle know whether to do a single or multiproject build? If you trigger a multiproject build
from a directory with a settings file, things are easy. But Gradle also allows you to execute the
build from within any subproject taking part in the build.
If you execute Gradle from within a project with no
settings.gradle file,
Gradle looks for a
settings.gradle file in the following way:
It looks in a directory called
master which has the same nesting level
as the current dir.
If not found yet, it searches parent directories.
If not found yet, the build is executed as a single project build.
If a
settings.gradle file is found, Gradle checks if the current project is part of the
multiproject hierarchy defined in the found
settings.gradle file. If not, the build is executed as a
single project build. Otherwise a multiproject build is executed.
What is the purpose of this behavior? Gradle needs to determine whether the project you are in is
a subproject of a multiproject build or not. Of course, if it is a subproject, only the subproject and its
dependent projects are built, but Gradle needs to create the build configuration for the whole multiproject
build (see Chapter 25, Multi-project Builds). You can use the
command line option to tell Gradle not to look in the parent hierarchy for a
settings.gradle file. The
current project is then always built as a single project build. If the current project contains a
settings.gradle file, the
-u option has no meaning. Such a build is always executed as:
a single project build, if the
settings.gradle file does not define a multiproject hierarchy
a multiproject build, if the
settings.gradle file does define a multiproject hierarchy.
The automatic search for a
settings.gradle file only works for multi-project
builds with a physical hierarchical or flat layout. For a flat layout you must additionally follow the
naming convention described above ("
master"). Gradle supports arbitrary physical layouts
for a multiproject build, but for such arbitrary layouts you need to execute the build from the directory
where the settings file is located. For information on how to run partial builds from the root see
Section 25.4, "Running tasks by their absolute path".
Gradle creates a Project object for every project taking part in the build. For a multi-project build these are the projects specified in the Settings object (plus the root project). Each project object has by default a name equal to the name of its top level directory, and every project except the root project has a parent project. Any project may have child projects.
For a single project build, the workflow of the after initialization phases are pretty simple. The build script is executed against the project object that was created during the initialization phase. Then Gradle looks for tasks with names equal to those passed as command line arguments. If these task names exist, they are executed as a separate build in the order you have passed them. The configuration and execution for multi-project builds is discussed in Chapter 25, Multi-project Builds.
Your build script can receive notifications as the build progresses through its lifecycle. These notifications generally take two forms: You can either implement a particular listener interface, or you can provide a closure to execute when the notification is fired. The examples below use closures. For details on how to use the listener interfaces, refer to the API documentation.
You can receive a notification immediately before and after a project is evaluated. This can be used to do things like performing additional configuration once all the definitions in a build script have been applied, or for some custom logging or profiling.
Below is an example which adds a
test task to each project which has a
hasTests property value of true.
Example 21.5. Adding of test task to each project which has certain property set
allprojects { afterEvaluate { project -> if (project.hasTests) { println "Adding test task to $project" project.task('test') { doLast { println "Running tests for $project" } } } } }
hasTests = true
Output of
gradle -q test
> gradle -q test Adding test task to project ':projectA' Running tests for project ':projectA'
This example uses method
Project.afterEvaluate() to add a closure which is executed
after the project is evaluated.
It is also possible to receive notifications when any project is evaluated. This example performs
some custom logging of project evaluation. Notice that the
afterProject notification
is received regardless of whether the project evaluates successfully or fails with an exception.
Example 21.6. Notifications
gradle.afterProject {project, projectState -> if (projectState.failure) { println "Evaluation of $project FAILED" } else { println "Evaluation of $project succeeded" } }
Output of
gradle -q test
> gradle -q test Evaluation of root project 'buildProjectEvaluateEvents' succeeded Evaluation of project ':projectA' succeeded Evaluation of project ':projectB' FAILED
You can also add a
ProjectEvaluationListener to the
Gradle to receive these events.
You can receive a notification immediately after a task is added to a project. This can be used to set some default values or add behaviour before the task is made available in the build file.
The following example sets the
srcDir property of each task as it is created.
Example 21.7. Setting of certain property to all tasks
tasks.whenTaskAdded { task -> task.ext.srcDir = 'src/main/java' } task a println "source dir is $a.srcDir"
Output of
gradle -q a
> gradle -q a source dir is src/main/java
You can also add an
Action to a
TaskContainer to receive these events.
You can receive a notification immediately after the task execution graph has been populated. We have seen this already in Section 15.13, "Configure by DAG".
You can also add a
TaskExecutionGraphListener to the
TaskExecutionGraph to receive these events.
You can receive a notification immediately before and after any task is executed.
The following example logs the start and end of each task execution. Notice that the
afterTask notification is received regardless of whether the task completes
successfully or fails with an exception.
Example 21.8. Logging of start and end of each task execution
task ok task broken(dependsOn: ok) { doLast { throw new RuntimeException('broken') } } gradle.taskGraph.beforeTask { Task task -> println "executing $task ..." } gradle.taskGraph.afterTask { Task task, TaskState state -> if (state.failure) { println "FAILED" } else { println "done" } }
Output of
gradle -q broken
> gradle -q broken executing task ':ok' ... done executing task ':broken' ... FAILED
You can also use a
TaskExecutionListener to the
TaskExecutionGraph to receive these events.
[10] Gradle supports partial multiproject builds (see Chapter 25, Multi-project Builds).
The wrapper plugin is currently incubating. Please be aware that the DSL and other configuration may change in later Gradle versions.
The Gradle wrapper plugin allows the generation of Gradle wrapper files by adding a
task, that generates all files needed to run the build using the Gradle Wrapper.
Details about the Gradle Wrapper can be found in Chapter 5, The Gradle Wrapper.
Without modifying the
build.gradle file, the wrapper plugin can be auto-applied to the root project of the current build by running
gradle wrapper" from the command line. This applies the plugin if no task named
wrapper is already defined in the build.
The wrapper plugin adds the following tasks to the project:
Table 22.1. Wrapper plugin - tasks
|Task name
|Depends on
|Generates Gradle wrapper files.
The log is the main 'UI' of a build tool. If it is too verbose, real warnings and problems are easily hidden by this. On the other hand you need relevant information for figuring out if things have gone wrong. Gradle defines 6 log levels, as shown in Table 23.1, "Log levels". There are two Gradle-specific log levels, in addition to the ones you might normally see. Those levels are QUIET and LIFECYCLE. The latter is the default, and is used to report build progress.
Table 23.1. Log levels
|Used for
|Error messages
|Important information messages
|Warning messages
|Progress information messages
|Information messages
|Debug messages
You can use the command line switches shown in Table 23.2, "Log level command-line options" to choose different log levels. In Table 23.3, "Stacktrace command-line options" you find the command line switches which affect stacktrace logging.
Table 23.2. Log level command-line options
|Outputs Log Levels
|no logging options
|LIFECYCLE and higher
-q or
|QUIET and higher
-i or
|INFO and higher
-d or
|DEBUG and higher (that is, all log messages)
Table 23.3. Stacktrace command-line options
|No stacktrace options
|No stacktraces are printed to the console in case of a build error (e.g. a compile error). Only in
case of internal exceptions will stacktraces be printed. If the
DEBUG log level
is chosen, truncated stacktraces are always printed.
-s or
|Truncated stacktraces are printed. We recommend this over full stacktraces. Groovy full stacktraces are extremely verbose (Due to the underlying dynamic invocation mechanisms. Yet they usually do not contain relevant information for what has gone wrong in your code.) This option renders stacktraces for deprecation warnings.
-S or
|The full stacktraces are printed out. This option renders stacktraces for deprecation warnings.
A simple option for logging in your build file is to write messages to standard output. Gradle redirects
anything written to standard output to it's logging system at the
QUIET log level.
Example 23.1. Using stdout to write log messages
println 'A message which is logged at QUIET level'
Gradle also provides a
logger property to a build script, which is an instance of
Logger. This interface extends the SLF4J
Logger interface and adds a few Gradle specific methods to it. Below is an example
of how this is used in the build script:
Example 23.2. Writing your own log messages
logger.quiet('An info log message which is always logged.') logger.error('An error log message.') logger.warn('A warning log message.') logger.lifecycle('A lifecycle info log message.') logger.info('An info log message.') logger.debug('A debug log message.') logger.trace('A trace log message.')
You can also hook into Gradle's logging system from within other classes used in the build (classes from
buildSrc directory for example). Simply use an SLF4J logger. You can use this
logger the same way as you use the provided logger in the build script.
Example 23.3. Using SLF4J to write log messages
import org.slf4j.Logger import org.slf4j.LoggerFactory Logger slf4jLogger = LoggerFactory.getLogger('some-logger') slf4jLogger.info('An info log message logged using SLF4j')
Internally, Gradle uses Ant and Ivy. Both have their own logging system. Gradle redirects their logging
output into the Gradle logging system. There is a 1:1 mapping from the Ant/Ivy log levels to the Gradle log
levels, except the Ant/Ivy
TRACE log level, which is mapped to Gradle
DEBUG log level. This means the default Gradle log level will not show any Ant/Ivy output
unless it is an error or a warning.
There are many tools out there which still use standard output for logging. By default, Gradle redirects
standard output to the
QUIET log level and standard error to the
level. This behavior is configurable. The project object provides a
LoggingManager, which allows you to change the log levels that
standard out or error are redirected to when your build script is evaluated.
Example 23.4. Configuring standard output capture
logging.captureStandardOutput LogLevel.INFO
println 'A message which is logged at INFO level'
To change the log level for standard out or error during task execution, tasks also provide a
Example 23.5. Configuring standard output capture for a task
task logInfo {
logging.captureStandardOutput LogLevel.INFO
doFirst {
println 'A task message which is logged at INFO level'
Gradle also provides integration with the Java Util Logging, Jakarta Commons Logging and Log4j logging toolkits. Any log messages which your build classes write using these logging toolkits will be redirected to Gradle's logging system.
You can replace much of Gradle's logging UI with your own. You might do this, for example, if you want to
customize the UI in some way - to log more or less information, or to change the formatting. You replace
the logging using the
Gradle.useLogger(java.lang.Object) method. This
is accessible from a build script, or an init script, or via the embedding API.
Note that this completely disables Gradle's default output.
Below is an example init script which changes how task execution and build completion is logged.
Example 23.6. Customizing what Gradle logs
useLogger(new CustomEventLogger()) class CustomEventLogger extends BuildAdapter implements TaskExecutionListener { public void beforeExecute(Task task) { println "[$task.name]" } public void afterExecute(Task task, TaskState state) { println() } public void buildFinished(BuildResult result) { println 'build completed' if (result.failure != null) { result.failure.printStackTrace() } } }
Output of
gradle -I init.gradle build
> gradle -I init.gradle build [compile] compiling source [testCompile] compiling test source [test] running unit tests [build] build completed
Your logger can implement any of the listener interfaces listed below. When you register a logger, only the logging for the interfaces that it implements is replaced. Logging for the other interfaces is left untouched. You can find out more about the listener interfaces in Section 21.6, "Responding to the lifecycle in the build script".
Dependency management is a critical feature of every build, and Gradle has placed an emphasis on offering first-class dependency management that is both easy to understand and compatible with a wide variety of approaches. If you are familiar with the approach used by either Maven or Ivy you will be delighted to learn that Gradle is fully compatible with both approaches in addition to being flexible enough to support fully-customized approaches.
Here are the major highlights of Gradle's support for dependency management:
Transitive dependency management: Gradle gives you full control of your project's dependency tree.
Support for non-managed dependencies: If your dependencies are simply files in version control or a shared drive, Gradle provides powerful functionality to support this.
Support for custom dependency definitions.: Gradle's Module Dependencies give you the ability to describe the dependency hierarchy in the build script.
A fully customizable approach to Dependency Resolution: Gradle provides you with the ability to customize resolution rules making dependency substitution easy.
Full Compatibility with Maven and Ivy: If you have defined dependencies in a Maven POM or an Ivy file, Gradle provides seamless integration with a range of popular build tools.
Integration with existing dependency management infrastructure: Gradle is compatible with both Maven and Ivy repositories. If you use Archiva, Nexus, or Artifactory, Gradle is 100% compatible with all repository formats.
With hundreds of thousands of interdependent open source components each with a range of versions and incompatibilities, dependency management has a habit of causing problems as builds grow in complexity. When a build's dependency tree becomes unwieldy, your build tool shouldn't force you to adopt a single, inflexible approach to dependency management. A proper build system has to be designed to be flexible, and Gradle can handle any situation.
Dependency management can be particularly challenging during a migration from one build system to another. If you are migrating from a tool like Ant or Maven to Gradle, you may be faced with some difficult situations. For example, one common pattern is an Ant project with version-less jar files stored in the filesystem. Other build systems require a wholesale replacement of this approach before migrating. With Gradle, you can adapt your new build to any existing source of dependencies or dependency metadata. This makes incremental migration to Gradle much easier than the alternative. On most large projects, build migrations and any change to development process is incremental because most organizations can't afford to stop everything and migrate to a build tool's idea of dependency management.
Even if your project is using a custom dependency management system or something like an Eclipse .classpath file as master data for dependency management, it is very easy to write a Gradle plugin to use this data in Gradle. For migration purposes this is a common technique with Gradle. (But, once you've migrated, it might be a good idea to move away from a .classpath file and use Gradle's dependency management features directly.)
It is ironic that in a language known for its rich library of open source components that Java has no concept of libraries or versions. In Java, there is no standard way to tell the
JVM that you are using version 3.0.5 of Hibernate, and there is no standard way to say that
depends on
bar-2.0.jar. This has led to external solutions often based on build tools. The most popular ones at the moment are Maven and Ivy. While Maven provides a
complete build system, Ivy focuses solely on dependency management.
Both tools rely on descriptor XML files, which contain information about the dependencies of a particular jar. Both also use repositories where the actual jars are placed together with their descriptor files, and both offer resolution for conflicting jar versions in one form or the other. Both have emerged as standards for solving dependency conflicts, and while Gradle originally used Ivy under the hood for its dependency management. Gradle has replaced this direct dependency on Ivy with a native Gradle dependency resolution engine which supports a range of approaches to dependency resolution including both POM and Ivy descriptor files.
While Gradle has strong opinions on dependency management, the tool gives you a choice between two options: follow recommended best practices or support any kind of pattern you can think of. This section outlines the Gradle project's recommended best practices for managing dependencies.
No matter what the language, proper dependency management is important for every project. From a complex enterprise application written in Java depending on hundreds of open source libraries to the simplest Clojure application depending on a handful of libraries, approaches to dependency management vary widely and can depend on the target technology, the method of application deployment, and the nature of the project. Projects bundled as reusable libraries may have different requirements than enterprise applications integrated into much larger systems of software and infrastructure. Despite this wide variation of requirements, the Gradle project recommends that all projects follow this set of core rules:
The version of a library must be part of the filename. While the version of a jar is usually in the Manifest file, it isn't readily apparent when you are inspecting a
project. If someone asks you to look at a collection of 20 jar files, which would you prefer? A collection of files with names like
or a collection of files with names like
spring.jar? If dependencies have file names with version numbers you can quickly identify the versions of
your dependencies.
If versions are unclear you can introduce subtle bugs which are very hard to find. For example there might be a project which uses Hibernate 2.5. Think about a developer who decides to install version 3.0.5 of Hibernate on her machine to fix a critical security bug but forgets to notify others in the team of this change. She may address the security bug successfully, but she also may have introduced subtle bugs into a codebase that was using a now-deprecated feature from Hibernate. Weeks later there is an exception on the integration machine which can't be reproduced on anyone's machine. Multiple developers then spend days on this issue only finally realising that the error would have been easy to uncover if they knew that Hibernate had been upgraded from 2.5 to 3.0.5.
Versions in jar names increase the expressiveness of your project and make them easier to maintain. This practice also reduces the potential for error.
Transitive dependency management is a technique that enables your project to depend on libraries which, in turn, depend on other libraries. This recursive pattern of transitive dependencies results in a tree of dependencies including your project's first-level dependencies, second-level dependencies, and so on. If you don't model your dependencies as a hierarchical tree of first-level and second-level dependencies it is very easy to quickly lose control over an assembled mess of unstructured dependencies. Consider the Gradle project itself, while Gradle only has a few direct, first-level dependencies, when Gradle is compiled it needs more than one hundred dependencies on the classpath. On a far larger scale, Enterprise projects using Spring, Hibernate, and other libraries, alongside hundreds or thousands of internal projects, can result in very large dependency trees.
When these large dependency trees need to change, you'll often have to solve some dependency version conflicts. Say one open source library needs one version of a logging library and a another uses an alternative version. Gradle and other build tools all have the ability to resolve conflicts, but what differentiates Gradle is the control it gives you over transitive dependencies and conflict resolution.
While you could try to manage this problem manually, you will quickly find that this approach doesn't scale. If you want to get rid of a first level dependency you really can't be sure which other jars you should remove. A dependency of a first level dependency might also be a first level dependency itself, or it might be a transitive dependency of yet another first level dependency. If you try to manage transitive dependencies yourself, the end of the story is that your build becomes brittle: no one dares to change your dependencies because the risk of breaking the build is too high. The project classpath becomes a complete mess, and, if a classpath problem arises, hell on earth invites you for a ride.
Gradle offers you different ways to express first-level and transitive dependencies. With Gradle you can mix and match approaches; for example, you could store your jars in an SCM without XML descriptor files and still use transitive dependency management.
Conflicting versions of the same jar should be detected and either resolved or cause an exception. If you don't use transitive dependency management, version conflicts are undetected and the often accidental order of the classpath will determine what version of a dependency will win. On a large project with many developers changing dependencies, successful builds will be few and far between as the order of dependencies may directly affect whether a build succeeds or fails (or whether a bug appears or disappears in production).
If you haven't had to deal with the curse of conflicting versions of jars on a classpath, here is a small anecdote of the fun that awaits you. In a large project with 30 submodules, adding a dependency to a subproject changed the order of a classpath, swapping Spring 2.5 for an older 2.4 version. While the build continued to work, developers were starting to notice all sorts of surprising (and surprisingly awful) bugs in production. Worse yet, this unintentional downgrade of Spring introduced several security vulnerabilities into the system, which now required a full security audit throughout the organization.
In short, version conflicts are bad, and you should manage your transitive dependencies to avoid them. You might also want to learn where conflicting versions are used and consolidate on a particular version of a dependency across your organization. With a good conflict reporting tool like Gradle, that information can be used to communicate with the entire organization and standardize on a single version. If you think version conflicts don't happen to you, think again. It is very common for different first-level dependencies to rely on a range of different overlapping versions for other dependencies, and the JVM doesn't yet offer an easy way to have different versions of the same jar in the classpath (see Section 24.1.2, "Dependency management and Java").
Gradle offers the following conflict resolution strategies:
ResolutionStrategy for details on how to explicitly choose a particular version.
While the strategies introduced above are usually enough to solve most conflicts, Gradle provides more fine-grained mechanisms to resolve version conflicts:
To deal with problems due to version conflicts, reports with dependency graphs are also very helpful. Such reports are another feature of dependency management.
There are many situations when you want to use the latest version of a particular dependency, or the latest in a range of versions. This can be a requirement during development, or
you may be developing a library that is designed to work with a range of dependency versions. You can easily depend on these constantly changing dependencies by using a
dynamic version. A dynamic version can be either a version range (e.g.
2.+) or it can be a placeholder for the latest version available
Alternatively, sometimes the module you request can change over time, even for the same version. An example of this type of changing module
is a Maven
SNAPSHOT module, which always points at the latest artifact published. In other words, a standard Maven snapshot is a module that never stands still
so to speak, it is a "changing module".
The main difference between a dynamic version and a changing module is that when you resolve a dynamic version, you'll get the real, static version as the module name. When you resolve a changing module, the artifacts are named using the version you requested, but the underlying artifacts may change over time.
By default, Gradle caches dynamic versions and changing modules for 24 hours. You can override the default cache modes using command line options. You can change the cache expiry times in your build using the resolution strategy (see Section 24.9.3, "Fine-tuned control over dependency caching").
In Gradle dependencies are grouped into configurations. Configurations have a name, a number of other properties, and they can extend each other. Many Gradle plugins add pre-defined configurations to your project. The Java plugin, for example, adds some configurations to represent the various classpaths it needs. see Section 46.5, "Dependency management" for details. Of course you can add custom configurations on top of that. There are many use cases for custom configurations. This is very handy for example for adding dependencies not needed for building or testing your software (e.g. additional JDBC drivers to be shipped with your distribution).
A project's configurations are managed by a
configurations object. The closure you pass to
the configurations object is applied against its API. To learn more about this API have a look at
To define a configuration:
To access a configuration:
Example 24.2. Accessing a configuration
println configurations.compile.name
println configurations['compile'].name
To configure a configuration:
Example 24.3. Configuration of a configuration
configurations { compile { description = 'compile classpath' transitive = true } runtime { extendsFrom compile } } configurations.compile { description = 'compile classpath' }
There are several different types of dependencies that you can declare:
Table 24.1. Dependency types
|External module dependency
|A dependency on an external module in some repository.
|Project dependency
|A dependency on another project in the same build.
|File dependency
|A dependency on a set of files on the local filesystem.
|Client module dependency
|A dependency on an external module, where the artifacts are located in some repository but the module meta-data is specified by the local build. You use this kind of dependency when you want to override the meta-data for the module.
|Gradle API dependency
|A dependency on the API of the current Gradle version. You use this kind of dependency when you are developing custom Gradle plugins and task types.
|Local Groovy dependency
|A dependency on the Groovy version used by the current Gradle version. You use this kind of dependency when you are developing custom Gradle plugins and task types.
External module dependencies are the most common dependencies. They refer to a module in an external repository.
Example 24.4. Module dependencies
dependencies { runtime group: 'org.springframework', name: 'spring-core', version: '2.5' runtime 'org.springframework:spring-core:2.5', 'org.springframework:spring-aop:2.5' runtime( [group: 'org.springframework', name: 'spring-core', version: '2.5'], [group: 'org.springframework', name: 'spring-aop', version: '2.5'] ) runtime('org.hibernate:hibernate:3.0.5') { transitive = true } runtime group: 'org.hibernate', name: 'hibernate', version: '3.0.5', transitive: true runtime(group: 'org.hibernate', name: 'hibernate', version: '3.0.5') { transitive = true } }
See the
DependencyHandler class in the API documentation
for more examples and a complete reference.
Gradle provides different notations for module dependencies. There is a string notation and
a map notation. A module dependency has an API which allows further configuration. Have a look at
to learn all about the API.
This API provides properties and configuration methods. Via the string notation you can define a subset
of the properties. With the map notation you can define all properties. To have access to the complete API,
either with the map or with the string notation, you can assign a single dependency to a configuration
together with a closure.
If you declare a module dependency, Gradle looks for a module descriptor file (
pom.xml or
ivy.xml) in the repositories. If such a module descriptor file exists, it is parsed and the artifacts of
this module (e.g.
hibernate-3.0.5.jar) as well as its dependencies (e.g. cglib) are downloaded. If no such
module descriptor file exists, Gradle looks for a file called
to retrieve. In Maven, a module can have one and only one artifact. In Gradle and Ivy, a module can have multiple artifacts.
Each artifact can have a different set of dependencies.
ivy.xml) can declare multiple artifacts.
For more information, see the Ivy reference for
In Gradle, when you declare a dependency on an Ivy module, you actually declare a dependency on the
default configuration of that module.
So the actual set of artifacts (typically jars) you depend on is the set of artifacts that are associated with the
default configuration of that module. Here are some situations where this matters:
default configuration of a module contains undesired artifacts. Rather than depending on the
whole configuration, a dependency on just the desired artifacts is declared.
default. That configuration is explicitly named
as part of the dependency declaration.
DependencyHandler class in the API documentation for examples and a complete reference for declaring dependencies.
As said above, if no module descriptor file can be found, Gradle by default
downloads a jar with the name of the module. But sometimes, even if the repository contains module descriptors, you want to download only the artifact jar, without
the dependencies.
And sometimes you want to download a zip from a repository, that does not have module descriptors. Gradle provides an artifact only
notation for those use cases - simply prefix the extension that you want to be downloaded with
'@' sign:
Example 24.5. Artifact only notation
dependencies { runtime "org.groovy:groovy:2.2.0@jar" runtime group: 'org.groovy', name: 'groovy', version: '2.2.0', ext: 'jar' }
An artifact only notation creates a module dependency which downloads only the artifact file with the specified extension. Existing module descriptors are ignored.
The Maven dependency management has the notion of classifiers. [12] Gradle supports this. To retrieve classified dependencies from a Maven repository you can write:
Example 24.6. Dependency with classifier
compile "org.gradle.test.classifiers:service:1.0:jdk15@jar" otherConf group: 'org.gradle.test.classifiers', name: 'service', version: '1.0', classifier: 'jdk14'
As can be seen in the first line above, classifiers can be used together with the artifact only notation.
It is easy to iterate over the dependency artifacts of a configuration:
Example 24.7. Iterating over a configuration
task listJars { doLast { configurations.compile.each { File file -> println file.name } } }
Output of
gradle -q listJars
> gradle -q listJars hibernate-core-3.6.7.Final.jar antlr-2.7.6.jar commons-collections-3.1.jar dom4j-1.6.1.jar hibernate-commons-annotations-3.2.0.Final.jar hibernate-jpa-2.0-api-1.0.1.Final.jar jta-1.1.jar slf4j-api-1.6.1.jar
Client module dependencies allow you to declare transitive dependencies directly in the build script. They are a replacement for a module descriptor in an external repository.
Example 24.8. Client module dependencies - transitive dependencies
dependencies { runtime module("org.codehaus.groovy:groovy:2.4.7") { dependency("commons-cli:commons-cli:1.0") { transitive = false } module(group: 'org.apache.ant', name: 'ant', version: '1.9.6') { dependencies "org.apache.ant:ant-launcher:1.9.6@jar", "org.apache.ant:ant-junit:1.9.6" } } }
This declares a dependency on Groovy. Groovy itself has dependencies. But Gradle does
not look for an XML descriptor to figure them out but gets the information from the build file. The
dependencies of a client module can be normal module dependencies or artifact dependencies or another
client module. Also look at the API documentation for the
ClientModule class.
In the current release client modules have one limitation. Let's say your project is a library and you want this library to be uploaded to your company's Maven or Ivy repository. Gradle uploads the jars of your project to the company repository together with the XML descriptor file of the dependencies. If you use client modules the dependency declaration in the XML descriptor file is not correct. We will improve this in a future release of Gradle.
Gradle distinguishes between external dependencies and dependencies on projects which are part of the same multi-project build. For the latter you can declare Project Dependencies.
For more information see the API documentation for
Multi-project builds are discussed in Chapter 25, Multi-project Builds.
File dependencies allow you to directly add a set of files to a configuration, without first adding them to a repository. This can be useful if you cannot, or do not want to, place certain files in a repository. Or if you do not want to use any repositories at all for storing your dependencies.
To add some files as a dependency for a configuration, you simply pass a file collection as a dependency:
Example 24.10. File dependencies
dependencies { runtime files('libs/a.jar', 'libs/b.jar') runtime fileTree(dir: 'libs', include: '*.jar') }
File dependencies are not included in the published dependency descriptor for your project. However, file dependencies are included in transitive project dependencies within the same build. This means they cannot be used outside the current build, but they can be used with the same build.
You can declare which tasks produce the files for a file dependency. You might do this when, for example, the files are generated by the build.
Example 24.11. Generated file dependencies
dependencies { compile files("$buildDir/classes") { builtBy 'compile' } } task compile { doLast { println 'compiling classes' } } task list(dependsOn: configurations.compile) { doLast { println "classpath = ${configurations.compile.collect { File file -> file.name }}" } }
Output of
gradle -q list
> gradle -q list compiling classes classpath = [classes]
You can declare a dependency on the API of the current version of Gradle by using the
method. This is useful when you are developing custom Gradle tasks or plugins.
You can declare a dependency on the Groovy that is distributed with Gradle by using the
method. This is useful when you are developing custom Gradle tasks or plugins in Groovy.
You can exclude a transitive dependency either by configuration or by dependency:
Example 24.14. Excluding transitive dependencies
configurations { compile.exclude module: 'commons' all*.exclude group: 'org.gradle.test.excludes', module: 'reports' } dependencies { compile("org.gradle.test.excludes:api:1.0") { exclude module: 'shared' } }
If you define an exclude for a particular configuration, the excluded transitive dependency will be filtered for all
dependencies when resolving this configuration or any inheriting configuration.
If you want to exclude a transitive dependency from all your
configurations you can use the Groovy spread-dot operator to express this in a concise way, as shown in the example.
When defining an exclude, you can specify either only the organization or only the module name or both.
Also look at the API documentation of the
Dependency and
Configuration classes.
Not every transitive dependency can be excluded - some transitive dependencies might be essential for correct runtime behavior of the application. Generally, one can exclude transitive dependencies that are either not required by runtime or that are guaranteed to be available on the target environment/platform.
Should you exclude per-dependency or per-configuration? It turns out that in the majority of cases you want to use the per-configuration exclusion. Here are some typical reasons why one might want to exclude a transitive dependency. Bear in mind that for some of these use cases there are better solutions than exclusions!
ResolutionStrategy for a potentially better solution to the problem.
Basically, in most of the cases excluding the transitive dependency should be done per configuration. This way the dependency declaration is more explicit. It is also more accurate because a per-dependency exclude rule does not guarantee the given transitive dependency does not show up in the configuration. For example, some other dependency, which does not have any exclude rules, might pull in that unwanted transitive dependency.
Other examples of dependency exclusions can be found in the reference for the
ModuleDependency or
DependencyHandler classes.
All attributes for a dependency are optional, except the name. Which attributes are required for actually finding dependencies in the repository will depend on the repository type. See Section 24.6, "Repositories". For example, if you work with Maven repositories, you need to define the group, name and version. If you work with filesystem repositories you might only need the name or the name and the version.
Example 24.15. Optional attributes of dependencies
dependencies { runtime ":junit:4.12", ":testng" runtime name: 'testng' }
You can also assign collections or arrays of dependency notations to a configuration:
Example 24.16. Collections and arrays of dependencies
List groovy = ["org.codehaus.groovy:groovy-all:2.4.7@jar", "commons-cli:commons-cli:1.0@jar", "org.apache.ant:ant:1.9.6@jar"] List hibernate = ['org.hibernate:hibernate:3.0.5@jar', 'somegroup:someorg:1.0@jar'] dependencies { runtime groovy, hibernate }
In Gradle a dependency can have different configurations (as your project can have different configurations). If you don't specify anything explicitly, Gradle uses the default configuration of the dependency. For dependencies from a Maven repository, the default configuration is the only possibility anyway. If you work with Ivy repositories and want to declare a non-default configuration for your dependency you have to use the map notation and declare:
Example 24.17. Dependency configurations
dependencies { runtime group: 'org.somegroup', name: 'somedependency', version: '1.0', configuration: 'someConfiguration' }
To do the same for project dependencies you need to declare:
Example 24.18. Dependency configurations for project
dependencies { compile project(path: ':api', configuration: 'spi') }
You can generate dependency reports from the command line (see Section 4.7.4, "Listing project dependencies"). With the help of the Project report plugin (see Chapter 28, The Project Report Plugin) such a report can be created by your build.
Since Gradle 1.2 there is also a new programmatic API to access the resolved dependency information.
The dependency reports (see the previous paragraph) are using this API under the covers.
The API lets you walk the resolved dependency graph and provides information about the dependencies.
In future releases the API will grow to provide more information about the resolution result.
For more information about the API please refer to the Javadocs on
Potential usages of the
ResolutionResult API:
For the examples below we have the following dependencies setup:
Example 24.19. Configuration.copy
configurations { sealife alllife } dependencies { sealife "sea.mammals:orca:1.0", "sea.fish:shark:1.0", "sea.fish:tuna:1.0" alllife configurations.sealife alllife "air.birds:albatross:1.0" }
The dependencies have the following transitive dependencies:
shark-1.0 -> seal-2.0, tuna-1.0
orca-1.0 -> seal-1.0
tuna-1.0 -> herring-1.0
You can use the configuration to access the declared dependencies or a subset of those:
Example 24.20. Accessing declared dependencies
task dependencies {
doLast {
configurations.alllife.dependencies.each { dep -> println dep.name }
configurations.alllife.allDependencies.each { dep -> println dep.name }
configurations.alllife.allDependencies.findAll { dep -> dep.name != 'orca' }
.each { dep -> println dep.name }
Output of
gradle -q dependencies
> gradle -q dependencies albatross albatross orca shark tuna albatross shark tuna
dependencies task returns only the dependencies belonging explicitly to the configuration.
allDependencies task includes the dependencies from extended configurations.
To get the library files of the configuration dependencies you can do:
Example 24.21. Configuration.files
task allFiles { doLast { configurations.sealife.files.each { file -> println file.name } } }
Output of
gradle -q allFiles
> gradle -q allFiles orca-1.0.jar shark-1.0.jar tuna-1.0.jar herring-1.0.jar seal-2.0.jar
Sometimes you want the library files of a subset of the configuration dependencies (e.g. of a single dependency).
Example 24.22. Configuration.files with spec
task files {
doLast {
configurations.sealife.files { dep -> dep.name == 'orca' }.each { file ->
println file.name
Output of
gradle -q files
> gradle -q files orca-1.0.jar seal-2.0.jar
Configuration.files method always retrieves all artifacts of the whole
configuration. It then filters the retrieved files by specified dependencies. As you can see in the example, transitive dependencies are included.
You can also copy a configuration. You can optionally specify that only a subset of dependencies from the original configuration
should be copied. The copying methods come in two flavors. The
copy method copies only the dependencies belonging explicitly to the configuration. The
copyRecursive method copies all the dependencies, including the dependencies from extended configurations.
Example 24.23. Configuration.copy
task copy {
doLast {
configurations.alllife.copyRecursive { dep -> dep.name != 'orca' }
.allDependencies.each { dep -> println dep.name }
.each { dep -> println dep.name }
Output of
gradle -q copy
> gradle -q copy albatross shark tuna albatross
It is important to note that the returned files of the copied configuration are often but not always the same than the returned files of the dependency subset of the original configuration. In case of version conflicts between dependencies of the subset and dependencies not belonging to the subset the resolve result might be different.
Example 24.24. Configuration.copy vs. Configuration.files
task copyVsFiles { doLast { configurations.sealife.copyRecursive { dep -> dep.name == 'orca' } .each { file -> println file.name } println() configurations.sealife.files { dep -> dep.name == 'orca' } .each { file -> println file.name } } }
Output of
gradle -q copyVsFiles
> gradle -q copyVsFiles orca-1.0.jar seal-1.0.jar orca-1.0.jar seal-2.0.jar
In the example above,
orca has a dependency on
seal-1.0 whereas
shark has a dependency on
seal-2.0. The original configuration
has therefore a version conflict which is resolved to the newer
seal-2.0 version. The
files method therefore returns
seal-2.0 as a
transitive dependency of
orca. The copied configuration only has
orca as a dependency and therefore there is no version conflict and
is returned as a transitive dependency.
Once a configuration is resolved it is immutable. Changing its state or the state of one of its dependencies will cause an exception. You can always copy a resolved configuration. The copied configuration is in the unresolved state and can be freshly resolved.
To learn more about the API of the configuration class see the API documentation:
Gradle repository management, based on Apache Ivy, gives you a lot of freedom regarding repository layout and retrieval policies. Additionally Gradle provides various convenience method to add pre-configured repositories.
You may configure any number of repositories, each of which is treated independently by Gradle. If Gradle finds a module descriptor in a particular repository, it will attempt to download all of the artifacts for that module from the same repository. Although module meta-data and module artifacts must be located in the same repository, it is possible to compose a single repository of multiple URLs, giving multiple locations to search for meta-data files and jar files.
There are several different types of repositories you can declare:
Table 24.2. Repository types
|Maven central repository
|A pre-configured repository that looks for dependencies in Maven Central.
|Maven JCenter repository
|A pre-configured repository that looks for dependencies in Bintray's JCenter.
|Maven local repository
|A pre-configured repository that looks for dependencies in the local Maven repository.
|Maven repository
|A Maven repository. Can be located on the local filesystem or at some remote location.
|Ivy repository
|An Ivy repository. Can be located on the local filesystem or at some remote location.
|Flat directory repository
|A simple repository on the local filesystem. Does not support any meta-data formats.
To add the central Maven 2 repository (http://repo1.maven.org/maven2) simply add this to your build script:
Now Gradle will look for your dependencies in this repository.
Warning: Be aware that the central Maven 2 repository is HTTP only and HTTPS is not supported. If you need a public HTTPS enabled central repository, you can use the JCenter public repository (see Section 24.6.2, "Maven JCenter repository").
Bintray's JCenter is an up-to-date collection of all popular Maven OSS artifacts, including artifacts published directly to Bintray.
To add the JCenter Maven repository (https://jcenter.bintray.com) simply add this to your build script:
Now Gradle will look for your dependencies in the JCenter repository. jcenter() uses HTTPS to connect to the repository.
If you want to use HTTP you can configure
Example 24.27. Using Bintrays's JCenter with HTTP
repositories {
jcenter {
url "http://jcenter.bintray.com/"
To use the local Maven cache as a repository you can do:
Example 24.28. Adding the local Maven cache as a repository
repositories { mavenLocal() }
Gradle uses the same logic as Maven to identify the location of your local Maven cache. If a local repository location is defined in a
settings.xml, this location
will be used. The
settings.xml in
takes precedence over the
. If no
settings.xml is available, Gradle uses the default location
For adding a custom Maven repository you can do:
Example 24.29. Adding custom Maven repository
repositories {
maven {
url "http://repo.mycompany.com/maven2"
Sometimes a repository will have the POMs published to one location, and the JARs and other artifacts published at another location. To define such a repository, you can do:
Example 24.30. Adding additional Maven repositories for JAR files
repositories { maven { // Look for POMs and artifacts, such as JARs, here url "http://repo2.mycompany.com/maven2" // Look for artifacts here if not found at the above location artifactUrls "http://repo.mycompany.com/jars" artifactUrls "http://repo.mycompany.com/jars2" } }
Gradle will look at the first URL for the POM and the JAR. If the JAR can't be found there, the artifact URLs are used to look for JARs.
To access a Maven repository which uses basic authentication, you specify the username and password to use when you define the repository:
Example 24.31. Accessing password protected Maven repository
repositories { maven { credentials { username 'user' password 'password' } url "http://repo.mycompany.com/maven2" } }
It is advisable to keep your username and password in
gradle.properties rather than directly in the build file.
If you want to use a (flat) filesystem directory as a repository, simply type:
Example 24.32. Flat repository resolver
repositories { flatDir { dirs 'lib' } flatDir { dirs 'lib1', 'lib2' } }
This adds repositories which look into one or more directories for finding dependencies. Note that
this type of repository does not support any meta-data formats like Ivy XML or Maven POM files. Instead,
Gradle will dynamically generate a module descriptor (without any dependency information) based on the
presence of artifacts. However, as Gradle prefers to use modules whose descriptor has been created from
real meta-data rather than being generated, flat directory repositories cannot be used to override
artifacts with real meta-data from other repositories. So, for example, if Gradle finds only
jmxri-1.2.1.jar in a flat directory repository, but
in another repository that supports meta-data, it will use the second repository to provide the module.
For the use case of overriding remote artifacts with local ones consider using an Ivy or Maven repository
instead whose URL points to a local directory.
If you only work with flat directory repositories you don't need to set all attributes of a dependency.
See Section 24.4.8, "Optional attributes".
Example 24.33. Ivy repository
repositories {
ivy {
url "http://repo.mycompany.com/repo"
You can specify that your repository conforms to the Ivy or Maven default layout by using a named layout.
Example 24.34. Ivy repository with named layout
repositories { ivy { url "http://repo.mycompany.com/repo" layout "maven" } }
Valid named layout values are
'gradle' (the default),
'ivy' and
IvyArtifactRepository.layout(java.lang.String, groovy.lang.Closure) in the API documentation for details of these named layouts.
To define an Ivy repository with a non-standard layout, you can define a 'pattern' layout for the repository:
Example 24.35. Ivy repository with pattern layout
repositories { ivy { url "http://repo.mycompany.com/repo" layout "pattern", { artifact "[module]/[revision]/[type]/[artifact].[ext]" } } }
To define an Ivy repository which fetches Ivy files and artifacts from different locations, you can define separate patterns to use to locate the Ivy files and artifacts:
artifact or
ivy specified for a repository adds an additional pattern to use.
The patterns are used in the order that they are defined.
Example 24.36. Ivy repository with multiple custom patterns
repositories { ivy { url "http://repo.mycompany.com/repo" layout "pattern", { artifact "3rd-party-artifacts/[organisation]/[module]/[revision]/[artifact]-[revision].[ext]" artifact "company-artifacts/[organisation]/[module]/[revision]/[artifact]-[revision].[ext]" ivy "ivy-files/[organisation]/[module]/[revision]/ivy.xml" } } }
Optionally, a repository with pattern layout can have its 'organisation' part laid out in Maven style, with
forward slashes replacing dots as separators. For example, the organisation
my.company would then be represented as
Example 24.37. Ivy repository with Maven compatible layout
repositories { ivy { url "http://repo.mycompany.com/repo" layout "pattern", { artifact "[organisation]/[module]/[revision]/[artifact]-[revision].[ext]" m2compatible = true } } }
To access an Ivy repository which uses basic authentication, you specify the username and password to use when you define the repository:
Example 24.38. Ivy repository
repositories { ivy { url 'http://repo.mycompany.com' credentials { username 'user' password 'password' } } }
Maven and Ivy repositories support the use of various transport protocols. At the moment the following protocols are supported:
Table 24.3. Repository transport protocols
|Credential types
|access key/secret key
To define a repository use the
repositories configuration block. Within the
repositories closure,
a Maven repository is declared with
maven. An Ivy repository is declared with
ivy. The transport protocol
is part of the URL definition for a repository. The following build script demonstrates how to create a HTTP-based Maven and Ivy repository:
Example 24.39. Declaring a Maven and Ivy repository
repositories { maven { url "http://repo.mycompany.com/maven2" } ivy { url "http://repo.mycompany.com/repo" } }
If authentication is required for a repository, the relevant credentials can be provided. The following example shows how to provide username/password-based authentication for SFTP repositories:
Example 24.40. Providing credentials to a Maven and Ivy repository
repositories { maven { url "sftp://repo.mycompany.com:22/maven2" credentials { username 'user' password 'password' } } ivy { url "sftp://repo.mycompany.com:22/repo" credentials { username 'user' password 'password' } } }
When using an AWS S3 backed repository you need to authenticate using
AwsCredentials, providing access-key and a private-key.
The following example shows how to declare a S3 backed repository and providing AWS credentials:
Example 24.41. Declaring a S3 backed Maven and Ivy repository
repositories { maven { url "s3://myCompanyBucket/maven2" credentials(AwsCredentials) { accessKey "someKey" secretKey "someSecret" } } ivy { url "s3://myCompanyBucket/ivyrepo" credentials(AwsCredentials) { accessKey "someKey" secretKey "someSecret" } } }
The following system properties can be used to configure the interactions with s3 repositories:
Table 24.4. S3 Configuration Properties
|Used to override the AWS S3 endpoint when using a non AWS, S3 API compatible, storage service.
|Specifies the maximum number of times to retry a request in the event that the S3 server responds with a HTTP 5xx status code. When not specified a default value of 3 is used.
S3 URL's are 'virtual-hosted-style' and must be in the following format
is the AWS S3 bucket name.
is the optional region specific endpoint.
is the AWS S3 key (unique identifier for an object within a bucket)
A proxy for S3 can be configured using the following system properties:
If the 'org.gradle.s3.endpoint' property has been specified with a http (not https) URI the following system proxy settings can be used:
Some of the AWS S3 regions (eu-central-1 - Frankfurt) require that all HTTP requests are signed in accordance with AWS's
signature version 4. It is recommended to specify
S3 URL's containing the region specific endpoint when using buckets that require V4 signatures.
Attempting to re-send the request to .... with AWS V4 authentication. To avoid this warning in the future, please use region-specific endpoint to access buckets located in regions that require V4 signing.
Failing to specify the region-specific endpoint for buckets requiring V4 signatures means:
3 round-trips to AWS, as opposed to one, for every file upload and download.
Depending on location - increased network latencies and slower builds.
Increased likelihood of transmission failures.
When configuring a repository using HTTP or HTTPS transport protocols, multiple authentication schemes are available. By default, Gradle will attempt to use all schemes that are supported by the Apache HttpClient library, documented here. In some cases, it may be preferable to explicitly specify which authentication schemes should be used when exchanging credentials with a remote server. When explicitly declared, only those schemes are used when authenticating to a remote repository. The following example show how to configure a repository to use only digest authentication:
Example 24.42. Configure repository to use only digest authentication
repositories { maven { url 'https://repo.mycompany.com/maven2' credentials { username 'user' password 'password' } authentication { digest(DigestAuthentication) } } }
Currently supported authentication schemes are:
Table 24.5. Authentication schemes
|Basic access authentication over HTTP. When using this scheme, credentials are sent preemptively.
|Digest access authentication over HTTP.
Gradle's default behavior is to only submit credentials when a server responds with an authentication challenge in the form of a HTTP 401 response. In some cases,
the server will respond with a different code (ex. for repositories hosted on GitHub a 404 is returned) causing dependency resolution to fail. To get around this
behavior, credentials may be sent to the server preemptively. To enable preemptive authentication simply configure your repository to explicitly use the
BasicAuthentication scheme:
Example 24.43. Configure repository to use preemptive authentication
repositories { maven { url 'https://repo.mycompany.com/maven2' credentials { username 'user' password 'password' } authentication { basic(BasicAuthentication) } } }
To access a repository:
Example 24.44. Accessing a repository
println repositories.localRepository.name
println repositories['localRepository'].name
To configure a repository:
Example 24.45. Configuration of a repository
repositories { flatDir { name 'localRepository' } } repositories { localRepository { dirs 'lib' } } repositories.localRepository { dirs 'lib' }
Gradle is extremely flexible regarding repositories:
There are many options for the protocol to communicate with the repository (e.g. filesystem, http, ssh, sftp ...)
The protocol sftp currently only supports username/password-based authentication.
Each repository can have its own layout.
Let's say, you declare a dependency on the
junit:junit:3.8.2 library.
Now how does Gradle find it in the repositories? Somehow the dependency information has to be mapped to a
path. In contrast to Maven, where this path is fixed, with Gradle you can define a pattern that defines
what the path will look like. Here are some examples:
// Maven2 layout (if a repository is marked as Maven2 compatible, the organization (group) is split into subfolders according to the dots.) someroot/[organisation]/[module]/[revision]/[module]-[revision].[ext] // Typical layout for an Ivy repository (the organization is not split into subfolder) someroot/[organisation]/[module]/[revision]/[type]s/[artifact].[ext] // Simple layout (the organization is not used, no nested folders.) someroot/[artifact]-[revision].[ext]
To add any kind of repository (you can pretty easy write your own ones) you can do:
Example 24.46. Definition of a custom repository
repositories { ivy { ivyPattern "$projectDir/repo/[organisation]/[module]-ivy-[revision].xml" artifactPattern "$projectDir/repo/[organisation]/[module]-[revision](-[classifier]).[ext]" } }
An overview of which Resolvers are offered by Ivy and thus also by Gradle can be found here. With Gradle you just don't configure them via XML but directly via their API.
Gradle takes your dependency declarations and repository definitions and attempts to download all of your dependencies by a process called dependency resolution. Below is a brief outline of how this process works.
Given a required dependency, Gradle first attempts to resolve the module for that dependency. Each repository is inspected in order, searching first for a module descriptor file (POM or Ivy file) that indicates the presence of that module. If no module descriptor is found, Gradle will search for the presence of the primary module artifact file indicating that the module exists in the repository.
If the dependency is declared as a dynamic version (like
1.+), Gradle will resolve this to the newest available static version (like
1.2) in the repository. For Maven repositories, this is done using the
file, while for Ivy repositories this is done by directory listing.
If the module descriptor is a POM file that has a parent POM declared, Gradle will recursively attempt to resolve each of the parent modules for the POM.
Once each repository has been inspected for the module, Gradle will choose the 'best' one to use. This is done using the following criteria:
When the dependency is declared by a static version and a module descriptor file is found in a repository, there is no need to continue searching later repositories and the remainder of the process is short-circuited.
All of the artifacts for the module are then requested from the same repository that was chosen in the process above.
In most cases, Gradle's default dependency management will resolve the dependencies that you want in your build. In some cases, however, it can be necessary to tweak dependency resolution to ensure that your build receives exactly the right dependencies.
There are a number of ways that you can influence how Gradle resolves dependencies.
Forcing a module version tells Gradle to always use a specific version for given dependency (transitive or not), overriding any version specified in a published module descriptor. This can be very useful when tackling version conflicts - for more information see Section 24.2.3, "Resolve version conflicts".
Force versions can also be used to deal with rogue metadata of transitive dependencies.
If a transitive dependency has poor quality metadata that leads to problems at dependency resolution time, you can force Gradle to use a newer, fixed version of this dependency.
For an example, see the
ResolutionStrategy class in the API documentation.
Note that 'dependency resolve rules' (outlined below) provide a more powerful mechanism for replacing a broken module dependency. See the section called "Blacklisting a particular version with a replacement".
Preferring project modules tells Gradle to use the version of a module that is part of the build itself (as part of Chapter 25, Multi-project Builds or as includes in Chapter 10, Composite builds). This allows the easy inclusion of an individual fork (e.g. containing a bugfix) of a module - for more information see Section 24.2.3, "Resolve version conflicts".
A dependency resolve rule is executed for each resolved dependency, and offers a powerful api for manipulating a requested dependency prior to that dependency being resolved. This feature is incubating, but currently offers the ability to change the group, name and/or version of a requested dependency, allowing a dependency to be substituted with a completely different module during resolution.
Dependency resolve rules provide a very powerful way to control the dependency resolution process, and can be used to implement all sorts of advanced
patterns in dependency management. Some of these patterns are outlined below.
For more information and code samples see the
ResolutionStrategy class in the API documentation.
Often an organisation publishes a set of libraries with a single version; where the libraries are built, tested and published together. These libraries form a 'releasable unit', designed and intended to be used as a whole. It does not make sense to use libraries from different releasable units together.
But it is easy for transitive dependency resolution to violate this contract. For example:
module-a depends on
module-b depends on
A build depending on both
module-a and
module-b will obtain different versions of libraries within the releasable unit.
Dependency resolve rules give you the power to enforce releasable units in your build. Imagine a releasable unit defined by all libraries that have 'org.gradle' group. We can force all of these libraries to use a consistent version:
Example 24.47. Forcing consistent version for a group of libraries
configurations.all { resolutionStrategy.eachDependency { DependencyResolveDetails details -> if (details.requested.group == 'org.gradle') { details.useVersion '1.4' } } }
In some corporate environments, the list of module versions that can be declared in Gradle builds is maintained and audited externally. Dependency resolve rules provide a neat implementation of this pattern:
This rule implementation can be neatly encapsulated in a corporate plugin, and shared across all builds within the organisation.
Example 24.48. Using a custom versioning scheme
configurations.all { resolutionStrategy.eachDependency { DependencyResolveDetails details -> if (details.requested.version == 'default') { def version = findDefaultVersionInCatalog(details.requested.group, details.requested.name) details.useVersion version } } } def findDefaultVersionInCatalog(String group, String name) { //some custom logic that resolves the default version into a specific version "1.0" }
Dependency resolve rules provide a mechanism for blacklisting a particular version of a dependency and providing a replacement version. This can be useful if a certain dependency version is broken and should not be used, where a dependency resolve rule causes this version to be replaced with a known good version. One example of a broken module is one that declares a dependency on a library that cannot be found in any of the public repositories, but there are many other reasons why a particular module version is unwanted and a different version is preferred.
In example below, imagine that version
1.2.1 contains important fixes and should always be used in preference to
The rule provided will enforce just this: any time version
1.2 is encountered it will be replaced with
Note that this is different from a forced version as described above, in that any other versions of this module would not be affected.
This means that the 'newest' conflict resolution strategy would still select version
1.3 if this version was also pulled transitively.
Example 24.49. Blacklisting a version with a replacement
configurations.all { resolutionStrategy.eachDependency { DependencyResolveDetails details -> if (details.requested.group == 'org.software' && details.requested.name == 'some-library' && details.requested.version == '1.2') { //prefer different version which contains some necessary fixes details.useVersion '1.2.1' } } }
At times a completely different module can serve as a replacement for a requested module dependency.
Examples include using '
groovy' in place of '
groovy-all', or using '
log4j-over-slf4j' instead of '
Starting with Gradle 1.5 you can make these substitutions using dependency resolve rules:
Example 24.50. Changing dependency group and/or name at the resolution
configurations.all { resolutionStrategy.eachDependency { DependencyResolveDetails details -> if (details.requested.name == 'groovy-all') { //prefer 'groovy' over 'groovy-all': details.useTarget group: details.requested.group, name: 'groovy', version: details.requested.version } if (details.requested.name == 'log4j') { //prefer 'log4j-over-slf4j' over 'log4j', with fixed version: details.useTarget "org.slf4j:log4j-over-slf4j:1.7.10" } } }
Dependency substitution rules work similarly to dependency resolve rules. In fact, many capabilities of dependency resolve rules can be implemented with dependency substitution rules. They allow project and module dependencies to be transparently substituted with specified replacements. Unlike dependency resolve rules, dependency substitution rules allow project and module dependencies to be substituted interchangeably.
NOTE: Adding a dependency substitution rule to a configuration changes the timing of when that configuration is resolved. Instead of being resolved on first use, the configuration is instead resolved when the task graph is being constructed. This can have unexpected consequences if the configuration is being further modified during task execution, or if the configuration relies on modules that are published during execution of another task.
To explain:
Configuration can be declared as an input to any Task, and that configuration can include project dependencies when it is resolved.
Configuration inputs.
In the absence of dependency substitution rules, Gradle knows that an external module dependency will never transitively reference a project dependency. This makes it easy to determine the full set of project dependencies for a configuration through simple graph traversal. With this functionality, Gradle can no longer make this assumption, and must perform a full resolve in order to determine the project dependencies.
One use case for dependency substitution is to use a locally developed version of a module in place of one that is downloaded from an external repository. This could be useful for testing a local, patched version of a dependency.
The module to be replaced can be declared with or without a version specified.
Example 24.51. Substituting a module with a project
configurations.all { resolutionStrategy.dependencySubstitution { substitute module("org.utils:api") with project(":api") substitute module("org.utils:util:2.5") with project(":util") } }
Note that a project that is substituted must be included in the multi-project build (via settings.gradle). Dependency substitution rules take care of replacing the module dependency with the project dependency and wiring up any task dependencies, but do not implicitly include the project in the build.
Another way to use substitution rules is to replace a project dependency with a module in a multi-project build. This can be useful to speed up development with a large multi-project build, by allowing a subset of the project dependencies to be downloaded from a repository rather than being built.
The module to be used as a replacement must be declared with a version specified.
Example 24.52. Substituting a project with a module
configurations.all { resolutionStrategy.dependencySubstitution { substitute project(":api") with module("org.utils:api:1.3") } }
When a project dependency has been replaced with a module dependency, that project is still included in the overall multi-project build.
However, tasks to build the replaced dependency will not be executed in order to build the resolve the depending
A common use case for dependency substitution is to allow more flexible assembly of sub-projects within a multi-project build. This can be useful for developing a local, patched version of an external dependency or for building a subset of the modules within a large multi-project build.
The following example uses a dependency substitution rule to replace any module dependency with the group "org.example", but only if a local project matching the dependency name can be located.
Example 24.53. Conditionally substituting a dependency
configurations.all { resolutionStrategy.dependencySubstitution.all { DependencySubstitution dependency -> if (dependency.requested instanceof ModuleComponentSelector && dependency.requested.group == "org.example") { def targetProject = findProject(":${dependency.requested.module}") if (targetProject != null) { dependency.useTarget targetProject } } } }
Note that a project that is substituted must be included in the multi-project build (via settings.gradle). Dependency substitution rules take care of replacing the module dependency with the project dependency, but do not implicitly include the project in the build.
A configuration can be configured with default dependencies to be used if no dependencies are explicitly set for the configuration. A primary use case of this functionality is for developing plugins that make use of versioned tools that the user might override. By specifying default dependencies, the plugin can use a default version of the tool only if the user has not specified a particular version to use.
Example 24.54. Specifying default dependencies on a configuration
configurations { pluginTool { defaultDependencies { dependencies -> dependencies.add(this.project.dependencies.create("org.gradle:my-util:1.0")) } } }
Gradle's Ivy repository implementations support the equivalent to Ivy's dynamic resolve mode. Normally, Gradle will use the
rev attribute for each dependency
definition included in an
ivy.xml file. In dynamic resolve mode, Gradle will instead prefer the
revConstraint attribute over the
rev attribute for a given dependency definition. If the
revConstraint attribute is not present, the
rev attribute is used
To enable dynamic resolve mode, you need to set the appropriate option on the repository definition. A couple of examples are shown below. Note that dynamic resolve mode is only
available for Gradle's Ivy repositories. It is not available for Maven repositories, or custom Ivy
DependencyResolver implementations.
Example 24.55. Enabling dynamic resolve mode
// Can enable dynamic resolve mode when you define the repository repositories { ivy { url "http://repo.mycompany.com/repo" resolve.dynamicMode = true } } // Can use a rule instead to enable (or disable) dynamic resolve mode for all repositories repositories.withType(IvyArtifactRepository) { resolve.dynamicMode = true }
Each module (also called component) has metadata associated with it, such as its group, name, version, dependencies, and so on. This metadata typically originates in the module's descriptor. Metadata rules allow certain parts of a module's metadata to be manipulated from within the build script. They take effect after a module's descriptor has been downloaded, but before it has been selected among all candidate versions. This makes metadata rules another instrument for customizing dependency resolution.
One piece of module metadata that Gradle understands is a module's status scheme. This concept, also known from Ivy, models the different
levels of maturity that a module transitions through over time. The default status scheme, ordered from least to most mature status, is
release. Apart from a status scheme, a module also has a (current) status, which must be one of
the values in its status scheme. If not specified in the (Ivy) descriptor, the status defaults to
integration for Ivy modules and Maven snapshot modules,
release for Maven modules that aren't snapshots.
A module's status and status scheme are taken into consideration when a
latest version selector is resolved. Specifically,
will resolve to the highest module version that has status
someStatus or a more mature status. For example, with the default status scheme in place,
latest.integration will select the highest module version regardless of its status (because
integration is the least mature status),
latest.release will select the highest module version with status
release. Here is what this looks like in code:
Example 24.56. 'Latest' version selector
dependencies { config1 "org.sample:client:latest.integration" config2 "org.sample:client:latest.release" } task listConfigs { doLast { configurations.config1.each { println it.name } println() configurations.config2.each { println it.name } } }
Output of
gradle -q listConfigs
> gradle -q listConfigs client-1.5.jar client-1.4.jar
The next example demonstrates
latest selectors based on a custom status scheme declared in a component metadata rule that applies to all modules:
Example 24.57. Custom status scheme
dependencies { config3 "org.sample:api:latest.silver" components { all { ComponentMetadataDetails details -> if (details.id.group == "org.sample" && details.id.name == "api") { details.statusScheme = ["bronze", "silver", "gold", "platinum"] } } } }
Component metadata rules can be applied to a specified module. Modules must be specified in the form of "group:module".
Example 24.58. Custom status scheme by module
dependencies { config4 "org.sample:lib:latest.prod" components { withModule('org.sample:lib') { ComponentMetadataDetails details -> details.statusScheme = ["int", "rc", "prod"] } } }
Gradle can also create component metadata rules utilizing Ivy-specific metadata for modules resolved from an Ivy repository.
Values from the Ivy descriptor are made available via the
IvyModuleDescriptor interface.
Example 24.59. Ivy component metadata rule
dependencies { config6 "org.sample:lib:latest.rc" components { withModule("org.sample:lib") { ComponentMetadataDetails details, IvyModuleDescriptor ivyModule -> if (ivyModule.branch == 'testing') { details.status = "rc" } } } }
Note that any rule that declares specific arguments must always include a
ComponentMetadataDetails argument
as the first argument. The second Ivy metadata argument is optional.
Component metadata rules can also be defined using a rule source object. A rule source object
is any object that contains exactly one method that defines the rule action and is annotated with
This method:
ComponentMetadataDetails as the first argument.
Example 24.60. Rule source component metadata rule
dependencies { config5 "org.sample:api:latest.gold" components { withModule('org.sample:api', new CustomStatusRule()) } } class CustomStatusRule { @Mutate void setStatusScheme(ComponentMetadataDetails details) { details.statusScheme = ["bronze", "silver", "gold", "platinum"] } }
Component selection rules may influence which component instance should be selected when multiple versions are available that match a version selector. Rules are applied against every available version and allow the version to be explicitly rejected by rule. This allows Gradle to ignore any component instance that does not satisfy conditions set by the rule. Examples include:
Rules are configured via the
ComponentSelectionRules object. Each rule configured
will be called with a
ComponentSelection object as an argument which contains information
about the candidate version being considered.
ComponentSelection.reject(java.lang.String) causes the given candidate version to be explicitly rejected,
in which case the candidate will not be considered for the selector.
The following example shows a rule that disallows a particular version of a module but allows the dynamic version to choose the next best candidate.
Example 24.61. Component selection rule
configurations { rejectConfig { resolutionStrategy { componentSelection { // Accept the highest version matching the requested version that isn't '1.5' all { ComponentSelection selection -> if (selection.candidate.group == 'org.sample' && selection.candidate.module == 'api' && selection.candidate.version == '1.5') { selection.reject("version 1.5 is broken for 'org.sample:api'") } } } } } } dependencies { rejectConfig "org.sample:api:1.+" }
Note that version selection is applied starting with the highest version first. The version selected will be the first version found that all component selection rules accept. A version is considered accepted no rule explicitly rejects it.
Similarly, rules can be targeted at specific modules. Modules must be specified in the form of "group:module".
Example 24.62. Component selection rule with module target
configurations { targetConfig { resolutionStrategy { componentSelection { withModule("org.sample:api") { ComponentSelection selection -> if (selection.candidate.version == "1.5") { selection.reject("version 1.5 is broken for 'org.sample:api'") } } } } } }
Component selection rules can also consider component metadata when selecting a version. Possible metadata arguments that can be considered are
ComponentMetadata and
Example 24.63. Component selection rule with metadata
configurations { metadataRulesConfig { resolutionStrategy { componentSelection { // Reject any versions with a status of 'experimental' all { ComponentSelection selection, ComponentMetadata metadata -> if (selection.candidate.group == 'org.sample' && metadata.status == 'experimental') { selection.reject("don't use experimental candidates from 'org.sample'") } } // Accept the highest version with either a "release" branch or a status of 'milestone' withModule('org.sample:api') { ComponentSelection selection, IvyModuleDescriptor descriptor, ComponentMetadata metadata -> if (descriptor.branch != "release" && metadata.status != 'milestone') { selection.reject("'org.sample:api' must have testing branch or milestone status") } } } } } }
Note that a
ComponentSelection argument is always required as the first
parameter when declaring a component selection rule with additional Ivy metadata parameters, but the metadata parameters can be declared
in any order.
Lastly, component selection rules can also be defined using a rule source object. A rule source object
is any object that contains exactly one method that defines the rule action and is annotated with
This method:
ComponentSelection as the first argument.
ComponentMetadata and/or
Example 24.64. Component selection rule using a rule source object
class RejectTestBranch { @Mutate void evaluateRule(ComponentSelection selection, IvyModuleDescriptor ivy) { if (ivy.branch == "test") { selection.reject("reject test branch") } } } configurations { ruleSourceConfig { resolutionStrategy { componentSelection { all new RejectTestBranch() } } } }
Module replacement rules allow a build to declare that a legacy library has been replaced by a new one. A good example when a new library replaced a legacy one is the "google-collections" -> "guava" migration. The team that created google-collections decided to change the module name from "com.google.collections:google-collections" into "com.google.guava:guava". This a legal scenario in the industry: teams need to be able to change the names of products they maintain, including the module coordinates. Renaming of the module coordinates has impact on conflict resolution.
To explain the impact on conflict resolution, let's consider the "google-collections" -> "guava" scenario. It may happen that both libraries are pulled into the same dependency graph. For example, "our" project depends on guava but some of our dependencies pull in a legacy version of google-collections. This can cause runtime errors, for example during test or application execution. Gradle does not automatically resolve the google-collections VS guava conflict because it is not considered as a "version conflict". It's because the module coordinates for both libraries are completely different and conflict resolution is activated when "group" and "name" coordinates are the same but there are different versions available in the dependency graph (for more info, please refer to the section on conflict resolution). Traditional remedies to this problem are:
Traditional approaches work but they are not general enough. For example, an organisation wants to resolve the google-collections VS guava conflict resolution problem in all projects. Starting from Gradle 2.2 it is possible to declare that certain module was replaced by other. This enables organisations to include the information about module replacement in the corporate plugin suite and resolve the problem holistically for all Gradle-powered projects in the enterprise.
Example 24.65. Declaring module replacement
dependencies { modules { module("com.google.collections:google-collections") { replacedBy("com.google.guava:guava") } } }
For more examples and detailed API, please refer to the DSL reference for
What happens when we declare that "google-collections" are replaced by "guava"? Gradle can use this information for conflict resolution. Gradle will consider every version of "guava" newer/better than any version of "google-collections". Also, Gradle will ensure that only guava jar is present in the classpath / resolved file list. Please note that if only "google-collections" appears in the dependency graph (e.g. no "guava") Gradle will not eagerly replace it with "guava". Module replacement is an information that Gradle uses for resolving conflicts. If there is no conflict (e.g. only "google-collections" or only "guava" in the graph) the replacement information is not used.
Currently it is not possible to declare that certain modules is replaced by a set of modules. However, it is possible to declare that multiple modules are replaced by a single module.
Gradle contains a highly sophisticated dependency caching mechanism, which seeks to minimise the number of remote requests made in dependency resolution, while striving to guarantee that the results of dependency resolution are correct and reproducible.
The Gradle dependency cache consists of 2 key types of storage:
A file-based store of downloaded artifacts, including binaries like jars as well as raw downloaded meta-data like POM files and Ivy files. The storage path for a downloaded artifact includes the SHA1 checksum, meaning that 2 artifacts with the same name but different content can easily be cached.
A binary store of resolved module meta-data, including the results of resolving dynamic versions, module descriptors, and artifacts.
Separating the storage of downloaded artifacts from the cache metadata permits us to do some very powerful things with our cache that would be difficult with a transparent, file-only cache layout.
The Gradle cache does not allow the local cache to hide problems and create other mysterious and difficult to debug behavior that has been a challenge with many build tools. This new behavior is implemented in a bandwidth and storage efficient way. In doing so, Gradle enables reliable and reproducible enterprise builds.
Gradle keeps a record of various aspects of dependency resolution in binary format in the metadata cache. The information stored in the metadata cache includes:
1.+) to a concrete version (e.g.
Every entry in the metadata cache includes a record of the repository that provided the information as well as a timestamp that can be used for cache expiry.
As described above, for each repository there is a separate metadata cache. A repository is identified by its URL, type and layout. If a module or artifact has not been previously resolved from this repository, Gradle will attempt to resolve the module against the repository. This will always involve a remote lookup on the repository, however in many cases no download will be required (seethe section called "Artifact reuse", below).
Dependency resolution will fail if the required artifacts are not available in any repository specified by the build, even if the local cache has a copy of this artifact which was retrieved from a different repository. Repository independence allows builds to be isolated from each other in an advanced way that no build tool has done before. This is a key feature to create builds that are reliable and reproducible in any environment.
Before downloading an artifact, Gradle tries to determine the checksum of the required artifact by downloading the sha file associated with that artifact. If the checksum can be retrieved, an artifact is not downloaded if an artifact already exists with the same id and checksum. If the checksum cannot be retrieved from the remote server, the artifact will be downloaded (and ignored if it matches an existing artifact).
As well as considering artifacts downloaded from a different repository, Gradle will also attempt to reuse artifacts found in the local Maven Repository. If a candidate artifact has been downloaded by Maven, Gradle will use this artifact if it can be verified to match the checksum declared by the remote server.
It is possible for different repositories to provide a different binary artifact in response to the same artifact identifier. This is often the case with Maven SNAPSHOT artifacts, but can also be true for any artifact which is republished without changing it's identifier. By caching artifacts based on their SHA1 checksum, Gradle is able to maintain multiple versions of the same artifact. This means that when resolving against one repository Gradle will never overwrite the cached artifact file from a different repository. This is done without requiring a separate artifact file store per repository.
command line switch tells Gradle to always use dependency modules from the cache, regardless if they are due to be checked again.
When running with offline, Gradle will never attempt to access the network to perform dependency resolution.
If required modules are not present in the dependency cache, build execution will fail.
At times, the Gradle Dependency Cache can be out of sync with the actual state of the configured repositories. Perhaps a repository was initially misconfigured,
or perhaps a "non-changing" module was published incorrectly. To refresh all dependencies in the dependency cache, use the
--refresh-dependencies option on the command line.
--refresh-dependencies option tells Gradle to ignore all cached entries for resolved modules and artifacts.
A fresh resolve will be performed against all configured repositories, with dynamic versions recalculated, modules refreshed, and artifacts downloaded.
However, where possible Gradle will check if the previously downloaded artifacts are valid before downloading again.
This is done by comparing published SHA1 values in the repository with the SHA1 values for existing downloaded artifacts.
You can fine-tune certain aspects of caching using the
for a configuration.
By default, Gradle caches dynamic versions for 24 hours. To change how long Gradle will cache the resolved version for a dynamic version, use:
Example 24.66. Dynamic version cache control
configurations.all { resolutionStrategy.cacheDynamicVersionsFor 10, 'minutes' }
By default, Gradle caches changing modules for 24 hours. To change how long Gradle will cache the meta-data and artifacts for a changing module, use:
Example 24.67. Changing module cache control
configurations.all { resolutionStrategy.cacheChangingModulesFor 4, 'hours' }
For more details, take a look at the API documentation for
Many projects rely on the Maven Central repository. This is not without problems.
The Maven Central repository can be down or can be slow to respond.
The POM files of many popular projects specify dependencies or other configuration that
are just plain wrong (for instance, the POM file of the "
module declares JUnit as a runtime dependency).
For many projects there is not one right set of dependencies (as more or less imposed by the POM format).
If your project relies on the Maven Central repository you are likely to need an additional custom repository, because:
You might need dependencies that are not uploaded to Maven Central yet.
You want to deal properly with invalid metadata in a Maven Central POM file.
You don't want to expose people to the downtimes or slow response of Maven Central, if they just want to build your project.
It is not a big deal to set-up a custom repository, [14] but it can be tedious to keep it up to date. For a new version, you always have to create the new XML descriptor and the directories. Your custom repository is another infrastructure element which might have downtimes and needs to be updated. To enable historical builds, you need to keep all the past libraries, not to mention a backup of these. It is another layer of indirection. Another source of information you have to lookup. All this is not really a big deal but in its sum it has an impact. Repository managers like Artifactory or Nexus make this easier, but most open source projects don't usually have a host for those products. This is changing with new services like Bintray that let developers host and distribute their release binaries using a self-service repository platform. Bintray also supports sharing approved artifacts though the JCenter public repository to provide a single resolution address for all popular OSS Java artifacts (see Section 24.6.2, "Maven JCenter repository").
This is a common reason why many projects prefer to store their libraries in their version control system. This approach is fully supported by Gradle. The libraries can be stored in a flat directory without any XML module descriptor files. Yet Gradle offers complete transitive dependency management. You can use either client module dependencies to express the dependency relations, or artifact dependencies in case a first level dependency has no transitive dependencies. People can check out such a project from your source code control system and have everything necessary to build it.
If you are working with a distributed version control system like Git you probably don't want to use the version control system to store libraries as people check out the whole history. But even here the flexibility of Gradle can make your life easier. For example, you can use a shared flat directory without XML descriptors and yet you can have full transitive dependency management, as described above.
You could also have a mixed strategy. If your main concern is bad metadata in the POM file and maintaining custom XML descriptors, then Client Modules offer an alternative. However, you can still use a Maven2 repo or your custom repository as a repository for jars only and still enjoy transitive dependency management. Or you can only provide client modules for POMs with bad metadata. For the jars and the correct POMs you still use the remote repository.
There is another way to deal with transitive dependencies without XML descriptor files. You can do this with Gradle, but we don't recommend it. We mention it for the sake of completeness and comparison with other build tools.
The trick is to use only artifact dependencies and group them in lists. This will directly express
your first level dependencies and your transitive dependencies (see Section 24.4.8, "Optional attributes").
The problem with this is that Gradle dependency management will see this as specifying all
dependencies as first level dependencies. The dependency reports won't show your real dependency
graph and the
compile task uses all dependencies, not just the first level
dependencies. All in all, your build is less maintainable and reliable than it could be when using
client modules, and you don't gain anything.
[11] Gradle supports partial multiproject builds (see Chapter 25, Multi-project Builds).
[12] http://books.sonatype.com/mvnref-book/reference/pom-relationships-sect-project-relationships.html
[13] At http://ant.apache.org/ivy/history/latest-milestone/concept.html you can learn more about ivy patterns.
[14] If you want to shield your project from the downtimes of Maven Central things get more complicated. You probably want to set-up a repository proxy for this. In an enterprise environment this is rather common. For an open source project it looks like overkill.
The powerful support for multi-project builds is one of Gradle's unique selling points. This topic is also the most intellectually challenging.
A multi-project build in gradle consists of one root project, and one or more subprojects that may also have subprojects.
While each subproject could configure itself in complete isolation of the other subprojects, it is common that subprojects share common traits. It is then usually preferable to share configurations among projects, so the same configuration affects several subprojects.
Let's start with a very simple multi-project build. Gradle is a general purpose build tool at its core, so the projects don't have to be Java projects. Our first examples are about marine life.
Section 21.1, "Build phases" describes the phases of every Gradle build.
Let's zoom into the configuration and execution phases of a multi-project build.
Configuration here means executing the
build.gradle file of a project, which
implies e.g. downloading all plugins that were declared using '
apply plugin'.
By default, the configuration of all projects happens before any task is executed.
This means that when a single task, from a single project is requested,
all projects of multi-project build are configured first.
The reason every project needs to be configured is to support
the flexibility of accessing and changing any part of the Gradle project model.
The Configuration injection feature and access to the complete project model are possible because every project is configured before the execution phase. Yet, this approach may not be the most efficient in a very large multi-project build. There are Gradle builds with a hierarchy of hundreds of subprojects. The configuration time of huge multi-project builds may become noticeable. Scalability is an important requirement for Gradle. Hence, starting from version 1.4 a new incubating 'configuration on demand' mode is introduced.
Configuration on demand mode attempts to configure only projects that are relevant for requested
tasks, i.e. it only executes the
build.gradle file of projects that are participating in the build.
This way, the configuration time of a large multi-project build can be reduced.
In the long term, this mode will become the default mode, possibly the only mode for Gradle build execution.
The configuration on demand feature is incubating so not every build is guaranteed to work correctly.
The feature should work very well for multi-project builds that have decoupled projects (Section 25.9, "Decoupled Projects").
In "configuration on demand" mode, projects are configured as follows:
Eager to try out this new feature? To configure on demand with every build run see Section 12.1, "Configuring the build environment via gradle.properties". To configure on demand just for a given build please see Appendix D, Gradle Command Line.
Let's look at some examples with the following project tree. This is a multi-project build with a root project
water and a subproject named
Example 25.1. Multi-project tree - water & bluewhale projects
Build layout
water/ build.gradle settings.gradle bluewhale/
Note: The code for this example can be found at
samples/userguide/multiproject/firstExample/water in the '-all' distribution of Gradle.
include 'bluewhale'
And where is the build script for the
bluewhale project? In Gradle build scripts are optional.
Obviously for a single project build, a project without a build script doesn't make much sense. For
multiproject builds the situation is different. Let's look at the build script for the
water project and
execute it:
Example 25.2. Build script of water (parent) project
Closure cl = { task -> println "I'm $task.project.name" } task('hello').doLast(cl) project(':bluewhale') { task('hello').doLast(cl) }
Output of
gradle -q hello
> gradle -q hello I'm water I'm bluewhale
Gradle allows you to access any project of the multi-project build from any build script. The Project
API provides a method called
project(), which takes a path as an argument and returns
the Project object for this path. The capability to configure a project build from any build script we
call cross project configuration. Gradle implements this via
configuration injection.
We are not that happy with the build script of the
water project. It is inconvenient to add the task
explicitly for every project. We can do better. Let's first add another project called
krill to our multi-project build.
Example 25.3. Multi-project tree - water, bluewhale & krill projects
Build layout
water/ build.gradle settings.gradle bluewhale/ krill/
Note: The code for this example can be found at
samples/userguide/multiproject/addKrill/water in the '-all' distribution of Gradle.
include 'bluewhale', 'krill'
Now we rewrite the
water build script and boil it down to a single line.
Example 25.4. Water project build script
allprojects {
task hello {
doLast { task ->
println "I'm $task.project.name"
Output of
gradle -q hello
> gradle -q hello I'm water I'm bluewhale I'm krill
Is this cool or is this cool? And how does this work? The Project API provides a property
which returns a list with the current project and all its subprojects underneath it. If you call
with a closure, the statements of the closure are delegated to the projects associated with
allprojects. You could also do an iteration via
allprojects.each, but
that would be more verbose.
Other build systems use inheritance as the primary means for defining common behavior. We also offer inheritance for projects as you will see later. But Gradle uses configuration injection as the usual way of defining common behavior. We think it provides a very powerful and flexible way of configuring multiproject builds.
Another possibilty for sharing configuration is to use a common external script. See Section 42.3, "Configuring the project using an external build script" for more information.
The Project API also provides a property for accessing the subprojects only.
Example 25.5. Defining common behavior of all projects and subprojects
allprojects { task hello { doLast { task -> println "I'm $task.project.name" } } } subprojects { hello { doLast { println "- I depend on water" } } }
Output of
gradle -q hello
> gradle -q hello I'm water I'm bluewhale - I depend on water I'm krill - I depend on water
You may notice that there are two code snippets referencing the "
hello" task.
The first one, which uses the "
task" keyword, constructs the task and provides it's base configuration.
The second piece doesn't use the "
task" keyword, as it is further configuring the existing "
hello" task.
You may only construct a task once in a project, but you may add any number of code blocks providing additional configuration.
You can add specific behavior on top of the common behavior. Usually we put the project specific
behavior in the build script of the project where we want to apply this specific behavior. But as we
have already seen, we don't have to do it this way. We could add project specific behavior for the
bluewhale project like this:
Example 25.6. Defining specific behaviour for particular project
allprojects { task hello { doLast { task -> println "I'm $task.project.name" } } } subprojects { hello { doLast { println "- I depend on water" } } } project(':bluewhale').hello { doLast { println "- I'm the largest animal that has ever lived on this planet." } }
Output of
gradle -q hello
> gradle -q hello I'm water I'm bluewhale - I depend on water - I'm the largest animal that has ever lived on this planet. I'm krill - I depend on water
As we have said, we usually prefer to put project specific behavior into the build script of this
project. Let's refactor and also add some project specific behavior to the
Example 25.7. Defining specific behaviour for project krill
Build layout
water/ build.gradle settings.gradle bluewhale/ build.gradle krill/ build.gradle
Note: The code for this example can be found at
samples/userguide/multiproject/spreadSpecifics/water in the '-all' distribution of Gradle.
include 'bluewhale', 'krill'
hello.doLast {
println "- I'm the largest animal that has ever lived on this planet."
hello.doLast {
println "- The weight of my species in summer is twice as heavy as all human beings."
allprojects { task hello { doLast { task -> println "I'm $task.project.name" } } } subprojects { hello { doLast { println "- I depend on water" } } }
Output of
gradle -q hello
> gradle -q hello I'm water I'm bluewhale - I depend on water - I'm the largest animal that has ever lived on this planet. I'm krill - I depend on water - The weight of my species in summer is twice as heavy as all human beings.
To show more of the power of configuration injection, let's add another project
tropicalFish and add more behavior to the build via the build script of the
water project.
Example 25.8. Adding custom behaviour to some projects (filtered by project name)
Build layout
water/ build.gradle settings.gradle bluewhale/ build.gradle krill/ build.gradle tropicalFish/
Note: The code for this example can be found at
samples/userguide/multiproject/addTropical/water in the '-all' distribution of Gradle.
include 'bluewhale', 'krill', 'tropicalFish'
allprojects { task hello { doLast { task -> println "I'm $task.project.name" } } } subprojects { hello { doLast { println "- I depend on water" } } } configure(subprojects.findAll {it.name != 'tropicalFish'}) { hello { doLast { println '- I love to spend time in the arctic waters.' } } }
Output of
gradle -q hello
> gradle -q hello I'm water I'm bluewhale - I depend on water - I love to spend time in the arctic waters. - I'm the largest animal that has ever lived on this planet. I'm krill - I depend on water - I love to spend time in the arctic waters. - The weight of my species in summer is twice as heavy as all human beings. I'm tropicalFish - I depend on water
configure() method takes a list as an argument and applies the
configuration to the projects in this list.
Using the project name for filtering is one option. Using extra project properties is another. (See Section 17.4.2, "Extra properties" for more information on extra properties.)
Example 25.9. Adding custom behaviour to some projects (filtered by project properties)
Build layout
water/ build.gradle settings.gradle bluewhale/ build.gradle krill/ build.gradle tropicalFish/ build.gradle
Note: The code for this example can be found at
samples/userguide/multiproject/tropicalWithProperties/water in the '-all' distribution of Gradle.
include 'bluewhale', 'krill', 'tropicalFish'
ext.arctic = true
hello.doLast {
println "- I'm the largest animal that has ever lived on this planet."
ext.arctic = true
hello.doLast {
println "- The weight of my species in summer is twice as heavy as all human beings."
ext.arctic = false
allprojects { task hello { doLast { task -> println "I'm $task.project.name" } } } subprojects { hello { doLast {println "- I depend on water"} afterEvaluate { Project project -> if (project.arctic) { doLast { println '- I love to spend time in the arctic waters.' } } } } }
Output of
gradle -q hello
> gradle -q hello I'm water I'm bluewhale - I depend on water - I'm the largest animal that has ever lived on this planet. - I love to spend time in the arctic waters. I'm krill - I depend on water - The weight of my species in summer is twice as heavy as all human beings. - I love to spend time in the arctic waters. I'm tropicalFish - I depend on water
In the build file of the
water project we use an
notification. This means that the closure we are passing gets evaluated after
the build scripts of the subproject are evaluated. As the property
is set in those build scripts, we have to do it this way. You will find more on this topic in
Section 25.6, "Dependencies - Which dependencies?"
When we executed the
hello task from the root project dir, things behaved in an
intuitive way. All the
hello tasks of the different projects were executed. Let's switch
to the
bluewhale dir and see what happens if we execute Gradle from there.
Example 25.10. Running build from subproject
Output of
gradle -q hello
> gradle -q hello I'm bluewhale - I depend on water - I'm the largest animal that has ever lived on this planet. - I love to spend time in the arctic waters.
The basic rule behind Gradle's behavior is simple. Gradle looks down the hierarchy, starting with the
current dir, for tasks with the name
and executes them. One thing is very important to note. Gradle
project of the multi-project build and creates all existing task objects. Then, according to the task name
arguments and the current dir, 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. We will have a closer look at this in the next section. Let's now have our last marine
example. Let's add a task to
bluewhale and
Example 25.11. Evaluation and execution of projects
ext.arctic = true hello { doLast { println "- I'm the largest animal that has ever lived on this planet." } } task distanceToIceberg { doLast { println '20 nautical miles' } }
ext.arctic = true hello { doLast { println "- The weight of my species in summer is twice as heavy as all human beings." } } task distanceToIceberg { doLast { println '5 nautical miles' } }
Output of
gradle -q distanceToIceberg
> gradle -q distanceToIceberg 20 nautical miles 5 nautical miles
Here's the output without the
-q option:
Example 25.12. Evaluation and execution of projects
Output of
gradle distanceToIceberg
> gradle distanceToIceberg :bluewhale:distanceToIceberg 20 nautical miles :krill:distanceToIceberg 5 nautical miles BUILD SUCCESSFUL Total time: 1 secs
The build is executed from the
water project. Neither
water nor
tropicalFish have a task with the name
distanceToIceberg. Gradle does
not care. The simple rule mentioned already above is: Execute all tasks down the hierarchy which have this
name. Only complain if there is no such task!
As we have seen, you can run a multi-project build by entering any subproject dir and execute the build from there. All matching task names of the project hierarchy starting with the current dir are executed. But Gradle also offers to execute tasks by their absolute path (see also Section 25.5, "Project and task paths"):
Example 25.13. Running tasks by their absolute path
Output of
gradle -q :hello :krill:hello hello
> gradle -q :hello :krill:hello hello I'm water I'm krill - I depend on water - The weight of my species in summer is twice as heavy as all human beings. - I love to spend time in the arctic waters. I'm tropicalFish - I depend on water
The build is executed from the
tropicalFish project. We execute the
tasks of the
water, the
krill and the
project. The first two tasks are specified by their absolute path, the last task is executed using the name
matching mechanism described above.
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.
The path of a task is simply its project path plus the task name, like "
:bluewhale:hello". Within a project you can address a task of the same project just by its name.
This is interpreted as a relative path.
The examples from the last section were special, as the projects had no Execution Dependencies. They had only Configuration Dependencies. The following sections illustrate the differences between these two types of dependencies.
Example 25.14. Dependencies and execution order
Build layout
messages/ settings.gradle consumer/ build.gradle producer/ build.gradle
Note: The code for this example can be found at
samples/userguide/multiproject/dependencies/firstMessages/messages in the '-all' distribution of Gradle.
include 'consumer', 'producer'
task action {
doLast {
println("Consuming message: ${rootProject.producerMessage}")
task action { doLast { println "Producing message:" rootProject.producerMessage = 'Watch the order of execution.' } }
Output of
gradle -q action
> gradle -q action Consuming message: null Producing message:
This didn't quite do what we want. If nothing else is defined, Gradle executes the task in alphanumeric order.
Therefore, Gradle will execute
before "
:producer:action". Let's try to solve this with a hack and
rename the producer project to "
Example 25.15. Dependencies and execution order
Build layout
messages/ settings.gradle aProducer/ build.gradle consumer/ build.gradle
include 'consumer', 'aProducer'
task action { doLast { println "Producing message:" rootProject.producerMessage = 'Watch the order of execution.' } }
task action {
doLast {
println("Consuming message: ${rootProject.producerMessage}")
Output of
gradle -q action
> gradle -q action Producing message: Consuming message: Watch the order of execution.
We can show where this hack doesn't work if we now switch to the
consumer dir and
execute the build.
Example 25.16. Dependencies and execution order
Output of
gradle -q action
> gradle -q action Consuming message: null
The problem is that the two
tasks are unrelated. If you execute the build from the
project Gradle executes them both because they have the same name and they are down the hierarchy.
In the last example only one
action" task
was down the hierarchy and therefore it was the only task that was executed. We need something
better than this hack.
Example 25.17. Declaring dependencies
Build layout
messages/ settings.gradle consumer/ build.gradle producer/ build.gradle
Note: The code for this example can be found at
samples/userguide/multiproject/dependencies/messagesWithDependencies/messages in the '-all' distribution of Gradle.
include 'consumer', 'producer'
task action(dependsOn: ":producer:action") { doLast { println("Consuming message: ${rootProject.producerMessage}") } }
task action { doLast { println "Producing message:" rootProject.producerMessage = 'Watch the order of execution.' } }
Output of
gradle -q action
> gradle -q action Producing message: Consuming message: Watch the order of execution.
Running this from the
consumer directory gives:
Example 25.18. Declaring dependencies
Output of
gradle -q action
> gradle -q action Producing message: Consuming message: Watch the order of execution.
This is now working better because we have declared that the
action" task in the "
project has an
execution dependency
on the "
action" task in the
Of course, task dependencies across different projects are not limited to tasks with the same name. Let's change the naming of our tasks and execute the build.
Example 25.19. Cross project task dependencies
task consume(dependsOn: ':producer:produce') { doLast { println("Consuming message: ${rootProject.producerMessage}") } }
task produce { doLast { println "Producing message:" rootProject.producerMessage = 'Watch the order of execution.' } }
Output of
gradle -q consume
> gradle -q consume Producing message: Consuming message: Watch the order of execution.
Let's see one more example with our producer-consumer build before we enter
land. We add a property to the "
producer" project and create a configuration time dependency from
consumer" to "
Example 25.20. Configuration time dependencies
def message = rootProject.producerMessage
task consume {
doLast {
println("Consuming message: " + message)
rootProject.producerMessage = 'Watch the order of evaluation.'
Output of
gradle -q consume
> gradle -q consume Consuming message: null
The default
order of projects is alphanumeric (for the same nesting level). Therefore the
project is evaluated before the
project and the
value is set
it is read by the
project. Gradle offers a solution for this.
Example 25.21. Configuration time dependencies - evaluationDependsOn
evaluationDependsOn(':producer') def message = rootProject.producerMessage task consume { doLast { println("Consuming message: " + message) } }
Output of
gradle -q consume
> gradle -q consume Consuming message: Watch the order of evaluation.
The use of the
evaluationDependsOn" command
results in the evaluation of
the "
producer" project
before the
consumer" project
is evaluated. This example is a bit contrived to show the mechanism. In
case there would be an easier solution by reading the key property at execution time.
Example 25.22. Configuration time dependencies
task consume {
doLast {
println("Consuming message: ${rootProject.producerMessage}")
Output of
gradle -q consume
> gradle -q consume Consuming message: Watch the order of evaluation.
Configuration dependencies are very different from execution dependencies. Configuration dependencies are between projects whereas execution dependencies are always resolved to task dependencies. Also note that all projects are always configured, even when you start the build from a subproject. The default configuration order is top down, which is usually what is needed.
To change the default configuration order to "bottom up", use the "
evaluationDependsOnChildren()" method instead.
On the same nesting level the configuration order depends on the alphanumeric position. The most
common use case is to have multi-project builds that share a common lifecycle (e.g. all projects use the
Java plugin). If you declare with
execution dependency
between different projects, the default behavior of this method is to also create a
dependency between the two projects. Therefore it is likely that you don't have to define configuration
dependencies explicitly.
Gradle's multi-project features are driven by real life use cases. One good example consists of two web application projects and a parent project that creates a distribution including the two web applications. [15] For the example we use only one build script and do cross project configuration.
Example 25.23. Dependencies - real life example - crossproject configuration
Build layout
webDist/ settings.gradle build.gradle date/ src/main/java/ org/gradle/sample/ DateServlet.java hello/ src/main/java/ org/gradle/sample/ HelloServlet.java
Note: The code for this example can be found at
samples/userguide/multiproject/dependencies/webDist in the '-all' distribution of Gradle.
include 'date', 'hello'
allprojects { apply plugin: 'java' group = 'org.gradle.sample' version = '1.0' } subprojects { apply plugin: 'war' repositories { mavenCentral() } dependencies { compile "javax.servlet:servlet-api:2.5" } } task explodedDist(type: Copy) { into "$buildDir/explodedDist" subprojects { from tasks.withType(War) } }
We have an interesting set of dependencies. Obviously the
projects have a
dependency on
webDist, as all the build logic for the webapp projects is injected by
webDist. The
dependency is in the other direction, as
depends on the build artifacts of
hello. There is even a third dependency.
has a
dependency on
because it needs to know the
archivePath. But it asks for this information at
execution time. Therefore we have no circular dependency.
Such dependency patterns are daily bread in the problem space of multi-project builds. If a build system does not support these patterns, you either can't solve your problem or you need to do ugly hacks which are hard to maintain and massively impair your productivity as a build master.
What if one project needs the jar produced by another project in its compile path, and not just the jar but also the transitive dependencies of this jar? Obviously this is a very common use case for Java multi-project builds. As already mentioned in Section 24.4.3, "Project dependencies", Gradle offers project lib dependencies for this.
Example 25.24. Project lib dependencies
Build layout
java/ settings.gradle build.gradle api/ src/main/java/ org/gradle/sample/ api/ Person.java apiImpl/ PersonImpl.java services/personService/ src/ main/java/ org/gradle/sample/services/ PersonService.java test/java/ org/gradle/sample/services/ PersonServiceTest.java shared/ src/main/java/ org/gradle/sample/shared/ Helper.java
Note: The code for this example can be found at
samples/userguide/multiproject/dependencies/java in the '-all' distribution of Gradle.
We have the projects "
shared", "
api" and "
The "
personService" project has a lib dependency on the other two projects. The "
api" project
has a lib dependency on the "
shared" project.
Example 25.25. Project lib dependencies
include 'api', 'shared', 'services:personService'
subprojects { apply plugin: 'java' group = 'org.gradle.sample' version = '1.0' repositories { mavenCentral() } dependencies { testCompile "junit:junit:4.12" } } project(':api') { dependencies { compile project(':shared') } } project(':services:personService') { dependencies { compile project(':shared'), project(':api') } }
All the build logic is in the
build.gradle" file of the root project.
A "lib"
dependency is a special form of an execution dependency. It causes the other project to be built first and
adds the jar with the classes of the other project to the classpath. It also adds the dependencies of the
other project to the classpath. So you can enter the
directory and trigger a "
gradle compile". First
the "
shared" project
is built and then
the "
api" project
is built. Project dependencies enable partial multi-project builds.
If you come from Maven land you might be perfectly happy with this. If you come from Ivy land, you might expect some more fine grained control. Gradle offers this to you:
Example 25.26. Fine grained control over dependencies
subprojects { apply plugin: 'java' group = 'org.gradle.sample' version = '1.0' } project(':api') { configurations { spi } dependencies { compile project(':shared') } task spiJar(type: Jar) { baseName = 'api-spi' dependsOn classes from sourceSets.main.output include('org/gradle/sample/api/**') } artifacts { spi spiJar } } project(':services:personService') { dependencies { compile project(':shared') compile project(path: ':api', configuration: 'spi') testCompile "junit:junit:4.12", project(':api') } }
The Java plugin adds per default a jar to your project libraries which contains all the classes. In this
example we create an
library containing only the interfaces of the
project. We assign this library to a new dependency configuration. For the person
service we declare that the project should be compiled only against the
interfaces but tested with all classes from "
With more and more CPU cores available on developer desktops and CI servers, it is important that Gradle is able to fully utilise these processing resources. More specifically, the parallel execution attempts to:
Although Gradle already offers parallel test execution via
the feature described in this section is parallel execution at a project level.
Parallel execution is an incubating feature. Please use it and let us know how it works for you.
Parallel project execution allows the separate projects in a decoupled multi-project build to be executed in parallel (see also: Section 25.9, "Decoupled Projects"). While parallel execution does not strictly require decoupling at configuration time, the long-term goal is to provide a powerful set of features that will be available for fully decoupled projects. Such features include:
How does parallel execution work? First, you need to tell Gradle to use the parallel mode. You can use the command line argument (Appendix D, Gradle Command Line) or configure your build environment (Section 12.1, "Configuring the build environment via gradle.properties"). Unless you provide a specific number of parallel threads Gradle attempts to choose the right number based on available CPU cores. Every parallel worker exclusively owns a given project while executing a task. This means that 2 tasks from the same project are never executed in parallel. Therefore only multi-project builds can take advantage of parallel execution. Task dependencies are fully supported and parallel workers will start executing upstream tasks first. Bear in mind that the alphabetical scheduling of decoupled tasks, known from the sequential execution, does not really work in parallel mode. You need to make sure the task dependencies are declared correctly to avoid ordering issues.
Warning: Be aware that task ordering is not strictly enforced when using parallel execution and can lead to unexpected results. A common case that surfaces this
limitation is the use of the
clean task provided by the
base plugin in combination with any other task producing an output for a multi-project build
if executed in parallel. For example let us assume a multi-project build with project A and project B where B depends on A. Running
gradle clean build --parallel
could lead to the following situation:
A:clean is executed after
B:classes fails as it was executed after
A:clean which deleted the JAR
file B depends on for compilation.
Furthermore, the tasks
clean and
classes could run at the same time and delete files that are needed for compilation across project boundaries. Gradle
emits a warning for those situations. Future versions of Gradle will provide an appropriate fix.
Gradle allows any project to access any other project during both the configuration and execution phases. While this provides a great deal of power and flexibility to the build author, it also limits the flexibility that Gradle has when building those projects. For instance, this effectively prevents Gradle from correctly building multiple projects in parallel, configuring only a subset of projects, or from substituting a pre-built artifact in place of a project dependency.
Two projects are said to be decoupled if they do not directly access each other's project model. Decoupled projects may only interact in terms of declared dependencies: project dependencies (Section 24.4.3, "Project dependencies") and/or task dependencies (Section 15.5, "Task dependencies"). Any other form of project interaction (i.e. by modifying another project object or by reading a value from another project object) causes the projects to be coupled. The consequence of coupling during the configuration phase is that if gradle is invoked with the 'configuration on demand' option, the result of the build can be flawed in several ways. The consequence of coupling during execution phase is that if gradle is invoked with the parallel option, one project task runs too late to influence a task of a project building in parallel. Gradle does not attempt to detect coupling and warn the user, as there are too many possibilities to introduce coupling.
A very common way for projects to be coupled is by using configuration injection (Section 25.1, "Cross project configuration"). It may not be immediately apparent, but using key
Gradle features like the
allprojects and
subprojects keywords automatically cause your projects to be coupled.
This is because these keywords are used in a
build.gradle file, which defines a project. Often this is a "root project" that does nothing more than
define common configuration, but as far as Gradle is concerned this root project is still a fully-fledged project, and by using
that project is effectively coupled to all other projects. Coupling of the root project to subprojects does not impact 'configuration on demand', but using the
allprojects and
subprojects in any subproject's
build.gradle file will have an impact.
This means that using any form of shared build script logic or configuration injection (
subprojects, etc.)
will cause your projects to be coupled. As we extend the concept of project decoupling and provide features that take advantage of decoupled projects,
we will also introduce new features to help you to solve common use cases (like configuration injection) without causing your projects to be coupled.
In order to make good use of cross project configuration without running into issues for parallel and 'configuration on demand' options, follow these recommendations:
build.gradle referencing other subprojects; prefering cross configuration from the root project.
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
buildDependents tasks can help with this.
Look at Example 25.25, "Project lib dependencies". In this
example, the "
:services:personservice" 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.
build task does this.
Example 25.27. Build and Test Single Project
Output of
gradle :api:build
> gradle :api:build :shared:compileJava :shared:processResources :shared:classes :shared:jar :api:compileJava :api:processResources :api:classes :api:jar :api:assemble :api:compileTestJava :api:processTestResources :api:testClasses :api:test :api:check :api:build BUILD SUCCESSFUL Total time: 1 secs
While you are working in a typical development cycle repeatedly building and testing changes to
the "
:api" project (knowing that you are only changing files in this one project), you may not want to
even suffer the expense of building "
:shared:compile" to see what has changed in the "
:shared" project.
Adding the "
-a" option will cause Gradle to use cached jars to resolve any project lib
dependencies and not try to re-build the depended on projects.
Example 25.28. Partial Build and Test Single Project
Output of
gradle -a :api:build
> gradle -a :api:build :api:compileJava :api:processResources :api:classes :api:jar :api:assemble :api:compileTestJava :api:processTestResources :api:testClasses :api:test :api:check :api:build BUILD SUCCESSFUL Total time: 1 secs
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 lib dependencies of the testRuntime configuration.
Example 25.29. Build and Test Depended On Projects
Output of
gradle :api:buildNeeded
> gradle :api:buildNeeded :shared:compileJava :shared:processResources :shared:classes :shared:jar :api:compileJava :api:processResources :api:classes :api:jar :api:assemble :api:compileTestJava :api:processTestResources :api:testClasses :api:test :api:check :api:build :shared:assemble :shared:compileTestJava :shared:processTestResources :shared:testClasses :shared:test :shared:check :shared:build :shared:buildNeeded :api:buildNeeded BUILD SUCCESSFUL Total time: 1 secs
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 "
project, you also need to test all projects that depend on the "
:api" project.
buildDependents task also tests all the projects that have a project lib dependency
(in the testRuntime configuration) on the specified project.
Example 25.30. Build and Test Dependent Projects
Output of
gradle :api:buildDependents
> gradle :api:buildDependents :shared:compileJava :shared:processResources :shared:classes :shared:jar :api:compileJava :api:processResources :api:classes :api:jar :api:assemble :api:compileTestJava :api:processTestResources :api:testClasses :api:test :api:check :api:build :services:personService:compileJava :services:personService:processResources :services:personService:classes :services:personService:jar :services:personService:assemble :services:personService:compileTestJava :services:personService:processTestResources :services:personService:testClasses :services:personService:test :services:personService:check :services:personService:build :services:personService:buildDependents :api:buildDependents BUILD SUCCESSFUL Total time: 1 secs
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.
Section 42.4, "Build sources in the
buildSrc project" tells us that we can place build logic to be compiled and tested in the special
buildSrc directory.
In a multi project build, there can only be one
buildSrc directory which must be located in the root directory.
Properties and methods declared in a project are inherited to all its subprojects. This is an alternative to configuration injection. But we think that the model of inheritance does not reflect the problem space of multi-project builds very well. In a future edition of this user guide we might write more about this.
Method inheritance might be interesting to use as Gradle's Configuration Injection does not support methods yet (but will in a future release).
You might be wondering why we have implemented a feature we obviously don't like that much. One reason is that it is offered by other tools and we want to have the check mark in a feature comparison :). And we like to offer our users a choice.
Writing this chapter was pretty exhausting and reading it might have a similar effect. Our final message
for this chapter is that multi-project builds with Gradle are usually
difficult. There are five elements you need to remember:
and project lib dependencies.
With those elements, and keeping in mind that Gradle has a distinct configuration and execution phase, you
already have a lot of flexibility. But when you enter steep territory Gradle does not become an obstacle and
usually accompanies and carries you to the top of the mountain.
[15] The real use case we had, was using http://lucene.apache.org/solr, where you need a separate war for each index you are accessing. That was one reason why we have created a distribution of webapps. The Resin servlet container allows us, to let such a distribution point to a base installation of the servlet container.
is also a project, but we use it just as a container. It has no build script and gets nothing
injected by another build script.
[17] We do this here, as it makes the layout a bit easier. We usually put the project specific stuff into the build script of the respective projects.
[18] So we are well in the range of the 7 plus 2 Rule :)
Gradle at its core intentionally provides very little for real world automation. All of the useful
features, like the ability to compile Java code, are added by plugins.
Plugins add new tasks (e.g.
JavaCompile), domain objects (e.g.
SourceSet), conventions (e.g. Java source is located at
src/main/java) as well as extending core objects and objects from other plugins.
In this chapter we discuss how to use plugins and the terminology and concepts surrounding plugins.
Applying a plugin to a project allows the plugin to extend the project's capabilities. It can do things such as:
By applying plugins, rather than adding logic to the project build script, we can reap a number of benefits. Applying plugins:
There are two general types of plugins in Gradle, script plugins and binary plugins.
Script plugins are additional build scripts that further configure the build and usually implement a declarative approach to
manipulating the build. They are typically used within a build although they can be externalized and accessed from a remote
location. Binary plugins are classes that implement the
Plugin interface and adopt a programmatic
approach to manipulating the build. Binary plugins can reside within a build script, within the project hierarchy or externally
in a plugin jar.
A plugin often starts out as a script plugin (because they are easy to write) and then, as the code becomes more valuable, it's migrated to a binary plugin that can be easily tested and shared between multiple projects or organizations.
To use the build logic encapsulated in a plugin, Gradle needs to perform two steps. First, it needs to resolve the
plugin, and then it needs to apply the plugin to the target, usually a
Resolving a plugin means finding the correct version of the jar which contains a given plugin and adding it the script classpath. Once a plugin is resolved, its API can be used in a build script. Script plugins are self-resolving in that they are resolved from the specific file path or URL provided when applying them. Core binary plugins provided as part of the Gradle distribution are automatically resolved.
Applying a plugin means actually executing the plugin's
on the Project you want to enhance with the plugin. Applying plugins is idempotent. That is, you can safely apply
any plugin multiple times without side effects.
The most common use case for using a plugin is to both resolve the plugin and apply it to the current project. Since this is such a common use case, it's recommended that build authors use the plugins DSL to both resolve and apply plugins in one step. The feature is technically still incubating, but it works well, and should be used by most users.
Script plugins are automatically resolved and can be applied from a script on the local filesystem or at a remote location. Filesystem locations are relative to the project directory, while remote script locations are specified with an HTTP URL. Multiple script plugins (of either form) can be applied to a given target.
You apply plugins by their plugin id, which is a globally unique identifier, or name, for plugins.
Core Gradle plugins are special in that they provide short names, such as
'java' for the core
JavaPlugin. All other binary plugins must use the fully qualified form of
the plugin id (e.g.
com.github.foo.bar), although some legacy plugins may still utilize a short, unqualified
form. Where you put the plugin id depends on whether you are using the plugins DSL or
the buildscript block.
A plugin is simply any class that implements the
Plugin interface. Gradle provides
the core plugins (e.g.
JavaPlugin) as part of its distribution which means they are automatically resolved.
However, non-core binary plugins need to be resolved before they can be applied. This can be achieved in a number of ways:
buildSrc project").
For more on defining your own plugins, see Chapter 40, Writing Custom Plugins.
The plugins DSL is currently incubating. Please be aware that the DSL and other configuration may change in later Gradle versions.
The new plugins DSL provides a succinct and convenient way to declare plugin dependencies. It works with the
Gradle plugin portal to provide easy access to both core and community
plugins. The plugins DSL block configures an instance of
To apply a core plugin, the short name can be used:
To apply a community plugin from the portal, the fully qualified plugin id must be used:
Example 26.3. Applying a community plugin
plugins { id "com.jfrog.bintray" version "0.4.1" }
PluginDependenciesSpec for more information on using the Plugin DSL.
This way of adding plugins to a project is much more than a more convenient syntax. The plugins DSL is processed in a way which allows Gradle to determine the plugins in use very early and very quickly. This allows Gradle to do smart things such as:
This requires that plugins be specified in a way that Gradle can easily and quickly extract, before executing the rest of the build script. It also requires that the definition of plugins to use be somewhat static.
There are some key differences between the new plugin mechanism and the "traditional"
apply() method
mechanism. There are also some constraints, some of which are temporary limitations while the mechanism is still
being developed and some are inherent to the new approach.
The new
plugins {} block does not support arbitrary Groovy code. It is constrained, in order to be idempotent
(produce the same result every time) and side effect free (safe for Gradle to execute at any time).
The form is:
plugins { id «plugin id» version «plugin version» [apply «false»] }
«plugin version» and
«plugin id» must be constant, literal, strings and
apply statement with a
boolean can be used to disable the default behavior
of applying the plugin immediately (e.g. you want to apply it only in
subprojects). No other statements
are allowed; their presence will cause a compilation error.
plugins {} block must also be a top level statement in the buildscript. It cannot be nested inside
another construct (e.g. an if-statement or for-loop).
plugins {} block can currently only be used in a project's build script. It cannot be used in
script plugins, the settings.gradle file or init scripts.
Future versions of Gradle will remove this restriction.
If the restrictions of the new syntax are prohibitive, the recommended approach is to apply plugins using the buildscript {} block.
If you have a multi-project build, you probably want to apply plugins to some or
all of the subprojects in your build, but not to the
root or
master project. The default
behavior of the
plugins {} block is to immediately
resolve and
apply the plugins. But, you can use the
apply false syntax to tell Gradle not
to apply the plugin to the current project and then use
apply plugin: «plugin version» in the
subprojects block:
Example 26.4. Applying plugins only on certain subprojects.
include 'helloA' include 'helloB' include 'goodbyeC'
plugins { id "org.gradle.sample.hello" version "1.0.0" apply false id "org.gradle.sample.goodbye" version "1.0.0" apply false } subprojects { subproject -> if (subproject.name.startsWith("hello")) { apply plugin: 'org.gradle.sample.hello' } if (subproject.name.startsWith("goodbye")) { apply plugin: 'org.gradle.sample.goodbye' } }
If you then run
gradle hello you'll see that only the helloA and helloB subprojects had the hello plugin
gradle/subprojects/docs/src/samples/plugins/multiproject $> gradle hello Parallel execution is an incubating feature. :helloA:hello :helloB:hello Hello! Hello! BUILD SUCCESSFUL
pluginRepositories {} DSL is currently incubating.
Please be aware that the DSL and other configuration may change in later Gradle versions.
By default, the
plugins {} DSL resolves plugins from the public Gradle Plugin Portal.
Many build authors would also like to resolve plugins from private Maven or Ivy repositories because the plugins contain proprietary implementation
details, or just to have more control over what plugins are available to their builds.
To specify custom plugin repositories, add a
pluginRepositories {} block to the
settings.gradle file:
Example 26.5. Using plugins from custom plugin repositories.
pluginRepositories { maven { url 'maven-repo' } gradlePluginPortal() ivy { url 'ivy-repo' } }
This tells Gradle to first look in the Maven repository at
maven-repo when resolving plugins and then to
check the Gradle Plugin Portal if the plugins are not found in the Maven repository. If you don't want the Gradle Plugin Portal to be searched,
omit the
gradlePluginPortal() line. Finally, the Ivy repository at
ivy-repo will be checked.
pluginRepositories {} block may only appear in the
settings.gradle file, and must be the first block in the file.
Custom Maven and Ivy plugin repositories must contain plugin marker artifacts in addition to the artifacts
which actually implement the plugin. For more information on publishing plugins to custom repositories read Chapter 41, The Java Gradle Plugin Development Plugin.
PluginRepositoriesSpec for complete documentation for using the
pluginRepositories {} block.
Since the
plugins {} DSL block only allows for declaring plugins by their globally unique plugin
id and
version properties, Gradle needs a way to look up the coordinates of the plugin implementation artifact. To do so,
Gradle will look for a Plugin Marker Artifact with the coordinates
This marker needs to have a dependency on the actual plugin implementation. Publishing these markers is automated by the
For example, the following complete sample from the
sample-plugins project shows how to publish a
org.gradle.sample.hello plugin and a
org.gradle.sample.goodbye plugin to both an Ivy and Maven repository
using the combination of the java-gradle-plugin, the
maven-publish plugin, and the ivy-publish plugin.
Example 26.6. Complete Plugin Publishing Sample
plugins { id 'java-gradle-plugin' id 'maven-publish' id 'ivy-publish' } group 'org.gradle.sample' version '1.0.0' gradlePlugin { plugins { hello { id = "org.gradle.sample.hello" implementationClass = "org.gradle.sample.hello.HelloPlugin" } goodbye { id = "org.gradle.sample.goodbye" implementationClass = "org.gradle.sample.goodbye.GoodbyePlugin" } } } publishing { repositories { maven { url "../consuming/maven-repo" } ivy { url "../consuming/ivy-repo" } } }
gradle publish in the sample directory causes the following repo layouts to exist:
With the introduction of the plugins DSL, users should have little reason to use the legacy method of applying plugins. It is documented here in case a build author cannot use the plugins DSL due to restrictions in how it currently works.
Plugins can be applied using a plugin id. In the above case, we are
using the short name '
java' to apply the
Rather than using a plugin id, plugins can also be applied by simply specifying the class of the plugin:
JavaPlugin symbol in the above sample refers to the the
This class does not strictly need to be imported as the
org.gradle.api.plugins package is automatically imported in all build scripts
(see Section 17.8, "Default imports"). Furthermore, it is not necessary to append
.class to identify a class literal in Groovy as it is in Java.
Binary plugins that have been published as external jar files can be added to a project by adding the plugin to the
build script classpath and then applying the plugin. External jars can be added to the build script classpath
using the
buildscript {} block as described in Section 42.6, "External dependencies for the build script".
Example 26.9. Applying a plugin with the buildscript block
buildscript { repositories { jcenter() } dependencies { classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:0.4.1" } } apply plugin: "com.jfrog.bintray"
Gradle has a vibrant community of plugin developers who contribute plugins for a wide variety of capabilities. The Gradle plugin portal provides an interface for searching and exploring community plugins.
This chapter aims to serve as an introduction to plugins and Gradle and the role they play. For more information on the inner workings of plugins, see Chapter 40, Writing Custom Plugins.
There are a number of plugins included in the Gradle distribution. These are listed below.
These plugins add support for various languages which can be compiled for and executed in the JVM.
Table 27.1. Language plugins
|Plugin Id
|Automatically applies
|Works with
Adds Java compilation, testing and bundling capabilities to a project. It serves as the basis for many of the other Gradle plugins. See also Chapter 45, Java Quickstart.
Adds support for building Groovy projects. See also Chapter 53, Groovy Quickstart.
Adds support for building Scala projects.
Adds support for generating parsers using Antlr.
These plugins add support for various languages:
Table 27.2. Language plugins
|Plugin Id
|Automatically applies
|Works with
Adds native assembly language capabilities to a project.
Adds C source compilation capabilities to a project.
Adds C++ source compilation capabilities to a project.
Adds Objective-C source compilation capabilities to a project.
Adds Objective-C++ source compilation capabilities to a project.
Adds support for including Windows resources in native binaries.
These plugins provide some integration with various runtime technologies.
Table 27.3. Integration plugins
|Plugin Id
|Automatically applies
|Works with
Adds tasks for running and bundling a Java project as a command-line application.
Adds support for building J2EE applications.
Deploys your web application to a Jetty web container embedded in the build. See also Chapter 47, Web Application Quickstart. This plugin is deprecated and will be removed in Gradle 4.0. Consider using the more feature-rich Gretty plugin instead.
Adds support for publishing artifacts to Maven repositories.
Adds support for building OSGi bundles.
Adds support for assembling web application WAR files. See also Chapter 47, Web Application Quickstart.
These plugins provide some integration with various runtime technologies.
Table 27.4. Incubating integration plugins
|Plugin Id
|Automatically applies
|Works with
Adds support for building ZIP and TAR distributions.
Adds support for building ZIP and TAR distributions for a Java library.
This plugin provides a new DSL to support publishing artifacts to Ivy repositories, which improves on the existing DSL.
This plugin provides a new DSL to support publishing artifacts to Maven repositories, which improves on the existing DSL.
These plugins provide help with your software development process.
Table 27.5. Software development plugins
|Plugin Id
|Automatically applies
|Works with
Publish messages to your favourite platforms, such as Twitter or Growl.
Sends local announcements to your desktop about interesting events in the build lifecycle.
Performs quality checks on your project's Java source files using Checkstyle and generates reports from these checks.
Performs quality checks on your project's Groovy source files using CodeNarc and generates reports from these checks.
Generates files that are used by Eclipse IDE, thus making it possible to import the project into Eclipse. See also Chapter 45, Java Quickstart.
Does the same as the eclipse plugin plus generates eclipse WTP (Web Tools Platform) configuration files. After importing to eclipse your war/ear projects should be configured to work with WTP. See also Chapter 45, Java Quickstart.
Performs quality checks on your project's Java source files using FindBugs and generates reports from these checks.
Generates files that are used by Intellij IDEA IDE, thus making it possible to import the project into IDEA.
Performs quality checks on your project's source files using JDepend and generates reports from these checks.
Performs quality checks on your project's Java source files using PMD and generates reports from these checks.
Generates reports containing useful information about your Gradle build.
Adds the ability to digitally sign built files and artifacts.
These plugins provide help with your software development process.
Table 27.6. Software development plugins
|Plugin Id
|Automatically applies
|Works with
Generates build dashboard report.
Adds support for initializing a new Gradle build. Handles converting a Maven build to a Gradle build.
Adds support for running CUnit tests.
Provides integration with the JaCoCo code coverage library for Java.
|native language plugins
Adds integration with Visual Studio.
Adds a
Assists with development of Gradle plugins by providing standard plugin build configuration and validation.
These plugins form the basic building blocks which the other plugins are assembled from. They are available for you to use in your build files, and are listed here for completeness. However, be aware that they are not yet considered part of Gradle's public API. As such, these plugins are not documented in the user guide. You might refer to their API documentation to learn more about them.
Table 27.7. Base plugins
|Plugin Id
Adds the standard lifecycle tasks and configures reasonable defaults for the archive tasks:
Adds the source sets concept to the project. Does not add any particular source sets.
Adds the Groovy source sets concept to the project.
Adds the Scala source sets concept to the project.
Adds some shared convention properties to the project, relating to report generation.
You can find a list of external plugins at the Gradle Plugins site.
The Project report plugin adds some tasks to your project which generate reports containing useful
information about your build. These tasks generate the same content that you get by executing the
dependencies, and
properties tasks from the command line (see Section 4.7, "Obtaining information about your build").
In contrast to the command line reports, the report plugin generates the reports into a file. There is also an
aggregating task that depends on all report tasks added by the plugin.
We plan to add much more to the existing reports and create additional ones in future releases of Gradle.
To use the Project report plugin, include the following in your build script:
apply plugin: 'project-report'
The project report plugin defines the following tasks:
Table 28.1. Project report plugin - tasks
|Task name
|Depends on
|Generates the project dependency report.
|Generates an HTML dependency and dependency insight report for the project or a set of projects.
|Generates the project property report.
|Generates the project task report.
|Generates all project reports.
The project report defines the following convention properties:
Table 28.2. Project report plugin - convention properties
|Property name
|Default value
|The name of the directory to generate reports into, relative to the build directory.
File (read-only)
|The directory to generate reports into.
|A one element set with the project the plugin was applied to.
|The projects to generate the reports for.
|The name of the directory to generate the project report into, relative to the reports directory.
File (read-only)
|The directory to generate the project report into.
These convention properties are provided by a convention object of type
The build dashboard plugin is currently incubating. Please be aware that the DSL and other configuration may change in later Gradle versions.
The Build Dashboard plugin can be used to generate a single HTML dashboard that provides a single point of access to all of the reports generated by a build.
To use the Build Dashboard plugin, include the following in your build script:
Applying the plugin adds the
buildDashboard task to your project.
The task aggregates the reports for all tasks that implement the
interface from all projects in the build.
It is typically only applied to the root project.
buildDashboard task does not depend on any other tasks. It will only aggregate the reporting tasks that are independently
being executed as part of the build run. To generate the build dashboard, simply include this task in the list of tasks to execute.
For example, "
gradle buildDashboard build" will generate a dashboard for all of the reporting tasks that are dependents of the
build task.
The Build Dashboard plugin adds the following task to the project:
Table 29.1. Build Dashboard plugin - tasks
|Task name
|Depends on
|Generates build dashboard report.
The Build Dashboard plugin does not define any dependency configurations.
You can influence the location of build dashboard plugin generation via
Build comparison support is an incubating feature. This means that it is incomplete and not yet at regular Gradle production quality. This also means that this Gradle User Guide chapter is a work in progress.
Gradle provides support for comparing the outcomes (e.g. the produced binary archives) of two builds. There are several reasons why you may want to compare the outcomes of two builds. You may want to compare:
A build with a newer version of Gradle than it's currently using (i.e. upgrading the Gradle version).
A Gradle build with a build executed by another tool such as Apache Ant, Apache Maven or something else (i.e. migrating to Gradle).
The same Gradle build, with the same version, before and after a change to the build (i.e. testing build changes).
By comparing builds in these scenarios you can make an informed decision about the Gradle upgrade, migration to Gradle or build change by understanding the differences in the outcomes. The comparison process produces a HTML report outlining which outcomes were found to be identical and identifying the differences between non-identical outcomes.
The following are the terms used for build comparison and their definitions.
In the context of build comparison, a build is not necessarily a Gradle build. It can be any invokable "process" that produces observable "outcomes". At least one of the builds in a comparison will be a Gradle build.
Something that happens in an observable manner during a build, such as the creation of a zip file or test execution. These are the things that are compared.
The build that comparisons are being made against, typically the build in its "current" state. In other words, the left hand side of the comparison.
The build that is being compared to the source build, typically the "proposed" build. In other words, the right hand side of the comparison.
The Gradle build that executes the comparison process. It may be the same project as either the "target" or "source" build or may be a completely separate project. It does not need to be the same Gradle version as the "source" or "target" builds. The host build must be run with Gradle 1.2 or newer.
Build outcomes that are intended to be logically equivalent in the "source" and "target" builds, and are therefore meaningfully comparable.
A build outcome is uncompared if a logical equivalent from the other build cannot be found (e.g. a build produces a zip file that the other build does not).
A build outcome that cannot be understood by the host build. This can occur when the source or target build is a newer Gradle version than the host build and that Gradle version exposes new outcome types. Unknown build outcomes can be compared in so far as they can be identified to be logically equivalent to an unknown build outcome in the other build, but no meaningful comparison of what the build outcome actually is can be performed. Using the latest Gradle version for the host build will avoid encountering unknown build outcomes.
As this is an incubating feature, a limited set of the eventual functionality has been implemented at this time.
Only support for comparing Gradle builds is available at this time.
Both the source and target build must execute with Gradle newer or equal to version
The host build must be at least version
Future versions will provide support for executing builds from other build systems such as Apache Ant or Apache Maven, as well as support for executing arbitrary processes (e.g. shell script based builds)
Only support for comparing build outcomes that are
zip archives is supported at this time.
This includes
war and
ear archives.
Future versions will provide support for comparing outcomes such as test execution (i.e. which tests were executed, which tests failed, etc.)
compare-gradle-builds plugin can be used to facilitate a comparison between two Gradle builds. The plugin
adds a
CompareGradleBuilds task named "
to the project. The configuration of this task specifies what is to be compared. By default, it is configured to compare the current build with itself
using the current Gradle version by executing the tasks: "
clean assemble".
apply plugin: 'compare-gradle-builds'
This task can be configured to change what is compared.
compareGradleBuilds { sourceBuild { projectDir "/projects/project-a" gradleVersion "1.1" } targetBuild { projectDir "/projects/project-b" gradleVersion "1.2" } }
The example above specifies a comparison between two different projects using two different Gradle versions.
You can use the build comparison functionality to very quickly try a new Gradle version with your build.
To try your current build with a different Gradle version, simply add the following to the
build.gradle of the root project.
apply plugin: 'compare-gradle-builds' compareGradleBuilds { targetBuild.gradleVersion = "«gradle version»" }
Then simply execute the compareGradleBuilds task. You will see the console output of the "source" and "target" builds as they are executing.
If there are any differences between the compared outcomes, the task will fail. The location of the HTML report providing insight into the comparison will be given. If all compared outcomes are found to be identical, and there are no uncompared outcomes, and there are no unknown build outcomes, the task will succeed.
You can configure the task to not fail on compared outcome differences by setting the
ignoreFailures property to true.
compareGradleBuilds { ignoreFailures = true }
For an archive to be a candidate for comparison, it must be added as an artifact of the archives configuration. Take a look at Chapter 31, Publishing artifacts for more information on how to configure and add artifacts.
The archive must also have been produced by a
Ear task. Future versions of Gradle
will support increased flexibility in this area.
This chapter describes the original publishing mechanism available in Gradle 1.0: in Gradle 1.3 a new mechanism for publishing was introduced. While this new mechanism is incubating and not yet complete, it introduces some new concepts and features that do (and will) make Gradle publishing even more powerful.
You can read about the new publishing plugins in Chapter 34, Ivy Publishing (new) and Chapter 35, Maven Publishing (new). Please try them out and give us feedback.
This chapter is about how you declare the outgoing artifacts of your project, and how to work with them (e.g. upload them). We define the artifacts of the projects as the files the project provides to the outside world. This might be a library or a ZIP distribution or any other file. A project can publish as many artifacts as it wants.
Like dependencies, artifacts are grouped by configurations. In fact, a configuration can contain both artifacts and dependencies at the same time.
For each configuration in your project, Gradle provides the tasks
upload and
Execution of these tasks will build or upload the artifacts belonging to the respective configuration.
Table 46.5, "Java plugin - dependency configurations" shows the configurations added by the Java plugin. Two of the
configurations are relevant for the usage with artifacts. The
archives configuration is the standard
configuration to assign your artifacts to. The Java plugin automatically assigns the default jar to this
configuration. We will talk more about the
runtime configuration in Section 31.5, "More about project libraries".
As with dependencies, you can declare as many custom configurations as you like and assign artifacts to them.
You can use an archive task to define an artifact:
Example 31.1. Defining an artifact using an archive task
task myJar(type: Jar) artifacts { archives myJar }
It is important to note that the custom archives you are creating as part of your build are not automatically assigned to any configuration. You have to explicitly do this assignment.
You can also use a file to define an artifact:
Example 31.2. Defining an artifact using a file
def someFile = file('build/somefile.txt')
artifacts {
archives someFile
Gradle will figure out the properties of the artifact based on the name of the file. You can customize these properties:
Example 31.3. Customizing an artifact
task myTask(type: MyTaskType) { destFile = file('build/somefile.txt') } artifacts { archives(myTask.destFile) { name 'my-artifact' type 'text' builtBy myTask } }
There is a map-based syntax for defining an artifact using a file. The map must include a
file entry that
defines the file. The map may include other artifact properties:
Example 31.4. Map syntax for defining an artifact using a file
task generate(type: MyTaskType) { destFile = file('build/somefile.txt') } artifacts { archives file: generate.destFile, name: 'my-artifact', type: 'text', builtBy: generate }
We have said that there is a specific upload task for each configuration. Before you can do an upload, you have to configure the upload task and define where to publish the artifacts to. The repositories you have defined (as described in Section 24.6, "Repositories") are not automatically used for uploading. In fact, some of those repositories only allow downloading artifacts, not uploading. Here is an example of how you can configure the upload task of a configuration:
Example 31.5. Configuration of the upload task
repositories { flatDir { name "fileRepo" dirs "repo" } } uploadArchives { repositories { add project.repositories.fileRepo ivy { credentials { username "username" password "pw" } url "http://repo.mycompany.com" } } }
As you can see, you can either use a reference to an existing repository or create a new repository. As described in Section 24.6.9, "More about Ivy resolvers", you can use all the Ivy resolvers suitable for the purpose of uploading.
If an upload repository is defined with multiple patterns, Gradle must choose a pattern to use for uploading each file.
By default, Gradle will upload to the pattern defined by the
url parameter, combined with the optional
layout parameter.
If no
url parameter is supplied, then Gradle will use the first defined
artifactPattern for uploading,
or the first defined
ivyPattern for uploading Ivy files, if this is set.
Uploading to a Maven repository is described in Section 32.6, "Interacting with Maven repositories".
If your project is supposed to be used as a library, you need to define what are the artifacts of this library
and what are the dependencies of these artifacts. The Java plugin adds a
runtime configuration for
this purpose, with the implicit assumption that the
runtime dependencies are the dependencies of
the artifact you want to publish. Of course this is fully customizable. You can add your own custom configuration or let the
existing configurations extend from other configurations. You might have a different group of artifacts which have
a different set of dependencies. This mechanism is very powerful and flexible.
If someone wants to use your project as a library, she simply needs to declare which configuration of
the dependency to depend on.
A Gradle dependency offers the
configuration property to declare this. If this
is not specified, the
default configuration is used (see Section 24.4.9, "Dependency configurations").
Using your project as a library
can either happen from within a multi-project build or by retrieving your project from a repository. In
the latter case, an
ivy.xml descriptor in the repository is supposed to contain all the necessary information. If you
work with Maven repositories you don't have the flexibility as described above. For how to publish to a Maven
repository, see the section Section 32.6, "Interacting with Maven repositories".
[19] To be exact, the Base plugin provides those tasks. This plugin is automatically applied if you use the Java plugin.
This chapter is a work in progress
The Maven plugin adds support for deploying artifacts to Maven repositories.
To use the Maven plugin, include the following in your build script:
The Maven plugin defines the following tasks:
Table 32.1. Maven plugin - tasks
|Task name
|Depends on
|All tasks that build the associated archives.
|Installs the associated artifacts to the local Maven cache, including Maven metadata generation.
By default the install task is associated with the
archives configuration. This
configuration has by default only the default jar as an element. To learn more about installing to the
local repository, see: Section 32.6.3, "Installing to the local repository"
The Maven plugin defines the following convention properties:
Table 32.2. Maven plugin - properties
|Property name
|Default value
|The directory where the generated POMs are written to.
|Instructions for mapping Gradle configurations to Maven scopes. See the section called "Dependency mapping".
These properties are provided by a
convention object.
The maven plugin provides a factory method for creating a POM. This is useful if you need a POM without the context of uploading to a Maven repo.
Example 32.2. Creating a stand alone pom.
task writeNewPom { doLast { pom { project { inceptionYear '2008' licenses { license { name 'The Apache Software License, Version 2.0' url 'http://www.apache.org/licenses/LICENSE-2.0.txt' distribution 'repo' } } } }.writeTo("$buildDir/newpom.xml") } }
Amongst other things, Gradle supports the same builder syntax as polyglot Maven. To learn more about the Gradle Maven POM object, see
MavenPom. See also:
With Gradle you can deploy to remote Maven repositories or install to your local Maven repository. This includes all Maven metadata manipulation and works also for Maven snapshots. In fact, Gradle's deployment is 100 percent Maven compatible as we use the native Maven Ant tasks under the hood.
Deploying to a Maven repository is only half the fun if you don't have a POM. Fortunately Gradle can generate this POM for you using the dependency information it has.
Let's assume your project produces just the default jar file. Now you want to deploy this jar file to a remote Maven repository.
Example 32.3. Upload of file to remote Maven repository
apply plugin: 'maven' uploadArchives { repositories { mavenDeployer { repository(url: "file://localhost/tmp/myRepo/") } } }
That is all. Calling the
task will generate the POM and deploys the artifact and the POM to the specified repository.
There is more work to do if you need support for protocols other than
file. In
this case the native Maven code we delegate to needs additional libraries. Which libraries are needed depends on what
protocol you plan to use. The available protocols and the corresponding libraries are listed in Table 32.3, "Protocol jars for Maven deployment" (those libraries have transitive dependencies which have transitive
For example, to use the ssh protocol you can do:
Example 32.4. Upload of file via SSH
configurations { deployerJars } repositories { mavenCentral() } dependencies { deployerJars "org.apache.maven.wagon:wagon-ssh:2.2" } uploadArchives { repositories.mavenDeployer { configuration = configurations.deployerJars repository(url: "scp://repos.mycompany.com/releases") { authentication(userName: "me", password: "myPassword") } } }
There are many configuration options for the Maven deployer. The configuration is done via a Groovy builder. All the elements of this tree are Java beans. To configure the simple attributes you pass a map to the bean elements. To add bean elements to its parent, you use a closure. In the example above repository and authentication are such bean elements. Table 32.4, "Configuration elements of the MavenDeployer" lists the available bean elements and a link to the Javadoc of the corresponding class. In the Javadoc you can see the possible attributes you can set for a particular element.
In Maven you can define repositories and optionally snapshot repositories. If no snapshot repository
is defined, releases and snapshots are both deployed to the
element. Otherwise snapshots are deployed to the
Table 32.3. Protocol jars for Maven deployment
Table 32.4. Configuration elements of the MavenDeployer
The Maven plugin adds an
install task to your project. This task depends on all the archives
task of the
archives configuration. It installs those archives to your local Maven repository.
If the default location for the local repository is redefined in a Maven
settings.xml, this is
considered by this task.
When deploying an artifact to a Maven repository, Gradle automatically generates a POM for it. The
version and
elements used for the POM default to the values shown in the table below. The
elements are created from the project's dependency declarations.
Table 32.5. Default Values for Maven POM generation
|Maven Element
|Default Value
|uploadTask.repositories.mavenDeployer.pom.artifactId (if set) or archiveTask.baseName.
uploadTask and
archiveTask refer to the tasks used for uploading and generating the archive,
respectively (for example
uploadArchives and
archiveTask.baseName defaults
project.archivesBaseName which in turn defaults to
When you set the "
archiveTask.baseName" property to a value other than the default, you'll also have to set
uploadTask.repositories.mavenDeployer.pom.artifactId to the same value. Otherwise, the project at hand may
be referenced with the wrong artifact ID from generated POMs for other projects in the same build.
Generated POMs can be found in
<buildDir>/poms. They can be further customized via the
MavenPom API. For example, you might want the artifact deployed
to the Maven repository to have a different version or name than the artifact generated by Gradle. To customize these you can do:
Example 32.5. Customization of pom
uploadArchives { repositories { mavenDeployer { repository(url: "file://localhost/tmp/myRepo/") pom.version = '1.0Maven' pom.artifactId = 'myMavenName' } } }
To add additional content to the POM, the
pom.project builder can be used. With this builder,
any element listed in the Maven POM reference can be added.
Example 32.6. Builder style customization of pom
uploadArchives { repositories { mavenDeployer { repository(url: "file://localhost/tmp/myRepo/") pom.project { licenses { license { name 'The Apache Software License, Version 2.0' url 'http://www.apache.org/licenses/LICENSE-2.0.txt' distribution 'repo' } } } } } }
version, and
should always be set directly on the
pom object.
Example 32.7. Modifying auto-generated content
def installer = install.repositories.mavenInstaller def deployer = uploadArchives.repositories.mavenDeployer [installer, deployer]*.pom*.whenConfigured {pom -> pom.dependencies.find {dep -> dep.groupId == 'group3' && dep.artifactId == 'runtime' }.optional = true }
If you have more than one artifact to publish, things work a little bit differently. See the section called "Multiple artifacts per project".
To customize the settings for the Maven installer (see Section 32.6.3, "Installing to the local repository"), you can do:
Example 32.8. Customization of Maven installer
install { repositories.mavenInstaller { pom.version = '1.0Maven' pom.artifactId = 'myName' } }
Maven can only deal with one artifact per project. This is reflected in the structure of the
Maven POM. We think there are many situations where it makes sense to have more than one artifact per
project. In such a case you need to generate multiple POMs. In such a case you have to explicitly declare each artifact
you want to publish to a Maven repository. The
MavenDeployer and the MavenInstaller
both provide an API for this:
Example 32.9. Generation of multiple poms
uploadArchives { repositories { mavenDeployer { repository(url: "file://localhost/tmp/myRepo/") addFilter('api') {artifact, file -> artifact.name == 'api' } addFilter('service') {artifact, file -> artifact.name == 'service' } pom('api').version = 'mySpecialMavenVersion' } } }
You need to declare a filter for each artifact you want to publish. This filter defines a boolean expression for
which Gradle artifact it accepts. Each filter has a POM associated with it which you can configure.
To learn more about this have a look at
and its associated classes.
The Maven plugin configures the default mapping between the Gradle configurations added by the
Java and War plugin and the Maven scopes. Most
of the time you don't need to touch this and you can safely skip this section. The mapping
works like the following. You can map a configuration to one and only one scope. Different
configurations can be mapped to one or different scopes. You can also assign a priority to a particular
configuration-to-scope mapping. Have a look at
to learn more. To access the mapping configuration you can say:
Example 32.10. Accessing a mapping configuration
task mappings { doLast { println conf2ScopeMappings.mappings } }
Gradle exclude rules are converted to Maven excludes if possible. Such a conversion is possible if in the Gradle exclude rule the group as well as the module name is specified (as Maven needs both in contrast to Ivy). Per-configuration excludes are also included in the Maven POM, if they are convertible.
[20] It is planned for a future release to provide out-of-the-box support for this
The signing plugin adds the ability to digitally sign built files and artifacts. These digital signatures can then be used to prove who built the artifact the signature is attached to as well as other information such as when the signature was generated.
The signing plugin currently only provides support for generating OpenPGP signatures (which is the signature format required for publication to the Maven Central Repository).
To use the Signing plugin, include the following in your build script:
In order to create OpenPGP signatures, you will need a key pair (instructions on creating a key pair using the GnuPG tools can be found in the GnuPG HOWTOs). You need to provide the signing plugin with your key information, which means three things:
The public key ID (an 8 character hexadecimal string).
The absolute path to the secret key ring file containing your private key.
The passphrase used to protect your private key.
These items must be supplied as the values of properties
signing.secretKeyRingFile, and
signing.password respectively. Given the personal and private nature of these values, a good practice
is to store them in the user
gradle.properties file (described in Section 12.2, "Gradle properties and system properties").
signing.keyId=24875D73 signing.password=secret signing.secretKeyRingFile=/Users/me/.gnupg/secring.gpg
If specifying this information (especially
signing.password) in the user
gradle.properties file is not feasible for your environment, you can source the information
however you need to and set the project properties manually.
import org.gradle.plugins.signing.Sign gradle.taskGraph.whenReady { taskGraph -> if (taskGraph.allTasks.any { it instanceof Sign }) { // Use Java 6's console to read from the console (no good for // a CI environment) Console console = System.console() console.printf "\n\nWe have to sign some things in this build." + "\n\nPlease enter your signing details.\n\n" def id = console.readLine("PGP Key Id: ") def file = console.readLine("PGP Secret Key Ring File (absolute path): ") def password = console.readPassword("PGP Private Key Password: ") allprojects { ext."signing.keyId" = id } allprojects { ext."signing.secretKeyRingFile" = file } allprojects { ext."signing.password" = password } console.printf "\nThanks.\n\n" } }
OpenPGP supports subkeys, which are like the normal keys, except they're bound to a master key pair. One feature of OpenPGP subkeys is that they can be revoked independently of the master keys which makes key management easier. A practical case study of how subkeys can be leveraged in software development can be read on the Debian wiki.
The signing plugin supports OpenPGP subkeys out of the box. Just specify a subkey ID as the value in the
signing.keyId property.
As well as configuring how things are to be signed (i.e. the signatory configuration), you must also specify what is to be signed. The Signing plugin provides a DSL that allows you to specify the tasks and/or configurations that should be signed.
It is common to want to sign the artifacts of a configuration. For example, the Java plugin
configures a jar to build and this jar artifact is added to the
archives configuration.
Using the Signing DSL, you can specify that all of the artifacts of this configuration should be signed.
This will create a task (of type
Sign) in your project named "
that will build any
archives artifacts (if needed) and then generate signatures for them. The signature files will be placed
alongside the artifacts being signed.
Example 33.3. Signing a configuration output
Output of
gradle signArchives
> gradle signArchives :compileJava :processResources :classes :jar :signArchives BUILD SUCCESSFUL Total time: 1 secs
In some cases the artifact that you need to sign may not be part of a configuration. In this case you can directly sign the task that produces the artifact to sign.
Example 33.4. Signing a task
task stuffZip (type: Zip) { baseName = "stuff" from "src/stuff" } signing { sign stuffZip }
This will create a task (of type
Sign) in your project named "
that will build the input task's archive (if needed) and then sign it. The signature file will be placed
alongside the artifact being signed.
Example 33.5. Signing a task output
Output of
gradle signStuffZip
> gradle signStuffZip :stuffZip :signStuffZip BUILD SUCCESSFUL Total time: 1 secs
For a task to be "signable", it must produce an archive of some type. Tasks that do this are the
War and
Ear tasks.
A common usage pattern is to only sign build artifacts under certain conditions. For example, you may not wish to sign artifacts for non release versions. To achieve this, you can specify that signing is only required under certain conditions.
Example 33.6. Conditional signing
version = '1.0-SNAPSHOT' ext.isReleaseVersion = !version.endsWith("SNAPSHOT") signing { required { isReleaseVersion && gradle.taskGraph.hasTask("uploadArchives") } sign configurations.archives }
In this example, we only want to require signing if we are building a release version and we are going to publish it. Because we are inspecting the task
graph to determine if we are going to be publishing, we must set the
signing.required property to a closure to defer the evaluation. See
SigningExtension.setRequired(java.lang.Object) for more information.
When specifying what is to be signed via the Signing DSL, the resultant signature artifacts are automatically added to the
signatures and
archives dependency configurations. This means that if you want to upload your signatures to your distribution repository along
with the artifacts you simply execute the
uploadArchives task as normal.
When deploying signatures for your artifacts to a Maven repository, you will also want to sign the published POM file. The signing plugin adds a
signing.signPom() (see:
SigningExtension.signPom(org.gradle.api.artifacts.maven.MavenDeployment, groovy.lang.Closure)) method that can be used in the
beforeDeployment() block in your upload task configuration.
Example 33.7. Signing a POM for deployment
uploadArchives { repositories { mavenDeployer { beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) } } } }
When signing is not required and the POM cannot be signed due to insufficient configuration (i.e. no credentials for signing) then the
signPom() method will silently do nothing.
This chapter describes the new incubating Ivy publishing support provided by the "
plugin. Eventually this new publishing support will replace publishing via the
Upload task.
If you are looking for documentation on the original Ivy publishing support using the
Upload task please see
Chapter 31, Publishing artifacts.
This chapter describes how to publish build artifacts in the Apache Ivy format, usually to a repository
for consumption by other builds or projects. What is published is one or more artifacts created by the build, and an
Ivy module descriptor (normally
that describes the artifacts and the dependencies of the artifacts, if any.
A published Ivy module can be consumed by Gradle (see Chapter 24, Dependency Management) and other tools that understand the Ivy format.
The ability to publish in the Ivy format is provided by the "
ivy-publish" plugin.
The "
publishing" plugin creates an extension on the project named "
publishing" of type
This extension provides a container of named publications and a container of named repositories. The "
ivy-publish" plugin works with
IvyPublication publications and
IvyArtifactRepository repositories.
Applying the "
ivy-publish" plugin does the following:
publishing" plugin
task for each
IvyPublication added (see Section 34.2, "Publications").
PublishToIvyRepository task
for the combination of each
IvyPublication added (see Section 34.2, "Publications"),
with each
IvyArtifactRepository added (see Section 34.3, "Repositories").
If you are not familiar with project artifacts and configurations, you should read Chapter 31, Publishing artifacts, which introduces these concepts. This chapter also describes "publishing artifacts" using a different mechanism than what is described in this chapter. The publishing functionality described here will eventually supersede that functionality.
Publication objects describe the structure/configuration of a publication to be created. Publications are published to repositories via tasks, and the
configuration of the publication object determines exactly what is published. All of the publications of a project are defined in the
PublishingExtension.getPublications() container. Each publication has a unique name within the project.
For the "
ivy-publish" plugin to have any effect, an
IvyPublication must be added to the set of publications.
This publication determines which artifacts are actually published as well as the details included in the associated Ivy module descriptor file.
A publication can be configured by adding components, customizing artifacts, and by modifying the generated module descriptor file directly.
The simplest way to publish a Gradle project to an Ivy repository is to specify a
SoftwareComponent to publish.
The components presently available for publication are:
Table 34.1. Software Components
|Provided By
|Java Plugin
|Generated jar file
|Dependencies from 'runtime' configuration
|War Plugin
|Generated war file
|No dependencies
In the following example, artifacts and runtime dependencies are taken from the `java` component, which is added by the
Java Plugin.
Example 34.2. Publishing a Java module to Ivy
publications { ivyJava(IvyPublication) { from components.java } }
It is also possible to explicitly configure artifacts to be included in the publication. Artifacts are commonly supplied as raw files, or as instances of
AbstractArchiveTask (e.g. Jar, Zip).
For each custom artifact, it is possible to specify the
conf values to use for publication. Note that each artifacts must have a unique name/classifier/extension combination.
Configure custom artifacts as follows:
Example 34.3. Publishing additional artifact to Ivy
task sourceJar(type: Jar) { from sourceSets.main.java classifier "source" } publishing { publications { ivy(IvyPublication) { from components.java artifact(sourceJar) { type "source" conf "runtime" } } } }
See the
IvyPublication class in the API documentation for more detailed information on how artifacts can be customized.
The generated Ivy module descriptor file contains an
<info> element that identifies the module.
The default identity values are derived from the following:
organisation -
module -
revision -
status -
branch - (not set)
Overriding the default identity values is easy: simply specify the
revision attributes when configuring the
IvyPublication. The
branch attributes can be set via the
descriptor property (see
IvyModuleDescriptorSpec). The
descriptor property can also be
used to add additional custom elements as children of the
<info> element.
Example 34.4. customizing the publication identity
publishing { publications { ivy(IvyPublication) { organisation 'org.gradle.sample' module 'project1-sample' revision '1.1' descriptor.status = 'milestone' descriptor.branch = 'testing' descriptor.extraInfo 'http://my.namespace', 'myElement', 'Some value' from components.java } } }
Gradle will handle any valid Unicode character for organisation, module and revision (as well as artifact name, extension and classifier).
The only values that are explicitly prohibited are '
\', '
/' and any ISO control character.
The supplied values are validated early during publication.
At times, the module descriptor file generated from the project information will need to be tweaked before publishing. The "
plugin provides a hook to allow such modification.
Example 34.5. Customizing the module descriptor file
publications { ivyCustom(IvyPublication) { descriptor.withXml { asNode().info[0].appendNode('description', 'A demonstration of ivy descriptor customization') } } }
In this example we are simply adding a 'description' element to the generated Ivy dependency descriptor, but this hook allows you to modify any aspect of the generated descriptor. For example, you could replace the version range for a dependency with the actual version used to produce the build.
IvyModuleDescriptorSpec.withXml(org.gradle.api.Action) in the API documentation for more information.
It is possible to modify virtually any aspect of the created descriptor should you need to. This means that it is also possible to modify the descriptor in such a way that it is no longer a valid Ivy module descriptor, so care must be taken when using this feature.
The identifier (organisation, module, revision) of the published module is an exception; these values cannot be modified in the descriptor using the `withXML` hook.
Sometimes it's useful to publish multiple modules from your Gradle build, without creating a separate Gradle subproject. An example is publishing a separate API and implementation jar for your library. With Gradle this is simple:
Example 34.6. Publishing multiple modules from a single project
task apiJar(type: Jar) { baseName "publishing-api" from sourceSets.main.output exclude '**/impl/**' } publishing { publications { impl(IvyPublication) { organisation 'org.gradle.sample.impl' module 'project2-impl' revision '2.3' from components.java } api(IvyPublication) { organisation 'org.gradle.sample' module 'project2-api' revision '2' } } }
If a project defines multiple publications then Gradle will publish each of these to the defined repositories. Each publication must be given a unique identity as described above.
Publications are published to repositories. The repositories to publish to are defined by the
Example 34.7. Declaring repositories to publish to
repositories { ivy { // change to point to your repo, e.g. http://my.org/repo url "$buildDir/repo" } }
The DSL used to declare repositories for publishing is the same DSL that is used to declare repositories for dependencies (
However, in the context of Ivy publication only the repositories created by the
ivy() methods can be used as publication destinations.
You cannot publish an
IvyPublication to a Maven repository for example.
The "
ivy-publish" plugin automatically creates a
task for each
IvyPublication and
combination in the
publishing.publications and
publishing.repositories containers respectively.
The created task is named "
which is "
publishIvyJavaPublicationToIvyRepository" for this example. This task is of type
Example 34.8. Choosing a particular publication to publish
apply plugin: 'java' apply plugin: 'ivy-publish' group = 'org.gradle.sample' version = '1.0' publishing { publications { ivyJava(IvyPublication) { from components.java } } repositories { ivy { // change to point to your repo, e.g. http://my.org/repo url "$buildDir/repo" } } }
Output of
gradle publishIvyJavaPublicationToIvyRepository
> gradle publishIvyJavaPublicationToIvyRepository :generateDescriptorFileForIvyJavaPublication :compileJava UP-TO-DATE :processResources UP-TO-DATE :classes UP-TO-DATE :jar :publishIvyJavaPublicationToIvyRepository BUILD SUCCESSFUL Total time: 1 secs
The "
publish" plugin (that the "
ivy-publish" plugin implicitly applies) adds a lifecycle task
that can be used to publish all publications to all applicable repositories named "
In more concrete terms, executing this task will execute all
PublishToIvyRepository tasks in the project.
This is usually the most convenient way to perform a publish.
Example 34.9. Publishing all publications via the "publish" lifecycle task
Output of
gradle publish
> gradle publish :generateDescriptorFileForIvyJavaPublication :compileJava UP-TO-DATE :processResources UP-TO-DATE :classes UP-TO-DATE :jar :publishIvyJavaPublicationToIvyRepository :publish BUILD SUCCESSFUL Total time: 1 secs
At times it is useful to generate the Ivy module descriptor file (normally
ivy.xml) without publishing your module to an Ivy repository.
Since descriptor file generation is performed by a separate task, this is very easy to do.
The "
ivy-publish" plugin creates one
GenerateIvyDescriptor task
for each registered
named "
generateDescriptorFileFor«PUBNAME»Publication", which will be
generateDescriptorFileForIvyJavaPublication" for the previous example of the "
ivyJava" publication.
You can specify where the generated Ivy file will be located by setting the
destination property on the generated task.
By default this file is written to "
Example 34.10. Generating the Ivy module descriptor file
model {
tasks.generateDescriptorFileForIvyCustomPublication {
destination = file("$buildDir/generated-ivy.xml")
Output of
gradle generateDescriptorFileForIvyCustomPublication
> gradle generateDescriptorFileForIvyCustomPublication :generateDescriptorFileForIvyCustomPublication BUILD SUCCESSFUL Total time: 1 secs
The "
ivy-publish" plugin leverages some experimental support for late plugin configuration,
and the
GenerateIvyDescriptor task will not be constructed until the publishing extension is configured.
The simplest way to ensure that the publishing plugin is configured when you attempt to access the
GenerateIvyDescriptor task
is to place the access inside a
model block, as the example above demonstrates.
The same applies to any attempt to access publication-specific tasks like
These tasks should be referenced from within a
model block.
The following example demonstrates publishing with a multi-project build. Each project publishes a Java component and a configured additional source artifact. The descriptor file is customized to include the project description for each project.
Example 34.11. Publishing a Java module
subprojects { apply plugin: 'java' apply plugin: 'ivy-publish' version = '1.0' group = 'org.gradle.sample' repositories { mavenCentral() } task sourceJar(type: Jar) { from sourceSets.main.java classifier "source" } } project(":project1") { description = "The first project" dependencies { compile 'junit:junit:4.12', project(':project2') } } project(":project2") { description = "The second project" dependencies { compile 'commons-collections:commons-collections:3.2.2' } } subprojects { publishing { repositories { ivy { // change to point to your repo, e.g. http://my.org/repo url "${rootProject.buildDir}/repo" } } publications { ivy(IvyPublication) { from components.java artifact(sourceJar) { type "source" conf "runtime" } descriptor.withXml { asNode().info[0].appendNode('description', description) } } } } }
The result is that the following artifacts will be published for each project:
project1 is published, the module descriptor (i.e. the
ivy.xml file) that is produced will look like:
Note that
«PUBLICATION-TIME-STAMP» in this example Ivy module descriptor will be the timestamp of when the descriptor was generated.
Example 34.12. Example generated ivy.xml
<?xml version="1.0" encoding="UTF-8"?> <ivy-module version="2.0"> <info organisation="org.gradle.sample" module="project1" revision="1.0" status="integration" publication="?PUBLICATION-TIME-STAMP?"> <description>The first project</description> </info> <configurations> <conf name="default" visibility="public" extends="runtime"/> <conf name="runtime" visibility="public"/> </configurations> <publications> <artifact name="project1" type="jar" ext="jar" conf="runtime"/> <artifact name="project1" type="source" ext="jar" conf="runtime" m:classifier="source" xmlns:m="http://ant.apache.org/ivy/maven"/> </publications> <dependencies> <dependency org="junit" name="junit" rev="4.12" conf="runtime->default"/> <dependency org="org.gradle.sample" name="project2" rev="1.0" conf="runtime->default"/> </dependencies> </ivy-module>
The "
ivy-publish" plugin functionality as described above is incomplete, as the feature is still incubating.
In upcoming Gradle releases, the functionality will be expanded to include (but not limited to):
organisation etc.)
module descriptor.
This chapter describes the new incubating Maven publishing support provided by the "
plugin. Eventually this new publishing support will replace publishing via the
Upload task.
If you are looking for documentation on the original Maven publishing support using the
Upload task please see
Chapter 31, Publishing artifacts.
This chapter describes how to publish build artifacts to an Apache Maven Repository. A module published to a Maven repository can be consumed by Maven, Gradle (see Chapter 24, Dependency Management) and other tools that understand the Maven repository format.
The ability to publish in the Maven format is provided by the "
maven-publish" plugin.
The "
publishing" plugin creates an extension on the project named "
publishing" of type
This extension provides a container of named publications and a container of named repositories. The "
maven-publish" plugin works with
MavenPublication publications and
MavenArtifactRepository repositories.
Applying the "
maven-publish" plugin does the following:
publishing" plugin
task for each
MavenPublication added (see Section 35.2, "Publications").
PublishToMavenRepository task
for the combination of each
MavenPublication added (see Section 35.2, "Publications"),
with each
MavenArtifactRepository added (see Section 35.3, "Repositories").
PublishToMavenLocal task
for each
MavenPublication added (seeSection 35.2, "Publications").
If you are not familiar with project artifacts and configurations, you should read the Chapter 31, Publishing artifacts that introduces these concepts. This chapter also describes "publishing artifacts" using a different mechanism than what is described in this chapter. The publishing functionality described here will eventually supersede that functionality.
Publication objects describe the structure/configuration of a publication to be created. Publications are published to repositories via tasks, and the
configuration of the publication object determines exactly what is published. All of the publications of a project are defined in the
PublishingExtension.getPublications() container. Each publication has a unique name within the project.
For the "
maven-publish" plugin to have any effect, a
MavenPublication must be added to the set of publications.
This publication determines which artifacts are actually published as well as the details included in the associated POM file.
A publication can be configured by adding components, customizing artifacts, and by modifying the generated POM file directly.
The simplest way to publish a Gradle project to a Maven repository is to specify a
SoftwareComponent to publish.
The components presently available for publication are:
Table 35.1. Software Components
|Provided By
|Chapter 46, The Java Plugin
|Generated jar file
|Dependencies from 'runtime' configuration
|Chapter 48, The War Plugin
|Generated war file
|No dependencies
In the following example, artifacts and runtime dependencies are taken from the `java` component, which is added by the
Java Plugin.
Example 35.2. Adding a MavenPublication for a Java component
publishing { publications { mavenJava(MavenPublication) { from components.java } } }
It is also possible to explicitly configure artifacts to be included in the publication. Artifacts are commonly supplied as raw files, or as instances of
AbstractArchiveTask (e.g. Jar, Zip).
For each custom artifact, it is possible to specify the
extension and
classifier values to use for publication. Note that
only one of the published artifacts can have an empty classifier, and all other artifacts must have a unique classifier/extension combination.
Configure custom artifacts as follows:
Example 35.3. Adding additional artifact to a MavenPublication
task sourceJar(type: Jar) {
from sourceSets.main.allJava
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
artifact sourceJar {
classifier "sources"
See the
MavenPublication class in the API documentation for more information about how artifacts can be customized.
The attributes of the generated
POM file will contain identity values derived from the following project properties:
groupId -
artifactId -
version -
Overriding the default identity values is easy: simply specify the
version attributes when configuring the
Example 35.4. customizing the publication identity
publishing { publications { maven(MavenPublication) { groupId 'org.gradle.sample' artifactId 'project1-sample' version '1.1' from components.java } } }
Maven restricts 'groupId' and 'artifactId' to a limited character set (
[A-Za-z0-9_\\-.]+) and Gradle enforces this restriction.
For 'version' (as well as artifact 'extension' and 'classifier'), Gradle will handle any valid Unicode character.
The only Unicode values that are explicitly prohibited are '
\', '
/' and any ISO control character.
Supplied values are validated early in publication.
The generated POM file may need to be tweaked before publishing. The "
plugin provides a hook to allow such modification.
Example 35.5. Modifying the POM file
publications { mavenCustom(MavenPublication) { pom.withXml { asNode().appendNode('description', 'A demonstration of maven POM customization') } } }
In this example we are adding a 'description' element for the generated POM. With this hook, you can modify any aspect of the POM. For example, you could replace the version range for a dependency with the actual version used to produce the build.
MavenPom.withXml(org.gradle.api.Action) in the API documentation for more information.
It is possible to modify virtually any aspect of the created POM. This means that it is also possible to modify the POM in such a way that it is no longer a valid Maven POM, so care must be taken when using this feature.
The identifier (groupId, artifactId, version) of the published module is an exception; these values cannot be modified in the POM using the `withXML` hook.
Sometimes it's useful to publish multiple modules from your Gradle build, without creating a separate Gradle subproject. An example is publishing a separate API and implementation jar for your library. With Gradle this is simple:
Example 35.6. Publishing multiple modules from a single project
task apiJar(type: Jar) { baseName "publishing-api" from sourceSets.main.output exclude '**/impl/**' } publishing { publications { impl(MavenPublication) { groupId 'org.gradle.sample.impl' artifactId 'project2-impl' version '2.3' from components.java } api(MavenPublication) { groupId 'org.gradle.sample' artifactId 'project2-api' version '2' artifact apiJar } } }
If a project defines multiple publications then Gradle will publish each of these to the defined repositories. Each publication must be given a unique identity as described above.
Publications are published to repositories. The repositories to publish to are defined by the
Example 35.7. Declaring repositories to publish to
publishing { repositories { maven { // change to point to your repo, e.g. http://my.org/repo url "$buildDir/repo" } } }
The DSL used to declare repositories for publication is the same DSL that is used to declare repositories to consume dependencies from,
RepositoryHandler. However, in the context of Maven publication only
MavenArtifactRepository repositories can be used for publication.
The "
maven-publish" plugin automatically creates a
task for each
MavenPublication and
combination in the
publishing.publications and
publishing.repositories containers respectively.
The created task is named "
Example 35.8. Publishing a project to a Maven repository
apply plugin: 'java' apply plugin: 'maven-publish' group = 'org.gradle.sample' version = '1.0' publishing { publications { mavenJava(MavenPublication) { from components.java } } } publishing { repositories { maven { // change to point to your repo, e.g. http://my.org/repo url "$buildDir/repo" } } }
Output of
gradle publish
> gradle publish :generatePomFileForMavenJavaPublication :compileJava :processResources UP-TO-DATE :classes :jar :publishMavenJavaPublicationToMavenRepository :publish BUILD SUCCESSFUL Total time: 1 secs
In this example, a task named "
publishMavenJavaPublicationToMavenRepository" is created, which is of type
This task is wired into the
publish lifecycle task.
Executing "
gradle publish" builds the POM file and all of the artifacts to be published, and transfers them to the repository.
For integration with a local Maven installation, it is sometimes useful to publish the module into the local .m2 repository. In Maven parlance, this is
referred to as 'installing' the module. The "
maven-publish" plugin makes this easy to do by automatically creating a
PublishToMavenLocal task for each
in the
publishing.publications container. Each of these tasks is wired into the
publishToMavenLocal lifecycle task.
You do not need to have `mavenLocal` in your `publishing.repositories` section.
The created task is named "
Example 35.9. Publish a project to the Maven local repository
Output of
gradle publishToMavenLocal
> gradle publishToMavenLocal :generatePomFileForMavenJavaPublication :compileJava :processResources UP-TO-DATE :classes :jar :publishMavenJavaPublicationToMavenLocal :publishToMavenLocal BUILD SUCCESSFUL Total time: 1 secs
The resulting task in this example is named "
publishMavenJavaPublicationToMavenLocal". This task is wired into the
publishToMavenLocal lifecycle task.
Executing "
gradle publishToMavenLocal" builds the POM file and all of the artifacts to be published, and "installs" them into the local Maven repository.
At times it is useful to generate a Maven POM file for a module without actually publishing. Since POM generation is performed by a separate task, it is very easy to do so.
The task for generating the POM file is of type
GenerateMavenPom, and it is given a name based on the name
of the publication: "
generatePomFileFor«PUBNAME»Publication". So in the example below,
where the publication is named "
the task will be named "
Example 35.10. Generate a POM file without publishing
model {
tasks.generatePomFileForMavenCustomPublication {
destination = file("$buildDir/generated-pom.xml")
Output of
gradle generatePomFileForMavenCustomPublication
> gradle generatePomFileForMavenCustomPublication :generatePomFileForMavenCustomPublication BUILD SUCCESSFUL Total time: 1 secs
All details of the publishing model are still considered in POM generation, including
components, custom
and any modifications made via
The "
maven-publish" plugin leverages some experimental support for late plugin configuration,
and any
GenerateMavenPom tasks will not be constructed until the publishing extension is configured.
The simplest way to ensure that the publishing plugin is configured when you attempt to access the
GenerateMavenPom task
is to place the access inside a
model block, as the example above demonstrates.
The same applies to any attempt to access publication-specific tasks like
These tasks should be referenced from within a
model block.
The distribution plugin is currently incubating. Please be aware that the DSL and other configuration may change in later Gradle versions.
The distribution plugin facilitates building archives that serve as distributions of the project. Distribution archives typically contain the executable application and other supporting files, such as documentation.
To use the distribution plugin, include the following in your build script:
The plugin adds an extension named "
distributions" of type
DistributionContainer to the project.
It also creates a single distribution in the distributions container extension named "
If your build only produces one distribution you only need to configure this distribution (or use the defaults).
You can run "
gradle distZip" to package the main distribution as a ZIP, or "
gradle distTar" to create
a TAR file. To build both types of archives just run
gradle assembleDist.
The files will be created at "
You can run "
gradle installDist" to assemble the uncompressed distribution into "
The Distribution plugin adds the following tasks to the project:
Table 36.1. Distribution plugin - tasks
|Task name
|Depends on
|Creates a ZIP archive of the distribution contents
|Creates a TAR archive of the distribution contents
|Creates ZIP and TAR archives with the distribution contents
|Assembles the distribution content and installs it on the current machine
For each extra distribution set you add to the project, the distribution plugin adds the following tasks:
Table 36.2. Multiple distributions - tasks
|Task name
|Depends on
|Creates a ZIP archive of the distribution contents
|Creates a TAR archive of the distribution contents
|Assembles all distribution archives
|Assembles the distribution content and installs it on the current machine
Example 36.2. Adding extra distributions
apply plugin: 'distribution' version = '1.2' distributions { custom {} }
This will add following tasks to the project:
Given that the project name is "
myproject" and version "
1.2", running "
gradle customDistZip" will produce a
ZIP file named "
Running "
gradle installCustomDist" will install the distribution contents into "
All of the files in the "
src/" directory will automatically be included in the distribution.
You can add additional files by configuring the
Distribution object that is part of the container.
Example 36.3. Configuring the main distribution
apply plugin: 'distribution' distributions { main { baseName = 'someName' contents { from { 'src/readme' } } } } apply plugin:'maven' uploadArchives { repositories { mavenDeployer { repository(url: "file://some/repo") } } }
In the example above, the content of the "
src/readme" directory will be included in the distribution
(along with the files in the "
src/main/dist" directory which are added by default).
The "
baseName" property has also been changed. This will cause the distribution archives to be created with a different name.
The distribution plugin adds the distribution archives as candidate for default publishing artifacts.
With the
maven plugin applied the distribution zip file will be published when running uploadArchives
if no other default artifact is configured
Example 36.4. publish main distribution
apply plugin:'maven' uploadArchives { repositories { mavenDeployer { repository(url: "file://some/repo") } } }
The Gradle announce plugin allows you to send custom announcements during a build. The following notification systems are supported:
To use the announce plugin, apply it to your build script:
Next, configure your notification service(s) of choice (see table below for which configuration properties are available):
Example 37.2. Configure the announce plugin
announce { username = 'myId' password = 'myPassword' }
Finally, send announcements with the
announce method:
Example 37.3. Using the announce plugin
task helloWorld { doLast { println "Hello, world!" } } helloWorld.doLast { announce.announce("helloWorld completed!", "twitter") announce.announce("helloWorld completed!", "local") }
announce method takes two String arguments: The message to be sent, and the notification
service to be used. The following table lists supported notification services and their configuration properties.
Table 37.1. Announce Plugin Notification Services
|Notification Service
|Operating System
|Configuration Properties
|Further Information
|username, password
|Mac OS X
|Requires the notify-send package to be installed. Use
sudo apt-get install libnotify-bin
to install it.
|Windows, Mac OS X, Ubuntu
|Automatically chooses between snarl, growl, and notify-send depending on the current operating system.
See the
AnnouncePluginExtension class in the API documentation.
The build announcements plugin is currently incubating. Please be aware that the DSL and other configuration may change in later Gradle versions.
The build announcements plugin uses the announce plugin to send local announcements on important events in the build.
To use the build announcements plugin, include the following in your build script:
That's it. If you want to tweak where the announcements go, you can configure the announce plugin to change the local announcer.
You can also apply the plugin from an init script:
Example 38.2. Using the build announcements plugin from an init script
rootProject {
apply plugin: 'build-announcements'
Gradle supports two types of task. One such type is the simple task, where you define the task with an action closure. We have seen these in Chapter 15, Build Script Basics. For this type of task, the action closure determines the behaviour of the task. This type of task is good for implementing one-off tasks in your build script.
The other type of task is the enhanced task, where the behaviour is built into the task, and the task provides some properties which you can use to configure the behaviour. We have seen these in Chapter 18, More about Tasks. Most Gradle plugins use enhanced tasks. With enhanced tasks, you don't need to implement the task behaviour as you do with simple tasks. You simply declare the task and configure the task using its properties. In this way, enhanced tasks let you reuse a piece of behaviour in many different places, possibly across different builds.
The behaviour and properties of an enhanced task is defined by the task's class. When you declare an enhanced task, you specify the type, or class of the task.
Implementing your own custom task class in Gradle is easy. You can implement a custom task class in pretty much any language you like, provided it ends up compiled to bytecode. In our examples, we are going to use Groovy as the implementation language, but you could use, for example, Java or Scala. In general, using Groovy is the easiest option, because the Gradle API is designed to work well with Groovy.
There are several places where you can put the source for the task class.
You can include the task class directly in the build script. This has the benefit that the task class is automatically compiled and included in the classpath of the build script without you having to do anything. However, the task class is not visible outside the build script, and so you cannot reuse the task class outside the build script it is defined in.
buildSrc project
You can put the source for the task class in the
Gradle will take care of compiling and testing the task class and making it available on the
classpath of the build script. The task class is visible to every build script used by the build.
However, it is not visible outside the build, and so you cannot reuse the task class outside the
build it is defined in.
Using the
buildSrc project approach separates
the task declaration - that is, what the task should do - from the task implementation - that is,
how the task does it.
See Chapter 42, Organizing Build Logic for more details about the
You can create a separate project for your task class. This project produces and publishes a JAR which you can then use in multiple builds and share with others. Generally, this JAR might include some custom plugins, or bundle several related task classes into a single library. Or some combination of the two.
In our examples, we will start with the task class in the build script, to keep things simple. Then we will look at creating a standalone project.
To implement a custom task class, you extend
This task doesn't do anything useful, so let's add some behaviour. To do so, we add a method to the task
and mark it with the
TaskAction annotation. Gradle will call the
method when the task executes. You don't have to use a method to define the behaviour for the task. You
could, for instance, call
doFirst() or
doLast() with a closure in the
task constructor to add behaviour.
Example 39.2. A hello world task
task hello(type: GreetingTask) class GreetingTask extends DefaultTask { @TaskAction def greet() { println 'hello from GreetingTask' } }
Output of
gradle -q hello
> gradle -q hello hello from GreetingTask
Let's add a property to the task, so we can customize it. Tasks are simply POGOs, and when you declare a
task, you can set the properties or call methods on the task object. Here we add a
property, and set the value when we declare the
greeting task.
Example 39.3. A customizable hello world task
// Use the default greeting task hello(type: GreetingTask) // Customize the greeting task greeting(type: GreetingTask) { greeting = 'greetings from GreetingTask' } class GreetingTask extends DefaultTask { String greeting = 'hello from GreetingTask' @TaskAction def greet() { println greeting } }
Output of
gradle -q hello greeting
> gradle -q hello greeting hello from GreetingTask greetings from GreetingTask
Now we will move our task to a standalone project, so we can publish it and share it with others. This project is simply a Groovy project that produces a JAR containing the task class. Here is a simple build script for the project. It applies the Groovy plugin, and adds the Gradle API as a compile-time dependency.
Example 39.4. A build for a custom task
apply plugin: 'groovy'
dependencies {
compile gradleApi()
compile localGroovy()
Note: The code for this example can be found at
samples/customPlugin/plugin in the '-all' distribution of Gradle.
We just follow the convention for where the source for the task class should go.
Example 39.5. A custom task
package org.gradle import org.gradle.api.DefaultTask import org.gradle.api.tasks.TaskAction class GreetingTask extends DefaultTask { String greeting = 'hello from GreetingTask' @TaskAction def greet() { println greeting } }
To use a task class in a build script, you need to add the class to the build script's classpath. To
do this, you use a
buildscript { } block, as described in Section 42.6, "External dependencies for the build script".
The following example shows how you might do this when the JAR containing the task class has been published
to a local repository:
Example 39.6. Using a custom task in another project
buildscript { repositories { maven { url uri('../repo') } } dependencies { classpath group: 'org.gradle', name: 'customPlugin', version: '1.0-SNAPSHOT' } } task greeting(type: org.gradle.GreetingTask) { greeting = 'howdy!' }
You can use the
ProjectBuilder class to create
Project instances to use when you test your task class.
Example 39.7. Testing a custom task
class GreetingTaskTest { @Test public void canAddTaskToProject() { Project project = ProjectBuilder.builder().build() def task = project.task('greeting', type: GreetingTask) assertTrue(task instanceof GreetingTask) } }
Incremental tasks are an incubating feature.
Since the introduction of the implementation described above (early in the Gradle 1.6 release cycle), discussions within the Gradle community have produced superior ideas for exposing the information about changes to task implementors to what is described below. As such, the API for this feature will almost certainly change in upcoming releases. However, please do experiment with the current implementation and share your experiences with the Gradle community.
The feature incubation process, which is part of the Gradle feature lifecycle (see Appendix C, The Feature Lifecycle), exists for this purpose of ensuring high quality final implementations through incorporation of early user feedback.
With Gradle, it's very simple to implement a task that gets skipped when all of it's inputs and outputs are up to date (see Section 18.9, "Up-to-date checks (AKA Incremental Build)"). However, there are times when only a few input files have changed since the last execution, and you'd like to avoid reprocessing all of the unchanged inputs. This can be particularly useful for a transformer task, that converts input files to output files on a 1:1 basis.
If you'd like to optimise your build so that only out-of-date inputs are processed, you can do so with an incremental task.
For a task to process inputs incrementally, that task must contain an incremental task action. This is a task action method that contains a
IncrementalTaskInputs parameter, which indicates to Gradle that the action will process the changed inputs only.
The incremental task action may supply an
IncrementalTaskInputs.outOfDate(org.gradle.api.Action) action for processing any input file that is out-of-date,
and a
IncrementalTaskInputs.removed(org.gradle.api.Action) action that executes for any input file that has been removed since the previous execution.
Example 39.8. Defining an incremental task action
class IncrementalReverseTask extends DefaultTask { @InputDirectory def File inputDir @OutputDirectory def File outputDir @Input def inputProperty @TaskAction void execute(IncrementalTaskInputs inputs) { println inputs.incremental ? "CHANGED inputs considered out of date" : "ALL inputs considered out of date" if (!inputs.incremental) project.delete(outputDir.listFiles()) inputs.outOfDate { change -> println "out of date: ${change.file.name}" def targetFile = new File(outputDir, change.file.name) targetFile.text = change.file.text.reverse() } inputs.removed { change -> println "removed: ${change.file.name}" def targetFile = new File(outputDir, change.file.name) targetFile.delete() } } }
Note: The code for this example can be found at
samples/userguide/tasks/incrementalTask in the '-all' distribution of Gradle.
If for some reason the task is not run incremental, e.g. by running with --rerun-tasks, only the outOfDate action is executed, even if there where deleted input files. You should consider handling this case at the beginning, as is done in the example above.
For a simple transformer task like this, the task action simply needs to generate output files for any out-of-date inputs, and delete output files for any removed inputs.
A task may only contain a single incremental task action.
When Gradle has history of a previous task execution, and the only changes to the task execution context since that execution are to input files,
then Gradle is able to determine which input files need to be reprocessed by the task.
In this case, the
IncrementalTaskInputs.outOfDate(org.gradle.api.Action) action will be executed for any input file that was added or modified,
and the
IncrementalTaskInputs.removed(org.gradle.api.Action) action will be executed for any removed input file.
However, there are many cases where Gradle is unable to determine which input files need to be reprocessed. Examples include:
upToDateWhen criteria added to the task returns
In any of these cases, Gradle will consider all of the input files to be
IncrementalTaskInputs.outOfDate(org.gradle.api.Action) action will be executed for every input file, and the
IncrementalTaskInputs.removed(org.gradle.api.Action) action will not be executed at all.
You can check if Gradle was able to determine the incremental changes to input files with
Given the incremental task implementation above, we can explore the various change scenarios by example. Note that the various mutation tasks ('updateInputs', 'removeInput', etc) are only present for demonstration purposes: these would not normally be part of your build script.
First, consider the
IncrementalReverseTask executed against a set of inputs for the first time.
In this case, all inputs will be considered "out of date":
Example 39.9. Running the incremental task for the first time
task incrementalReverse(type: IncrementalReverseTask) { inputDir = file('inputs') outputDir = file("$buildDir/outputs") inputProperty = project.properties['taskInputProperty'] ?: "original" }
Build layout
incrementalTask/ build.gradle inputs/ 1.txt 2.txt 3.txt
Output of
gradle -q incrementalReverse
> gradle -q incrementalReverse ALL inputs considered out of date out of date: 1.txt out of date: 2.txt out of date: 3.txt
Naturally when the task is executed again with no changes, then the entire task is up to date and no files are reported to the task action:
Example 39.10. Running the incremental task with unchanged inputs
Output of
gradle -q incrementalReverse
> gradle -q incrementalReverse
When an input file is modified in some way or a new input file is added, then re-executing the task results in those files being reported to
Example 39.11. Running the incremental task with updated input files
task updateInputs() { doLast { file('inputs/1.txt').text = "Changed content for existing file 1." file('inputs/4.txt').text = "Content for new file 4." } }
Output of
gradle -q updateInputs incrementalReverse
> gradle -q updateInputs incrementalReverse CHANGED inputs considered out of date out of date: 1.txt out of date: 4.txt
When an existing input file is removed, then re-executing the task results in that file being reported to
Example 39.12. Running the incremental task with an input file removed
task removeInput() {
doLast {
Output of
gradle -q removeInput incrementalReverse
> gradle -q removeInput incrementalReverse CHANGED inputs considered out of date removed: 3.txt
When an output file is deleted (or modified), then Gradle is unable to determine which input files are out of date.
In this case, all input files are reported to the
IncrementalTaskInputs.outOfDate(org.gradle.api.Action) action,
and no input files are reported to the
IncrementalTaskInputs.removed(org.gradle.api.Action) action:
Example 39.13. Running the incremental task with an output file removed
task removeOutput() {
doLast {
Output of
gradle -q removeOutput incrementalReverse
> gradle -q removeOutput incrementalReverse ALL inputs considered out of date out of date: 1.txt out of date: 2.txt out of date: 3.txt
When a task input property is modified, Gradle is unable to determine how this property impacted the task outputs, so all input files are assumed to be out of date.
So similar to the changed output file example, all input files are reported to
IncrementalTaskInputs.outOfDate(org.gradle.api.Action) action,
and no input files are reported to the
IncrementalTaskInputs.removed(org.gradle.api.Action) action:
Example 39.14. Running the incremental task with an input property changed
Output of
gradle -q -PtaskInputProperty=changed incrementalReverse
> gradle -q -PtaskInputProperty=changed incrementalReverse ALL inputs considered out of date out of date: 1.txt out of date: 2.txt out of date: 3.txt
A Gradle plugin packages up reusable pieces of build logic, which can be used across many different projects and builds. Gradle allows you to implement your own custom plugins, so you can reuse your build logic, and share it with others.
You can implement a custom plugin in any language you like, provided the implementation ends up compiled as bytecode. For the examples here, we are going to use Groovy as the implementation language. You could use Java or Scala instead, if you want.
There are several places where you can put the source for the plugin.
You can include the source for the plugin directly in the build script. This has the benefit that the plugin is automatically compiled and included in the classpath of the build script without you having to do anything. However, the plugin is not visible outside the build script, and so you cannot reuse the plugin outside the build script it is defined in.
buildSrc project
You can put the source for the plugin in the
Gradle will take care of compiling and testing the plugin and making it available on the
classpath of the build script. The plugin is visible to every build script used by the build.
However, it is not visible outside the build, and so you cannot reuse the plugin outside the
build it is defined in.
See Chapter 42, Organizing Build Logic for more details about the
You can create a separate project for your plugin. This project produces and publishes a JAR which you can then use in multiple builds and share with others. Generally, this JAR might include some custom plugins, or bundle several related task classes into a single library. Or some combination of the two.
In our examples, we will start with the plugin in the build script, to keep things simple. Then we will look at creating a standalone project.
To create a custom plugin, you need to write an implementation of
Gradle instantiates the plugin and calls the plugin instance's
Plugin.apply(T) method when the
plugin is used with a project. The project
object is passed as a parameter, which the plugin can use to configure the project however it needs to.
The following sample contains a greeting plugin, which adds a
hello task to the project.
Example 40.1. A custom plugin
apply plugin: GreetingPlugin class GreetingPlugin implements Plugin<Project> { void apply(Project project) { project.task('hello') { doLast { println "Hello from the GreetingPlugin" } } } }
Output of
gradle -q hello
> gradle -q hello Hello from the GreetingPlugin
One thing to note is that a new instance of a given plugin is created for each project it is applied to. Also
note that the
Plugin class is a generic type. This example has it receiving the
Project type as a type parameter. It's possible to write unusual custom
plugins that take different type parameters, but this will be unlikely (until someone figures out more
creative things to do here).
Most plugins need to obtain some configuration from the build script. One method for doing this is to use extension objects.
The Gradle
Project has an associated
ExtensionContainer object
that helps keep track of all the settings and properties being passed to plugins. You can capture user input by telling
the extension container about your plugin. To capture input, simply add a Java Bean compliant class into the extension container's list of
extensions. Groovy is a good language choice for a plugin because plain old Groovy objects contain all the getter and setter methods
that a Java Bean requires.
Let's add a simple extension object to the project. Here we add a
greeting extension object to the
project, which allows you to configure the greeting.
Example 40.2. A custom plugin extension
apply plugin: GreetingPlugin greeting.message = 'Hi from Gradle' class GreetingPlugin implements Plugin<Project> { void apply(Project project) { // Add the 'greeting' extension object project.extensions.create("greeting", GreetingPluginExtension) // Add a task that uses the configuration project.task('hello') { doLast { println project.greeting.message } } } } class GreetingPluginExtension { def String message = 'Hello from GreetingPlugin' }
Output of
gradle -q hello
> gradle -q hello Hi from Gradle
In this example,
GreetingPluginExtension is a plain old Groovy object with a field called
The extension object is added to the plugin list with the name
greeting. This object then becomes available as a project property
with the same name as the extension object.
Oftentimes, you have several related properties you need to specify on a single plugin. Gradle adds a configuration closure block for each extension object, so you can group settings together. The following example shows you how this works.
Example 40.3. A custom plugin with configuration closure
apply plugin: GreetingPlugin greeting { message = 'Hi' greeter = 'Gradle' } class GreetingPlugin implements Plugin<Project> { void apply(Project project) { project.extensions.create("greeting", GreetingPluginExtension) project.task('hello') { doLast { println "${project.greeting.message} from ${project.greeting.greeter}" } } } } class GreetingPluginExtension { String message String greeter }
Output of
gradle -q hello
> gradle -q hello Hi from Gradle
In this example, several settings can be grouped together within the
greeting closure.
The name of the closure block in the build script (
greeting) needs to match the extension object name.
Then, when the closure is executed, the fields on the extension object will be mapped to the variables within the closure
based on the standard Groovy closure delegate feature.