I am expriencing a strange behavior in gradle dependency management, where project A references project B as compile dependency and project B references library C as runtime dependency. Now I can use classes from library C in my project A.
My question: (Why) is this a bug or a feature?
The problem can be reproduced with gradle 2.9 and 2.10 and the following minimal setup:
// settings.gradle
include ':A', ':B'
// build.gradle
allprojects {
apply plugin: 'java'
apply plugin: 'maven'
repositories {
mavenLocal()
mavenCentral()
}
}
project(':A') {
dependencies {
compile project(':B')
}
}
project(':B') {
dependencies {
runtime "org.slf4j:slf4j-log4j12:1.7.13"
}
}
As you can see, a gradle :A:dependencies
shows
[...]
compile - Compile classpath for source set 'main'.
\--- project :B
\--- org.slf4j:slf4j-log4j12:1.7.13
+--- org.slf4j:slf4j-api:1.7.13
\--- log4j:log4j:1.2.17
[...]
and using log4j is totally possible in java code residing in project A.
compile dependencies 1 Ways to declare dependencies. When declaring Java dependencies in Gradle you provide a dependency configuration to which to assign your dependency. ... 2 Writing Java libraries has an extra twist. ... 3 Quick summary on implementation and compile dependencies. ...
This dependency is called transitive dependency. Gradle script downloads the JAR from maven central or any other location which we specify. Sometimes we come in the situation to exclude transitive dependency. The scenario can be like 1. JAR version of transitive dependency is not available
It is the preferred method to express constraints that should be applied to all dependencies of a configuration. When Gradle attempts to resolve a dependency to a module version, all dependency declarations with version, all transitive dependencies and all dependency constraints for that module are taken into consideration.
Let’s find out with the two simple rules described in this article. Quick answer: use the implementation configuration and never compile, BUT read on for some important caveats When declaring Java dependencies in Gradle you provide a dependency configuration to which to assign your dependency. e.g.
See this Q&A. If you don't specify a configuration, Gradle will choose the default
configuration which extends from runtime
. A quick fix is to use
compile project(path: ":B", configuration: "compile")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With