Module dependencies

Module dependencies are the most common dependencies. They refer to a module in a repository.

Example 1. Module dependencies
build.gradle
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
    }
}
build.gradle.kts
dependencies {
    runtime(group = "org.springframework", name = "spring-core", version = "2.5")
    runtime("org.springframework:spring-aop:2.5")
    runtime("org.hibernate:hibernate:3.0.5") {
        isTransitive = true
    }
    runtime(group = "org.hibernate", name = "hibernate", version = "3.0.5") {
        isTransitive = 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 (.module, .pom or ivy.xml) in the repositories. If such a module metadata file exists, it is parsed and the artifacts of this module (e.g. hibernate-3.0.5.jar) as well as its dependencies (e.g. cglib) are downloaded. If no such module metadata file exists, Gradle may look, depending on the metadata sources definitions, for an artifact file called hibernate-3.0.5.jar directly. In Maven, a module can have one and only one artifact. In Gradle and Ivy, a module can have multiple artifacts. Each artifact can have a different set of dependencies.

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:

Example 2. File dependencies
build.gradle
dependencies {
    runtime files('libs/a.jar', 'libs/b.jar')
    runtime fileTree('libs') { include '*.jar' }
}
build.gradle.kts
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.

Example 3. Generated file dependencies
build.gradle
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 }}"
    }
}
build.gradle.kts
dependencies {
    compile(files("$buildDir/classes") {
        builtBy("compile")
    })
}

task("compile") {
    doLast {
        println("compiling classes")
    }
}

task("list") {
    dependsOn(configurations["compile"])
    doLast {
        println("classpath = ${configurations["compile"].map { 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.

Example 4. Project dependencies
build.gradle
dependencies {
    implementation project(':shared')
}
build.gradle.kts
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.

Example 5. Gradle API dependencies
build.gradle
dependencies {
    implementation gradleApi()
}
build.gradle.kts
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.

Example 6. Gradle TestKit dependencies
build.gradle
dependencies {
    testImplementation gradleTestKit()
}
build.gradle.kts
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.

Example 7. Gradle’s Groovy dependencies
build.gradle
dependencies {
    implementation localGroovy()
}
build.gradle.kts
dependencies {
    implementation(localGroovy())
}