Dependencies

Before we start adding dependencies, we need to say what plugis are going to be used. For Java project, we first have to apply Java plugin and then we can provide dependencies.

apply plugin: 'java'
repositories {
    jcenter()
}
dependencies {
    compile 'org.slf4j:slf4j-api:1.7.25'
}

Dynamic versions

We can specify version using +. That will make sure that always highest version of that version level is used. Suppose we use this semantic versioning.

dependencies {
    compile('org.springframework:spring-core:5.0.+'
}

Work with dependencies

We can takes all jars for given configuration and copies it into a specific folder. The following task is going to take all compile dependencies and copy it into to copied folder.

task copyDependenciesToLocalDir(type: Copy) {
    from configurations.getByName('compile').asFileTree
    into "copied"
}

We can then reuse all jar files in a folder and add them into a scope.

Dependencies

Add dependency without its transitive dependencies.

Dominate a certain version, including transitive dependencies, over others.

Exclude a certain transitive dependency.

Add jar into compile configuration.

Add all jars from a folder into compile configuration.

Project dependencies

We can have dependencies on other sub-modules or aka projects.

Repositories

We can obtain dependencies from multiple repository types.

Offline

Gradle tries to connect to repositories to check if the dependencies are up to date. We can turn all interactions with network using --offline flag.

Browse dependencies

We can create our own task to print all dependencies for a specific scope.

Other way to check dependencies is to use dependencies and dependencyInsight tasks that are provided by Gradle.

Dependency resolution

When using many libraries, there can be version conflicts of some libraries. We can force certain version of a library for our project.

If we want to be strict and we want to avoid version conflicts, we might want to say Gradle to fail build if there is a version conflict.

Refreshing dependencies

When working with SNAPSHOTs, we might want to force Gradle to reload dependencies.

We can refresh dependencies using --refresh-dependencies flag.

Or we can change caching times.

Creating own configuration

We might need to create our own configuration (a bucket) to place our files (dependencies).This example shows how compile configuration can be created and used.

For example Java plugin provides six configurations: archives, default, compile, runtime, testCompile testRuntime.

Configurations can extend from other configurations.

Last updated

Was this helpful?