-
Book Overview & Buying
-
Table Of Contents
Gradle Dependency Management
By :
In Gradle, we define dependency configurations to group dependencies together. A dependency configuration has a name and several properties, such as a description and is actually a special type of FileCollection. Configurations can extend from each other, so we can build a hierarchy of configurations in our build files. Gradle plugins can also add new configurations to our project, for example, the Java plugin adds several new configurations, such as compile and testRuntime, to our project. The compile configuration is then used to define the dependencies that are needed to compile our source tree. The dependency configurations are defined with a configurations configuration block. Inside the block, we can define new configurations for our build. All configurations are added to the project's ConfigurationContainer object.
In the following example build file, we define two new configurations, where the traffic configuration extends from the vehicles configuration. This means that any dependency added to the vehicles configuration is also available in the traffic configuration. We can also assign a description property to our configuration to provide some more information about the configuration for documentation purposes. The following code shows this:
// Define new configurations for build.
configurations {
// Define configuration vehicles.
vehicles {
description = 'Contains vehicle dependencies'
}
traffic {
extendsFrom vehicles
description = 'Contains traffic dependencies'
}
}To see which configurations are available in a project, we can execute the dependencies task. This task is available for each Gradle project. The task outputs all the configurations and dependencies of a project. Let's run this task for our current project and check the output:
$ gradle -q dependencies ------------------------------------------------------------ Root project ------------------------------------------------------------ traffic - Contains traffic dependencies No dependencies vehicles - Contains vehicle dependencies No dependencies
Note that we can see our two configurations, traffic and vehicles, in the output. We have not defined any dependencies to these configurations, as shown in the output.
The Java plugin adds a couple of configurations to a project, which are used by the tasks from the Java plugin. Let's add the Java plugin to our Gradle build file:
apply plugin: 'java'
To see which configurations are added, we invoke the dependencies task and look at the output:
$ gradle -q dependencies ------------------------------------------------------------ Root project ------------------------------------------------------------ archives - Configuration for archive artifacts. No dependencies compile - Compile classpath for source set 'main'. No dependencies default - Configuration for default artifacts. No dependencies runtime - Runtime classpath for source set 'main'. No dependencies testCompile - Compile classpath for source set 'test'. No dependencies testRuntime - Runtime classpath for source set 'test'. No dependencies
We see six configurations in our project just by adding the Java plugin. The archives configuration is used to group the artifacts our project creates. The other configurations are used to group the dependencies for our project. In the following table, the dependency configurations are summarized:
|
Name |
Extends |
Description |
|---|---|---|
|
compile |
none |
These are dependencies to compile. |
|
runtime |
compile |
These are runtime dependencies. |
|
testCompile |
compile |
These are extra dependencies to compile tests. |
|
testRuntime |
runtime, testCompile |
These are extra dependencies to run tests. |
|
default |
runtime |
These are dependencies used by this project and artifacts created by this project. |
Later in the chapter, we will see how we can work with the dependencies assigned to the configurations. In the next section, we will learn how to declare our project's dependencies.
Change the font size
Change margin width
Change background colour