Gradle User Guide Version 3.1

Gradle User Guide

Version 3.1

Copies of this document may be made for your own use and for distribution to others, provided that you do not charge any fee for such copies and further provided that each copy contains this Copyright Notice, whether distributed in print or electronically.

Table of Contents

I. About Gradle
1. Introduction
2. Overview
II. Working with existing builds
3. Installing Gradle
4. Using the Gradle Command-Line
5. The Gradle Wrapper
6. The Gradle Daemon
7. Dependency Management Basics
8. Introduction to multi-project builds
9. Continuous build
10. Composite builds
11. Using the Gradle Graphical User Interface
12. The Build Environment
13. Troubleshooting
14. Embedding Gradle using the Tooling API
III. Writing Gradle build scripts
15. Build Script Basics
16. Build Init Plugin
17. Writing Build Scripts
18. More about Tasks
19. Working With Files
20. Using Ant from Gradle
21. The Build Lifecycle
22. Wrapper Plugin
23. Logging
24. Dependency Management
25. Multi-project Builds
26. Gradle Plugins
27. Standard Gradle plugins
28. The Project Report Plugin
29. The Build Dashboard Plugin
30. Comparing Builds
31. Publishing artifacts
32. The Maven Plugin
33. The Signing Plugin
34. Ivy Publishing (new)
35. Maven Publishing (new)
36. The Distribution Plugin
37. The Announce Plugin
38. The Build Announcements Plugin
IV. Extending the build
39. Writing Custom Task Classes
40. Writing Custom Plugins
41. The Java Gradle Plugin Development Plugin
42. Organizing Build Logic
43. Initialization Scripts
44. The Gradle TestKit
V. Building JVM projects
45. Java Quickstart
46. The Java Plugin
47. Web Application Quickstart
48. The War Plugin
49. The Ear Plugin
50. The Jetty Plugin
51. The Application Plugin
52. The Java Library Distribution Plugin
53. Groovy Quickstart
54. The Groovy Plugin
55. The Scala Plugin
56. The ANTLR Plugin
57. The Checkstyle Plugin
58. The CodeNarc Plugin
59. The FindBugs Plugin
60. The JDepend Plugin
61. The PMD Plugin
62. The JaCoCo Plugin
63. The OSGi Plugin
64. The Eclipse Plugins
65. The IDEA Plugin
VI. The Software model
66. Rule based model configuration
67. Software model concepts
68. Implementing model rules in a plugin
69. Building Java Libraries
70. Building Play applications
71. Building native software
72. Extending the software model
VII. Appendix
A. Gradle Samples
B. Potential Traps
C. The Feature Lifecycle
D. Gradle Command Line
E. Documentation licenses
Glossary

List of Examples

