How to add dependencies

Dependencies in Gradle follow the same format as Maven.

group:name:version
val vlwjglVersion = "3.2.2"
val lwjglNatives = "natives-macos"
// ...
    
dependencies {        
    implementation ("org.hibernate:hibernate-core:3.6.7.Final")    
    runtimeOnly ("org.lwjgl:lwjgl:$lwjglVersion:$lwjglNatives")
    //...
}
project.ext.lwjglVersion = "3.2.2"
project.ext.lwjglNatives = "natives-macos"
// ...
    
dependencies {        
    implementation 'org.hibernate:hibernate-core:3.6.7.Final'    
    runtimeOnly "org.lwjgl:lwjgl:$lwjglVersion:$lwjglNatives"
    //...
}

repositories

Gradle uses repositories to download the remote library. So make sure the repository is listed in your build.gradle file.

repositories {
    // Maven Central contains open source libraries for use in Java projects.
    mavenCentral()    
    
    // with url (Spring boot snapshot repository)
    maven("https://repo.spring.io/libs-snapshot")
    
     // Bintray's JCenter 
    jcenter()
    
    // Android-specific artifacts including the Android SDK. 
    google()  
}

You can specify default repositories in the top-level build.gradle file.

allprojects {
 // ...
    repositories {
        mavenCentral()
        jcenter()
        google()
    }
}

Repositories can be specified in settings.gradle file. In this case you can not add repositories in the build scripts as above, otherwise you will get an error:

Build was configured to prefer settings repositories over project repositories but repository 'xxx' was added.

// settings.gradle
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        jcenter() // Warning: this repository is going to shut down soon
        maven {url "https://jitpack.io"}
    }
}

configuration

There are two classpaths when you work with java project:

  1. compile classpath - defines paths of dependencies that are required at compile time
  2. runtime classpath - defines paths of dependencies that are required at runtime

Gradle allows to add dependencies for specified classpath type:

  • compileOnly - add dependency for the compile classpath only
  • runtimeOnly - add dependency for the runtime classpath only
  • implementation - add dependency for both classpaths

Similarly Gradle works with the test dependencies:

  • testCompileOnly
  • testRuntimeOnly
  • testImplementation

Some Gradle plugins can change meaning or add other configurations. For example "java-library" plugin:

  • api - dependencies declared in the consumed library will appear on the compile and runtime classpaths of the consumer library
  • implementation - dependencies in the consumed library will appear on only the runtime classpath of the consumer library

excluding

You can exclude module from transitive dependency for replacing by other version of module.

dependencies {
    val RETROFIT_VERSION = "2.0.2"
    val OKHTTP_VERSION = "3.2.0"
    // ...

    // excludes OkHttp module from Retrofit library
    implementation("com.squareup.retrofit2:retrofit:$RETROFIT_VERSION") {
        exclude (module = "okhttp")
    }
    // because we'll add another version of OkHttp ourselves
    implementation ("com.squareup.okhttp3:okhttp:$OKHTTP_VERSION")
}
dependencies {
    final RETROFIT_VERSION = '2.0.2'
    final OKHTTP_VERSION = '3.2.0'
    // ...
    
    // excludes OkHttp module from Retrofit library
    implementation("com.squareup.retrofit2:retrofit:$RETROFIT_VERSION") {  
        exclude module: 'okhttp'
    }
    // because we'll add another version of OkHttp ourselves
    implementation "com.squareup.okhttp3:okhttp:$OKHTTP_VERSION"
}

test dependency

dependencies {
    // ...
    
    // default for kotlin
    // testImplementation(kotlin("test-junit"))
    
    // add Junit 5
    testImplementation("org.junit.jupiter:junit-jupiter:5.7.0")
}

tasks.withType<Test> {
    useJUnitPlatform()
}

If you have "events were not received" on your tests in IntelliJ IDEA CE, close and open IDE.

commands

# dependencies of the root project
gradle dependencies

# dependencies of the given subproject
gradle subproject_name:dependencies

# with configuration
gradle dependencies --configuration compile