Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gradle includes transitive runtime dependency as compile dependency

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.

like image 432
Michael Schaefers Avatar asked Jan 28 '16 16:01

Michael Schaefers


People also ask

How to compile dependencies in Gradle?

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. ...

What is transitive dependency in Gradle?

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

What is the use of it method in Gradle?

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.

Should I use the implementation configuration or implementation configuration in Gradle?

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.


1 Answers

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")
like image 58
lance-java Avatar answered Oct 22 '22 04:10

lance-java