4.1. Executing multiple tasks
4.2. Excluding tasks
4.3. Abbreviated task name
4.4. Abbreviated camel case task name
4.5. Selecting the project using a build file
4.6. Selecting the project using project directory
4.7. Forcing tasks to run
4.8. Obtaining information about projects
4.9. Providing a description for a project
4.10. Obtaining information about tasks
4.11. Changing the content of the task report
4.12. Obtaining more information about tasks
4.13. Obtaining detailed help for tasks
4.14. Obtaining information about dependencies
4.15. Filtering dependency report by configuration
4.16. Getting the insight into a particular dependency
4.17. Information about properties
5.1. Running the Wrapper task
5.2. Wrapper task
5.3. Wrapper generated files
5.4. Generating a SHA-256 hash
5.5. Configuring SHA-256 checksum verification
7.1. Declaring dependencies
7.2. Definition of an external dependency
7.3. Shortcut definition of an external dependency
7.4. Usage of Maven central repository
7.5. Usage of JCenter repository
7.6. Usage of a remote Maven repository
7.7. Usage of a remote Ivy directory
7.8. Usage of a local Ivy directory
7.9. Publishing to an Ivy repository
7.10. Publishing to a Maven repository
8.1. Listing the projects in a build
10.1. Dependencies of my-app
10.2. Declaring a command-line composite
10.3. Declaring a composite via settings.gradle
10.4. Declaring a separate composite
10.5. Depending on task from included build
10.6. Build that does not declare group attribute
10.7. Declaring the substitutions for an included build
10.8. Depending on a single task from an included build
10.9. Depending on a tasks with path in all included builds
11.1. Launching the GUI
12.1. Setting properties with a gradle.properties file
12.2. Configuring an HTTP proxy
12.3. Configuring an HTTPS proxy
14.1. Using the tooling API
15.1. Your first build script
15.2. Execution of a build script
15.3. A task definition shortcut
15.4. Using Groovy in Gradle's tasks
15.5. Using Groovy in Gradle's tasks
15.6. Declaration of task that depends on other task
15.7. Lazy dependsOn - the other task does not exist (yet)
15.8. Dynamic creation of a task
15.9. Accessing a task via API - adding a dependency
15.10. Accessing a task via API - adding behaviour
15.11. Accessing task as a property of the build script
15.12. Adding extra properties to a task
15.13. Using AntBuilder to execute ant.loadfile target
15.14. Using methods to organize your build logic
15.15. Defining a default task
15.16. Different outcomes of build depending on chosen tasks
17.1. Accessing property of the Project object
17.2. Using local variables
17.3. Using extra properties
17.4. Configuring arbitrary objects
17.5. Configuring arbitrary objects using a script
17.6. Groovy JDK methods
17.7. Property accessors
17.8. Method call without parentheses
17.9. List and map literals
17.10. Closure as method parameter
17.11. Closure delegates
18.1. Defining tasks
18.2. Defining tasks - using strings for task names
18.3. Defining tasks with alternative syntax
18.4. Accessing tasks as properties
18.5. Accessing tasks via tasks collection
18.6. Accessing tasks by path
18.7. Creating a copy task
18.8. Configuring a task - various ways
18.9. Configuring a task - with closure
18.10. Defining a task with closure
18.11. Adding dependency on task from another project
18.12. Adding dependency using task object
18.13. Adding dependency using closure
18.14. Adding a 'must run after' task ordering
18.15. Adding a 'should run after' task ordering
18.16. Task ordering does not imply task execution
18.17. A 'should run after' task ordering is ignored if it introduces an ordering cycle
18.18. Adding a description to a task
18.19. Overwriting a task
18.20. Skipping a task using a predicate
18.21. Skipping tasks with StopExecutionException
18.22. Enabling and disabling tasks
18.23. Custom task class
18.24. Ad-hoc task
18.25. Using runtime API with custom task type
18.26. Using skipWhenEmpty() via the runtime API
18.27. Inferred task dependency via task outputs
18.28. Inferred task dependency via a task argument
18.29. Declaring a method to add task inputs
18.30. Declaring a method to add a task as an input
18.31. Failed attempt at setting up an inferred task dependency
18.32. Setting up an inferred task dependency between output dir and input files
18.33. Setting up an inferred task dependency with files()
18.34. Setting up an inferred task dependency with builtBy()
18.35. Ignoring up-to-date checks
18.36. Task rule
18.37. Dependency on rule based tasks
18.38. Adding a task finalizer
18.39. Task finalizer for a failing task
19.1. Locating files
19.2. Creating a file collection
19.3. Using a file collection
19.4. Implementing a file collection
19.5. Creating a file tree
19.6. Using a file tree
19.7. Using an archive as a file tree
19.8. Specifying a set of files
19.9. Copying files using the copy task
19.10. Specifying copy task source files and destination directory
19.11. Selecting the files to copy
19.12. Copying files using the copy() method without up-to-date check
19.13. Copying files using the copy() method with up-to-date check
19.14. Renaming files as they are copied
19.15. Filtering files as they are copied
19.16. Nested copy specs
19.17. Using the Sync task to copy dependencies
19.18. Creating a ZIP archive
19.19. Creation of ZIP archive
19.20. Configuration of archive task - custom archive name
19.21. Configuration of archive task - appendix & classifier
20.1. Using an Ant task
20.2. Passing nested text to an Ant task
20.3. Passing nested elements to an Ant task
20.4. Using an Ant type
20.5. Using a custom Ant task
20.6. Declaring the classpath for a custom Ant task
20.7. Using a custom Ant task and dependency management together
20.8. Importing an Ant build
20.9. Task that depends on Ant target
20.10. Adding behaviour to an Ant target
20.11. Ant target that depends on Gradle task
20.12. Renaming imported Ant targets
20.13. Setting an Ant property
20.14. Getting an Ant property
20.15. Setting an Ant reference
20.16. Getting an Ant reference
20.17. Fine tuning Ant logging
21.1. Single project build
21.2. Hierarchical layout
21.3. Flat layout
21.4. Modification of elements of the project tree
21.5. Adding of test task to each project which has certain property set
21.6. Notifications
21.7. Setting of certain property to all tasks
21.8. Logging of start and end of each task execution
23.1. Using stdout to write log messages
23.2. Writing your own log messages
23.3. Using SLF4J to write log messages
23.4. Configuring standard output capture
23.5. Configuring standard output capture for a task
23.6. Customizing what Gradle logs
24.1. Definition of a configuration
24.2. Accessing a configuration
24.3. Configuration of a configuration
24.4. Module dependencies
24.5. Artifact only notation
24.6. Dependency with classifier
24.7. Iterating over a configuration
24.8. Client module dependencies - transitive dependencies
24.9. Project dependencies
24.10. File dependencies
24.11. Generated file dependencies
24.12. Gradle API dependencies
24.13. Gradle's Groovy dependencies
24.14. Excluding transitive dependencies
24.15. Optional attributes of dependencies
24.16. Collections and arrays of dependencies
24.17. Dependency configurations
24.18. Dependency configurations for project
24.19. Configuration.copy
24.20. Accessing declared dependencies
24.21. Configuration.files
24.22. Configuration.files with spec
24.23. Configuration.copy
24.24. Configuration.copy vs. Configuration.files
24.25. Adding central Maven repository
24.26. Adding Bintray's JCenter Maven repository
24.27. Using Bintrays's JCenter with HTTP
24.28. Adding the local Maven cache as a repository
24.29. Adding custom Maven repository
24.30. Adding additional Maven repositories for JAR files
24.31. Accessing password protected Maven repository
24.32. Flat repository resolver
24.33. Ivy repository
24.34. Ivy repository with named layout
24.35. Ivy repository with pattern layout
24.36. Ivy repository with multiple custom patterns
24.37. Ivy repository with Maven compatible layout
24.38. Ivy repository
24.39. Declaring a Maven and Ivy repository
24.40. Providing credentials to a Maven and Ivy repository
24.41. Declaring a S3 backed Maven and Ivy repository
24.42. Configure repository to use only digest authentication
24.43. Configure repository to use preemptive authentication
24.44. Accessing a repository
24.45. Configuration of a repository
24.46. Definition of a custom repository
24.47. Forcing consistent version for a group of libraries
24.48. Using a custom versioning scheme
24.49. Blacklisting a version with a replacement
24.50. Changing dependency group and/or name at the resolution
24.51. Substituting a module with a project
24.52. Substituting a project with a module
24.53. Conditionally substituting a dependency
24.54. Specifying default dependencies on a configuration
24.55. Enabling dynamic resolve mode
24.56. 'Latest' version selector
24.57. Custom status scheme
24.58. Custom status scheme by module
24.59. Ivy component metadata rule
24.60. Rule source component metadata rule
24.61. Component selection rule
24.62. Component selection rule with module target
24.63. Component selection rule with metadata
24.64. Component selection rule using a rule source object
24.65. Declaring module replacement
24.66. Dynamic version cache control
24.67. Changing module cache control
25.1. Multi-project tree - water & bluewhale projects
25.2. Build script of water (parent) project
25.3. Multi-project tree - water, bluewhale & krill projects
25.4. Water project build script
25.5. Defining common behavior of all projects and subprojects
25.6. Defining specific behaviour for particular project
25.7. Defining specific behaviour for project krill
25.8. Adding custom behaviour to some projects (filtered by project name)
25.9. Adding custom behaviour to some projects (filtered by project properties)
25.10. Running build from subproject
25.11. Evaluation and execution of projects
25.12. Evaluation and execution of projects
25.13. Running tasks by their absolute path
25.14. Dependencies and execution order
25.15. Dependencies and execution order
25.16. Dependencies and execution order
25.17. Declaring dependencies
25.18. Declaring dependencies
25.19. Cross project task dependencies
25.20. Configuration time dependencies
25.21. Configuration time dependencies - evaluationDependsOn
25.22. Configuration time dependencies
25.23. Dependencies - real life example - crossproject configuration
25.24. Project lib dependencies
25.25. Project lib dependencies
25.26. Fine grained control over dependencies
25.27. Build and Test Single Project
25.28. Partial Build and Test Single Project
25.29. Build and Test Depended On Projects
25.30. Build and Test Dependent Projects
26.1. Applying a script plugin
26.2. Applying a core plugin
26.3. Applying a community plugin
26.4. Applying plugins only on certain subprojects.
26.5. Using plugins from custom plugin repositories.
26.6. Complete Plugin Publishing Sample
26.7. Applying a binary plugin
26.8. Applying a binary plugin by type
26.9. Applying a plugin with the buildscript block
29.1. Using the Build Dashboard plugin
31.1. Defining an artifact using an archive task
31.2. Defining an artifact using a file
31.3. Customizing an artifact
31.4. Map syntax for defining an artifact using a file
31.5. Configuration of the upload task
32.1. Using the Maven plugin
32.2. Creating a stand alone pom.
32.3. Upload of file to remote Maven repository
32.4. Upload of file via SSH
32.5. Customization of pom
32.6. Builder style customization of pom
32.7. Modifying auto-generated content
32.8. Customization of Maven installer
32.9. Generation of multiple poms
32.10. Accessing a mapping configuration
33.1. Using the Signing plugin
33.2. Signing a configuration
33.3. Signing a configuration output
33.4. Signing a task
33.5. Signing a task output
33.6. Conditional signing
33.7. Signing a POM for deployment
34.1. Applying the “ivy-publish” plugin
34.2. Publishing a Java module to Ivy
34.3. Publishing additional artifact to Ivy
34.4. customizing the publication identity
34.5. Customizing the module descriptor file
34.6. Publishing multiple modules from a single project
34.7. Declaring repositories to publish to
34.8. Choosing a particular publication to publish
34.9. Publishing all publications via the “publish” lifecycle task
34.10. Generating the Ivy module descriptor file
34.11. Publishing a Java module
34.12. Example generated ivy.xml
35.1. Applying the 'maven-publish' plugin
35.2. Adding a MavenPublication for a Java component
35.3. Adding additional artifact to a MavenPublication
35.4. customizing the publication identity
35.5. Modifying the POM file
35.6. Publishing multiple modules from a single project
35.7. Declaring repositories to publish to
35.8. Publishing a project to a Maven repository
35.9. Publish a project to the Maven local repository
35.10. Generate a POM file without publishing
36.1. Using the distribution plugin
36.2. Adding extra distributions
36.3. Configuring the main distribution
36.4. publish main distribution
37.1. Using the announce plugin
37.2. Configure the announce plugin
37.3. Using the announce plugin
38.1. Using the build announcements plugin
38.2. Using the build announcements plugin from an init script
39.1. Defining a custom task
39.2. A hello world task
39.3. A customizable hello world task
39.4. A build for a custom task
39.5. A custom task
39.6. Using a custom task in another project
39.7. Testing a custom task
39.8. Defining an incremental task action
39.9. Running the incremental task for the first time
39.10. Running the incremental task with unchanged inputs
39.11. Running the incremental task with updated input files
39.12. Running the incremental task with an input file removed
39.13. Running the incremental task with an output file removed
39.14. Running the incremental task with an input property changed
40.1. A custom plugin
40.2. A custom plugin extension
40.3. A custom plugin with configuration closure
40.4. Evaluating file properties lazily
40.5. A build for a custom plugin
40.6. Wiring for a custom plugin
40.7. Using a custom plugin in another project
40.8. Applying a community plugin with the plugins DSL
40.9. Testing a custom plugin
40.10. Using the Java Gradle Plugin Development plugin
40.11. Managing domain objects
41.1. Using the Java Gradle Plugin Development plugin
41.2. Using the gradlePlugin {} block.
42.1. Using inherited properties and methods
42.2. Using injected properties and methods
42.3. Configuring the project using an external build script
42.4. Custom buildSrc build script
42.5. Adding subprojects to the root buildSrc project
42.6. Running another build from a build
42.7. Declaring external dependencies for the build script
42.8. A build script with external dependencies
42.9. Ant optional dependencies
43.1. Using init script to perform extra configuration before projects are evaluated
43.2. Declaring external dependencies for an init script
43.3. An init script with external dependencies
43.4. Using plugins in init scripts
44.1. Declaring the TestKit dependency
44.2. Declaring the JUnit dependency
44.3. Using GradleRunner with JUnit
44.4. Using GradleRunner with Spock
44.5. Making the code under test classpath available to the tests
44.6. Injecting the code under test classes into test builds
44.7. Using the Java Gradle Development plugin for generating the plugin metadata
44.8. Automatically injecting the code under test classes into test builds
44.9. Reconfiguring the classpath generation conventions of the Java Gradle Development plugin
44.10. Specifying a Gradle version for test execution
45.1. Using the Java plugin
45.2. Building a Java project
45.3. Adding Maven repository
45.4. Adding dependencies
45.5. Customization of MANIFEST.MF
45.6. Adding a test system property
45.7. Publishing the JAR file
45.8. Eclipse plugin
45.9. Java example - complete build file
45.10. Multi-project build - hierarchical layout
45.11. Multi-project build - settings.gradle file
45.12. Multi-project build - common configuration
45.13. Multi-project build - dependencies between projects
45.14. Multi-project build - distribution file
46.1. Using the Java plugin
46.2. Custom Java source layout
46.3. Accessing a source set
46.4. Configuring the source directories of a source set
46.5. Defining a source set
46.6. Defining source set dependencies
46.7. Compiling a source set
46.8. Assembling a JAR for a source set
46.9. Generating the Javadoc for a source set
46.10. Running tests in a source set
46.11. Filtering tests in the build script
46.12. JUnit Categories
46.13. Grouping TestNG tests
46.14. Preserving order of TestNG tests
46.15. Grouping TestNG tests by instances
46.16. Creating a unit test report for subprojects
46.17. Customization of MANIFEST.MF
46.18. Creating a manifest object.
46.19. Separate MANIFEST.MF for a particular archive
46.20. Configure Java 6 build
47.1. War plugin
47.2. Running web application with Jetty plugin
48.1. Using the War plugin
48.2. Customization of war plugin
49.1. Using the Ear plugin
49.2. Customization of ear plugin
50.1. Using the Jetty plugin
51.1. Using the application plugin
51.2. Configure the application main class
51.3. Configure default JVM settings
51.4. Include output from other tasks in the application distribution
51.5. Automatically creating files for distribution
52.1. Using the Java library distribution plugin
52.2. Configure the distribution name
52.3. Include files in the distribution
53.1. Groovy plugin
53.2. Dependency on Groovy
53.3. Groovy example - complete build file
54.1. Using the Groovy plugin
54.2. Custom Groovy source layout
54.3. Configuration of Groovy dependency
54.4. Configuration of Groovy test dependency
54.5. Configuration of bundled Groovy dependency
54.6. Configuration of Groovy file dependency
54.7. Configure Java 6 build for Groovy
55.1. Using the Scala plugin
55.2. Custom Scala source layout
55.3. Declaring a Scala dependency for production code
55.4. Declaring a Scala dependency for test code
55.5. Declaring a version of the Zinc compiler to use
55.6. Forcing a scala-library dependency for all configurations
55.7. Forcing a scala-library dependency for the zinc configuration
55.8. Adjusting memory settings
55.9. Forcing all code to be compiled
55.10. Configure Java 6 build for Scala
55.11. Explicitly specify a target IntelliJ IDEA version
56.1. Using the ANTLR plugin
56.2. Declare ANTLR version
56.3. setting custom max heap size and extra arguments for ANTLR
57.1. Using the Checkstyle plugin
57.2. Customizing the HTML report
58.1. Using the CodeNarc plugin
59.1. Using the FindBugs plugin
59.2. Customizing the HTML report
60.1. Using the JDepend plugin
61.1. Using the PMD plugin
62.1. Applying the JaCoCo plugin
62.2. Configuring JaCoCo plugin settings
62.3. Configuring test task
62.4. Configuring test task
62.5. Using application plugin to generate code coverage data
62.6. Coverage reports generated by applicationCodeCoverageReport
63.1. Using the OSGi plugin
63.2. Configuration of OSGi MANIFEST.MF file
64.1. Using the Eclipse plugin
64.2. Using the Eclipse WTP plugin
64.3. Partial Overwrite for Classpath
64.4. Partial Overwrite for Project
64.5. Export Dependencies
64.6. Customizing the XML
65.1. Using the IDEA plugin
65.2. Partial Rewrite for Module
65.3. Partial Rewrite for Project
65.4. Export Dependencies
65.5. Customizing the XML
66.1. applying a rule source plugin
66.2. a model creation rule
66.3. a model mutation rule
66.4. creating a task
66.5. a managed type
66.6. a String property
66.7. a File property
66.8. a Long property
66.9. a boolean property
66.10. an int property
66.11. a managed property
66.12. an enumeration type property
66.13. a managed set
66.14. strongly modelling sources sets
66.15. a DSL example applying a rule to every element in a scope
66.16. DSL configuration rule
66.17. Configuration run when required
66.18. Configuration not run when not required
66.19. DSL creation rule
66.20. DSL creation rule without initialization
66.21. Initialization before configuration
66.22. Nested DSL creation rule
66.23. Nested DSL configuration rule
66.24. DSL configuration rule for each element in a map
66.25. Nested DSL property configuration
66.26. a DSL example showing type conversions
66.27. a DSL rule using inputs
66.28. model task output
69.1. Using the Java software plugins
69.2. Creating a java library
69.3. Configuring a source set
69.4. Creating a new source set
69.5. The components report
69.6. Declaring a dependency onto a library
69.7. Declaring a dependency onto a project with an explicit library
69.8. Declaring a dependency onto a project with an implicit library
69.9. Declaring a dependency onto a library published to a Maven repository
69.10. Declaring a module dependency using shorthand notation
69.11. Configuring repositories for dependency resolution
69.12. Specifying api packages
69.13. Specifying api dependencies
69.14. Main sources
69.15. Client component
69.16. Broken client component
69.17. Recompiling the client
69.18. Declaring target platforms
69.19. Declaring binary specific sources
69.20. Declaring target platforms
69.21. Using the JUnit plugin
69.22. Executing the test suite
69.23. Executing the test suite
69.24. Declaring a component under test
69.25. Declaring local Java installations
70.1. Using the Play plugin
70.2. The components report
70.3. Selecting a version of the Play Framework
70.4. Adding dependencies to a Play application
70.5. Adding extra source sets to a Play application
70.6. Configuring Scala compiler options
70.7. Configuring routes style
70.8. Configuring a custom asset pipeline
70.9. Configuring dependencies on Play subprojects
70.10. Add extra files to a Play application distribution
70.11. Applying both the Play and IDEA plugins
71.1. Defining a library component
71.2. Defining executable components
71.3. The components report
71.4. The 'cpp' plugin
71.5. C++ source set
71.6. The 'c' plugin
71.7. C source set
71.8. The 'assembler' plugin
71.9. The 'objective-c' plugin
71.10. The 'objective-cpp' plugin
71.11. Settings that apply to all binaries
71.12. Settings that apply to all shared libraries
71.13. Settings that apply to all binaries produced for the 'main' executable component
71.14. Settings that apply only to shared libraries produced for the 'main' library component
71.15. The 'windows-resources' plugin
71.16. Configuring the location of Windows resource sources
71.17. Building a resource-only dll
71.18. Providing a library dependency to the source set
71.19. Providing a library dependency to the binary
71.20. Declaring project dependencies
71.21. Creating a precompiled header file
71.22. Including a precompiled header file in a source file
71.23. Configuring a precompiled header
71.24. Defining build types
71.25. Configuring debug binaries
71.26. Defining platforms
71.27. Defining flavors
71.28. Targeting a component at particular platforms
71.29. Building all possible variants
71.30. Defining tool chains
71.31. Reconfigure tool arguments
71.32. Defining target platforms
71.33. Registering CUnit tests
71.34. Running CUnit tests
71.35. Registering GoogleTest tests
72.1. an example of using a custom software model
72.2. Declare a custom component
72.3. Register a custom component
72.4. Declare a custom binary
72.5. Register a custom binary
72.6. Declare a custom source set
72.7. Register a custom source set
72.8. Generates documentation binaries
72.9. Generates tasks for text source sets
72.10. Register a custom source set
72.11. an example of using a custom software model
72.12. foo bar
72.13. public type and internal view declaration
72.14. type registration
72.15. public and internal data mutation
72.16. example build script and model report output
B.1. Variables scope: local and script wide
B.2. Distinct configuration and execution phase