Dependency Handler
A DependencyHandler
is used to declare dependencies. Dependencies are grouped into configurations (see org.gradle.api.artifacts.Configuration).
To declare a specific dependency for a configuration you can use the following syntax:
dependencies {
configurationName(dependencyNotation)
}
Example shows a basic way of declaring dependencies.
plugins {
id("java-library")
}
dependencies {
// Declare dependency on module components
// Coordinates are separated by a single colon -- group:name:version
implementation("org.apache.commons:commons-lang3:3.17.0")
testImplementation("org.mockito:mockito-core:5.18.0")
// Declare dependency on arbitrary files
implementation(files("hibernate.jar", "libs/spring.jar"))
// Declare dependency on all jars from the 'libs' directory
implementation(fileTree("libs"))
}
Advanced dependency configuration
To perform advanced configuration on a dependency when it is declared, you can pass an additional configuration closure:
dependencies {
configurationName(dependencyNotation){
configStatement1configStatement2
}
}
- Forcing certain dependency version in case of the conflict.
- Excluding certain dependencies by name, group or both. More details about per-dependency exclusions can be found in docs for exclude.
- Avoiding transitive dependencies for certain dependency.
plugins {
id("java-library")
}
dependencies {
implementation('org.hibernate:hibernate') {
// In case of versions conflict '3.1' version of hibernate wins:
version {
strictly('3.1')
}
// Excluding a particular transitive dependency:
exclude module: 'cglib' //by artifact name
exclude group: 'org.jmock' //by group
exclude group: 'org.unwanted', module: 'iAmBuggy' //by both name and group
// Disabling all transitive dependencies of this dependency
transitive = false
}
}
- Declaring dependency on a specific configuration of the component.
- Declaring explicit artifact requests. See also artifact.
plugins {
id("java-library")
}
dependencies {
// Configuring dependency to specific configuration of the module
// This notation should _only_ be used for Ivy dependencies
implementation("org.someOrg:someModule:1.0") {
targetConfiguration = "someConf"
}
// Configuring dependency on 'someLib' module
implementation("org.myorg:someLib:1.0") {
// Explicitly adding the dependency artifact:
// Prefer variant-aware dependency resolution
artifact {
// Useful when some artifact properties unconventional
name = "someArtifact" // Artifact name different than module name
extension = "someExt"
type = "someType"
classifier = "someClassifier"
}
}
}
Dependency notations
There are several supported dependency notations. These are described below. For each dependency declared this way, a Dependency object is created. You can use this object to query or further configure the dependency.
You can also always add instances of org.gradle.api.artifacts.Dependency directly:
configurationName(<instance>)
Dependencies can also be declared with a org.gradle.api.provider.Provider that provides any of the other supported dependency notations.
External dependencies
The following notation is used to declare a dependency on an external module:
configurationName("group:name:version:classifier@extension")
All properties, except name, are optional.
External dependencies are represented by a .
plugins {
id("java-library")
}
dependencies {
// Declare dependency on module components
// Coordinates are separated by a single colon -- group:name:version
implementation("org.apache.commons:commons-lang3:3.17.0")
testImplementation("org.mockito:mockito-core:5.18.0")
}
Project dependencies
To add a project dependency, you use the following notation:
configurationName(project(":some-project"))
Project dependencies are resolved by treating each consumable configuration in the target project as a variant and performing variant-aware attribute matching against them. However, in order to override this process, an explicit target configuration can be specified:
configurationName(project(path: ":project-a", configuration: "someOtherConfiguration"))
Project dependencies are represented using a org.gradle.api.artifacts.ProjectDependency.
File dependencies
You can also add a dependency using a org.gradle.api.file.FileCollection:
configurationName(files("a file"))
plugins {
id("java-library")
}
dependencies {
// Declare dependency on arbitrary files
implementation(files("hibernate.jar", "libs/spring.jar"))
// Declare dependency on all jars from the 'libs' directory
implementation(fileTree("libs"))
}
File dependencies are represented using a org.gradle.api.artifacts.FileCollectionDependency.
Gradle distribution specific dependencies
It is possible to depend on certain Gradle APIs or libraries that Gradle ships with. It is particularly useful for Gradle plugin development. Example:
plugins {
id("groovy")
}
dependencies {
// Declare dependency on the Groovy version that ships with Gradle
implementation(localGroovy())
// Declare dependency on the Gradle API interfaces and classes
implementation(gradleApi())
// Declare dependency on the Gradle test-kit to test build logic
testImplementation(gradleTestKit())
}
Properties
Functions
Adds a dependency to the given configuration, and configures the dependency using the given expression.
Creates a dependency without adding it to a configuration.
Creates a dependency on a module without adding it to a configuration.
Builds the dependency notation for the named Kotlin module at the embedded version (currently 2.2.0).
Kotlin extension function for org.gradle.api.artifacts.dsl.DependencyHandler.project.
Creates a dependency on a project without adding it to a configuration.
Kotlin extension function taking kotlin.reflect.KClass for org.gradle.api.artifacts.dsl.DependencyHandler.registerTransform.
Returns the extension of the specified type.
Returns the extension of the specified extensionType.