Gradle 实现与在 jar 任务中编译

Gradle implementation vs compile in jar task

我可以成功地使用 Gradle 来编译一个胖 JAR,但是在最近从 "compile" 依赖规范到 "implementation/api" 规范。我已经确定问题仅出现在以下两种情况之一中。在 IntelliJ 中的任何一种情况下,应用程序 运行s。

first/problem:

dependencies {implementation 'no.tornado:tornadofx:1.7.18'}

second/works:

dependencies {compile'no.tornado:tornadofx:1.7.18'}

JAR 在这两种情况下都可以编译。当我尝试在命令行上启动第一个案例 JAR 时出现问题,它抛出以下错误。

C:\aaa_eric\code\testr\mic\build\libs>java -jar mic-1.0-snapshot.jar Error: Could not find or load main class app.MyApp Caused by: java.lang.NoClassDefFoundError: tornadofx/App

这是 build.gradle 中的 JAR 任务。是否有可能 tornadofx 依赖项在编译时可用,但在 运行 时不可用?感谢您的帮助。

jar {
  manifest {
    attributes 'Main-Class': 'app.MyApp'
  }
  from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}

configurations.compile.collect 更改为 configurations.compileClasspath.collect 解决了我的问题。

我遇到了同样的问题并在 https://docs.gradle.org/current/javadoc/org/gradle/api/artifacts/ConfigurationContainer.html 中偶然发现了这个问题:

An example showing how to refer to a given configuration by name in order to get hold of all dependencies (e.g. jars, but only)

apply plugin: 'java' //so that I can use 'implementation', 'compileClasspath' configuration

dependencies {
    implementation 'org.slf4j:slf4j-api:1.7.26'
}

//copying all dependencies attached to 'compileClasspath' into a specific folder
task copyAllDependencies(type: Copy) {
    //referring to the 'compileClasspath' configuration
    from configurations.compileClasspath
    into 'allLibs'
}

需要注意的一件事是 configurations.compileClasspath.collect 即使在我使用 compile 规范而不是 implement 时也对我有用。