所以我在这里有一个项目,它有以下 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'
}
}
目前,遵循这些构建说明可以很好地编译应用程序。但是,对于com.dorkbox:SystemTray
库,我必须包含 SystemTray 的传递依赖项:net.java.dev.jna:jna
和org.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 依赖项,它将无法运行。