Dependency Types
Module dependencies
Module dependencies are the most common dependencies. They refer to a module in a repository.
Groovy
Kotlin
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 ExternalModuleDependency 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 metadata file ( |
File dependencies
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:
Groovy
Kotlin
dependencies {
runtime files('libs/a.jar', 'libs/b.jar')
runtime fileTree('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.
Groovy
Kotlin
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 }}"
}
}
$ gradle -q list compiling classes classpath = [classes]
Project dependencies
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.
Groovy
Kotlin
dependencies {
implementation project(':shared')
}
For more information see the API documentation for ProjectDependency.
Multi-project builds are discussed in this chapter.
Gradle distribution-specific dependencies
Gradle API dependency
You can declare a dependency on the API of the current version of Gradle by using the DependencyHandler.gradleApi() method. This is useful when you are developing custom Gradle tasks or plugins.
Groovy
Kotlin
dependencies {
implementation gradleApi()
}
Gradle TestKit dependency
You can declare a dependency on the TestKit API of the current version of Gradle by using the DependencyHandler.gradleTestKit() method. This is useful for writing and executing functional tests for Gradle plugins and build scripts.
Groovy
Kotlin
dependencies {
testImplementation gradleTestKit()
}
The TestKit chapter explains the use of TestKit by example.
Local Groovy dependency
You can declare a dependency on the Groovy that is distributed with Gradle by using the DependencyHandler.localGroovy() method. This is useful when you are developing custom Gradle tasks or plugins in Groovy.
Groovy
Kotlin
dependencies {
implementation localGroovy()
}