Gradle - Dependency

1 - About

dependency management in Gradle of module

3 - Type

3.1 - Module

  • Module dependency - A module dependency represents a dependency on a module with a specific version built outside the current build.

3.2 - Project

see project dependency

dependencies {
    implementation project(':shared')

4 - Scope

Gradle represents the scope of a dependency with the help of a Configuration.

5 - Syntax


dependencies {
    configurationName dependencyNotation1, dependencyNotation2, ...


  • A Configuration represents a group of artifacts and their dependencies.

6 - Example

apply plugin: 'java'
//so that we can use 'compile', 'testCompile' for dependencies
apply plugin: 'java-library'
//so that we can use 'api', 'implementation' as dependencies configuration for library

dependencies {
  //for dependencies found in artifact repositories you can use
  //the group:name:version notation
  compile 'commons-lang:commons-lang:2.6'
  testCompile 'org.mockito:mockito:1.9.0-rc1'

  //map-style notation:
  compile group: '', name: 'guice', version: '1.0'

  //declaring arbitrary files as dependencies
  compile files('hibernate.jar', 'libs/spring.jar')

  //putting all jars from 'libs' onto compile classpath
  compile fileTree('libs')
  compile 'com.jcraft:jsch:0.1.51'
  testCompile 'junit:junit:4.12'
  api 'org.apache.httpcomponents:httpclient:4.5.7'
  api 'org.apache.commons:commons-math3:3.6.1' 
  implementation 'org.apache.commons:commons-lang3:3.5'
  implementation '' 
  testImplementation 'junit:junit:4.12'
  testImplementation 'junit:junit:4.+' //  junit >= 4.0 is required to compile the project’s tests
  annotationProcessor ''


7 - Configuration

  • An api dependency is public (seen by other modules)
  • An implementation dependency is private (only seen by this module).
Plugin Dependency configuration Extends Used by tasks Meaning
Java Plugin compile (deprecated use api instead) - - Compile time dependencies
Java Plugin compileOnly compile - Compile time only dependencies, not used at runtime (same as provided for Maven)
Java Plugin compileClasspath compileOnly compileJava Compile classpath, used when compiling source
Java Plugin runtime compile - Runtime dependencies
Java Plugin testCompile compile - Additional dependencies for compiling tests
Java Plugin testCompileOnly testCompile - Additional dependencies only for compiling tests, not used at runtime
Java Plugin testCompileClasspath testCompileOnly compileTestJava Test compile classpath, used when compiling test sources
Java Plugin testRuntime runtime, testCompile test Additional dependencies for running tests only
Java Plugin archives - uploadArchives Artifacts (e.g. jars) produced by this project
Java Plugin default runtime - The default configuration used by a project dependency on this project. Contains the artifacts and dependencies required by this project at runtime.
java library plugin api compile These dependencies will appear in the compile classpath of customer ( application or library) using this library
java library plugin implementation compile These dependencies will not appear into the consumers' compile classpath.
Java Plugin annotationProcessor compileOnly Only used to generate class at compile time See (doc)

7.1 - compileOnly

compileOnly = maven provided

  • Dependencies required at compile time but never required at runtime, such as source-only annotations or annotation processors;
  • Dependencies required at compile time but required at runtime only when using certain features, a.k.a. optional dependencies;
  • Dependencies whose API is required at compile time but whose implementation is to be provided by a consuming library, application or runtime environment.

They are not included on the runtime classpath and they are non-transitive, meaning they are not included in dependent projects

compile-only dependencies are not inherited by the test classpath. The intention is that tests, like any other runtime environment, should provide their own implementation, either in the form of mocks or some other dependency.

8 - Management

8.1 - Tree

8.1.1 - Show

gradle dependencies

Identifying which dependency version was selected and why

dependencyInsight: Given a dependency in the dependency graph you can identify the origin of the dependency.

  • the mandatory parameter –dependency

gradle dependencyInsight --dependency slf4j

8.2 - Cache

The first time you run the build, Gradle will check whether or not you already have the listed dependencies in your cache under your ~/.gradle directory. If not, the libraries will be downloaded and stored there.

8.3 - No rebuild

  • Do not rebuild project dependencies.

gradle [-a|--no-rebuild]

8.4 - Refresh state

Refresh the state of dependencies.

gradle --refresh-dependencies    

8.5 - Task Dependency

8.6 - Download

Download should be done automatically. If it does not work, you can get details with the following command:

gradle dependencyInsight --dependency name

8.7 - Exclude

    exclude(group = "org.yaml")

9 - Documentation

Data Science
Data Analysis
Data Science
Linear Algebra Mathematics

Powered by ComboStrap