[[dependency_management_in_gradle]]
= Dependency Management

Software projects rarely work in isolation.
Projects often rely on reusable functionality from libraries.
Some projects organize unrelated functionality into separate parts of a modular system.
Dependency management is an automated technique for declaring, resolving and using functionality required by a project.

[TIP]
====
For an overview of dependency management terms, see <<dependency_management_terminology.adoc#dependency_management_terminology,Dependency Management Terminology>>.
====

[[sec:dependency-mgmt-in-gradle]]
== Dependency Management in Gradle

[.inset]
image::dependency-management-resolution.png[]

Gradle has built-in support for dependency management.

We’ll explore the main concepts with the help of the theoretical but common project to define terms:

* This project builds Java source code.
* Some of the Java source files import classes from link:https://github.com/google/guava[Google Guava], a open-source library.
* This project uses link:http://junit.org/junit5/[JUnit] for testing.

This project uses Guava and JUnit as _dependencies_.
Gradle fetches dependencies from _repositories_.
You can <<declaring_repositories.adoc#declaring-repositories,declare repositories>> to tell Gradle where to fetch dependencies.
Repositories offer dependencies in multiple formats.
For information about the formats supported by Gradle, see <<declaring_dependencies.adoc#sec:dependency-types,dependency types>>.

A build script developer can <<declaring_dependencies.adoc#declaring-dependencies,declare dependencies>> for different scopes.
For example, Gradle could use one scope to compile source code and another to execute tests.
Gradle calls the <<declaring_dependencies.adoc#sec:what-are-dependency-configurations,scope of a dependency>> a _configuration_.

During a build, Gradle locates the dependencies needed for the requested tasks.
The dependencies might need to be downloaded from a remote repository, retrieved from a local directory, or (in a multi-project setting) built from another project.
This process is called _dependency resolution_.
For more information, see <<dependency_resolution.adoc#sec:how-gradle-downloads-deps,How Gradle downloads dependencies>>.

Gradle <<dependency_resolution.adoc#sec:dependency_cache,stores resolved dependencies in a local cache>> called the _dependency cache_.
Subsequent builds use the cache to avoid unnecessary network calls.

Metadata describes dependencies. Some examples of metadata include:

* coordinates for finding the dependency in a repository
* information about the project that created the dependency
* the authors of the dependency
* other dependencies required for a dependency to work properly, known as _transitive dependencies_

You can <<dependency_constraints.adoc#dependency-constraints,customize Gradle's handling of transitive dependencies>> based on the requirements of a project.

Projects with tens or hundreds of declared dependencies can be difficult to debug.
Gradle provides tooling to visualize and analyze a project's dependency graph.
You can use a link:https://scans.gradle.com/get-started[build scan] or built-in tasks.
For more information, see <<viewing_debugging_dependencies.adoc#viewing-debugging-dependencies,Viewing and Debugging Dependencies>>.

.Build scan dependencies report
image::gradle-core-test-build-scan-dependencies.png[]
