简体   繁体   English

gradle包含传递运行时依赖性作为编译依赖性

[英]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. 我在gradle依赖管理中表达了一个奇怪的行为,其中项目A将项目B作为编译依赖项引用,项目B将库C作为运行时依赖项引用。 Now I can use classes from library C in my project A. 现在我可以在项目A中使用库C中的类。

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: 可以使用gradle 2.9和2.10以及以下最小设置重现该问题:

// 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 如您所见,gradle :A:dependencies显示

[...]

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. 并且在驻留在项目A中的java代码中完全可以使用log4j。

See this Q&A. 看到这个问答。 If you don't specify a configuration, Gradle will choose the default configuration which extends from runtime . 如果未指定配置,Gradle将选择从runtime扩展的default配置。 A quick fix is to use 快速解决方法是使用

compile project(path: ":B", configuration: "compile")

在Android库(aar)传递运行时依赖性的情况下,这从Gradle 5.0开始修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM