dependency management in Gradle of module
dependencies {
implementation project(':shared')
}
Gradle represents the scope of a dependency with the help of a Configuration.
dependencies {
configurationName dependencyNotation1, dependencyNotation2, ...
}
where:
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'
}
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 = maven provided
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.
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.
gradle dependencyInsight --dependency slf4j
The dependencies task gives you dependencies information such as runtime path, compile path and tree.
../gradlew dependencies
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.
gradle [-a|--no-rebuild]
Refresh the state of dependencies.
gradle --refresh-dependencies
task dependency
gradle --dry-run :project:task
Download should be done automatically. If it does not work, you can get details with the following command:
gradle dependencyInsight --dependency name
implementation("com.github.javafaker:javafaker:1.0.2"){
exclude(group = "org.yaml")
}