Gradle 传递依赖不起作用?

Gradle transitive dependencies not working?

所以我在 here 上有一个项目具有以下 build.gradle:

// PLUGINS

plugins {
  id 'java' // or 'groovy' Must be explicitly applied
  id 'com.github.johnrengelman.shadow' version '1.2.2'
}

apply plugin: 'application'
apply plugin: 'java'

// REPOSITORIES & DEPENDENCIES

repositories {
    mavenCentral()
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'joda-time:joda-time:2.9'
    compile 'com.miglayout:miglayout-swing:5.0'
    compile 'com.dorkbox:SystemTray:1.9'
    compile 'com.dorkbox:SystemTray-Dorkbox-Util:1.9'
    compile 'net.java.dev.jna:jna:4.2.1'
    compile 'org.slf4j:slf4j-simple:1.7.5'
    compile 'commons-cli:commons-cli:1.3.1'
}

// SETTING THE MAIN CLASS

mainClassName = "com.github.tgharib.Program"

jar {
    manifest {
        attributes 'Main-Class': 'com.github.tgharib.Program'
    }
}

目前,遵循这些 build instructions 可以很好地编译应用程序。但是,对于 com.dorkbox:SystemTray 库,我必须包括 SystemTray 的传递依赖项:net.java.dev.jna:jnaorg.slf4j:slf4j-simple。如果我理解正确,Gradle 默认情况下会自动包含传递依赖项,但是如果我删除 compile 'net.java.dev.jna:jna:4.2.1' 行,我的程序仍然可以编译,但无法 运行 (因为 JNA 是 运行时间依赖性)。

我花了大约 3 个小时试图修复它,但我一直无法解决它。一位 SO 用户建议清除缓存,因为这为他解决了问题,但清除缓存并没有解决问题。我还了解了 gradle dependencies 命令。这是输出:

:dependencies

------------------------------------------------------------
Root project
------------------------------------------------------------

archives - Configuration for archive artifacts.
No dependencies

compile - Compile classpath for source set 'main'.
+--- joda-time:joda-time:2.9
+--- com.miglayout:miglayout-swing:5.0
|    \--- com.miglayout:miglayout-core:5.0
+--- com.dorkbox:SystemTray:1.9
|    +--- org.slf4j:slf4j-api:1.6.4 -> 1.7.5
|    \--- net.java.dev.jna:jna:4.2.1
+--- com.dorkbox:SystemTray-Dorkbox-Util:1.9
|    \--- net.java.dev.jna:jna:4.2.1
+--- org.slf4j:slf4j-simple:1.7.5
|    \--- org.slf4j:slf4j-api:1.7.5
\--- commons-cli:commons-cli:1.3.1

default - Configuration for default artifacts.
+--- joda-time:joda-time:2.9
+--- com.miglayout:miglayout-swing:5.0
|    \--- com.miglayout:miglayout-core:5.0
+--- com.dorkbox:SystemTray:1.9
|    +--- org.slf4j:slf4j-api:1.6.4 -> 1.7.5
|    \--- net.java.dev.jna:jna:4.2.1
+--- com.dorkbox:SystemTray-Dorkbox-Util:1.9
|    \--- net.java.dev.jna:jna:4.2.1
+--- org.slf4j:slf4j-simple:1.7.5
|    \--- org.slf4j:slf4j-api:1.7.5
\--- commons-cli:commons-cli:1.3.1

runtime - Runtime classpath for source set 'main'.
+--- joda-time:joda-time:2.9
+--- com.miglayout:miglayout-swing:5.0
|    \--- com.miglayout:miglayout-core:5.0
+--- com.dorkbox:SystemTray:1.9
|    +--- org.slf4j:slf4j-api:1.6.4 -> 1.7.5
|    \--- net.java.dev.jna:jna:4.2.1
+--- com.dorkbox:SystemTray-Dorkbox-Util:1.9
|    \--- net.java.dev.jna:jna:4.2.1
+--- org.slf4j:slf4j-simple:1.7.5
|    \--- org.slf4j:slf4j-api:1.7.5
\--- commons-cli:commons-cli:1.3.1

shadow
No dependencies

testCompile - Compile classpath for source set 'test'.
+--- joda-time:joda-time:2.9
+--- com.miglayout:miglayout-swing:5.0
|    \--- com.miglayout:miglayout-core:5.0
+--- com.dorkbox:SystemTray:1.9
|    +--- org.slf4j:slf4j-api:1.6.4 -> 1.7.5
|    \--- net.java.dev.jna:jna:4.2.1
+--- com.dorkbox:SystemTray-Dorkbox-Util:1.9
|    \--- net.java.dev.jna:jna:4.2.1
+--- org.slf4j:slf4j-simple:1.7.5
|    \--- org.slf4j:slf4j-api:1.7.5
\--- commons-cli:commons-cli:1.3.1

testRuntime - Runtime classpath for source set 'test'.
+--- joda-time:joda-time:2.9
+--- com.miglayout:miglayout-swing:5.0
|    \--- com.miglayout:miglayout-core:5.0
+--- com.dorkbox:SystemTray:1.9
|    +--- org.slf4j:slf4j-api:1.6.4 -> 1.7.5
|    \--- net.java.dev.jna:jna:4.2.1
+--- com.dorkbox:SystemTray-Dorkbox-Util:1.9
|    \--- net.java.dev.jna:jna:4.2.1
+--- org.slf4j:slf4j-simple:1.7.5
|    \--- org.slf4j:slf4j-api:1.7.5
\--- commons-cli:commons-cli:1.3.1

(*) - dependencies omitted (listed previously)

BUILD SUCCESSFUL

Total time: 4.544 secs

如您所见,SystemTray 确实将 JNA 作为依赖项包含在内,但如果我在主项目中删除 JNA 依赖项,它将无法 运行。

它最终成为上游 pom 文件的问题。他们将依赖关系从 compile-time 依赖关系切换为 run-time 依赖关系并修复了它。