javamavengradledependency-managementtransitive-dependency

Gradle transitive dependencies not working?


So I have a project over here that has the following 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'
    }
}

Currently, following these build instructions compiles the application just fine. However, for the com.dorkbox:SystemTray library, I have to include SystemTray's transitive dependencies: net.java.dev.jna:jna and org.slf4j:slf4j-simple. If I understand correctly, Gradle automatically includes transitive dependencies by default but if I remove the compile 'net.java.dev.jna:jna:4.2.1' line for example, my program still compiles but it fails to run (because JNA is a runtime dependency).

I've spent about 3 hours trying to fix it but I haven't been able to solve it. One SO user suggested to clear the cache as that fixed the issue for him but clearing the cache didn't fix the issue. I also learned about the gradle dependencies command. Here's the output:

: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

So as you can see, SystemTray does indeed include JNA as a dependency but if I remove the JNA dependency in my main project, it fails to run.


Solution

  • It ended up being an issue with the pom file upstream. They switched the dependency from compile-time dependency to run-time dependency and that fixed it.

    enter image description here

    https://docs.gradle.org/current/userguide/java_library_plugin.html#sec:java_library_separation