Gradle - Dependency

Gradle - Dependency

About

dependency management in Gradle of module

Type

Type

Module

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

Project

see project dependency

dependencies {
    implementation project(':shared')
}

Scope / Path

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

Syntax

block

dependencies {
    configurationName dependencyNotation1, dependencyNotation2, ...
}

where:

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

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: 'com.google.code.guice', 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 'com.google.guava:guava:27.0.1-jre' 
  
  testImplementation 'junit:junit:4.12'
  testImplementation 'junit:junit:4.+' //  junit >= 4.0 is required to compile the project’s tests
  
  annotationProcessor 'com.google.dagger:dagger-compiler:2.8'

}

Configuration

https://docs.gradle.org/current/userguide/declaring_dependencies.html#sec:what-are-dependency-configurations

  • 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)

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.

Management

Tree

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

Runtime, Compile Paths …

The dependencies task gives you dependencies information such as runtime path, compile path and tree.

../gradlew dependencies

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.

No rebuild

  • Do not rebuild project dependencies.
gradle [-a|--no-rebuild]

Refresh state

Refresh the state of dependencies.

gradle --refresh-dependencies    

Task Dependency

task dependency

gradle --dry-run :project:task

Download

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

gradle dependencyInsight --dependency name

Exclude

implementation("com.github.javafaker:javafaker:1.0.2"){
    exclude(group = "org.yaml")
}

Documentation





Discover More
Gradle - Configuration

scope of a dependency The id is its name.
Gradle - Module

in Gradle Modules: are usually stored in a repository may have dependency
Gradle - Multi-Project

A page multi-project setup in gradle. There is only one settings.gradle file at the root There is one top-level build script (build.gradle) (also known as the root project) that configure common...
Gradle - Test

add a testImplementation or testCompile dependency in the module where the test are to create a test jar in kotlin dsl in the module that depends on the test class Single project:...



Share this page:
Follow us:
Task Runner