Gradle 实现与 jar 任务中的编译

J.E*_*zyk 1 jar executable-jar build.gradle

我可以成功地使用Gradle编译胖 JAR,但是在最近从“编译”依赖项规范切换到“实现/api”规范后运行 JAR 时遇到问题。我已经确定问题仅发生在以下两种情况之一。应用程序在 IntelliJ 中的任一情况下运行。

第一/问题:

dependencies {implementation 'no.tornado:tornadofx:1.7.18'}
Run Code Online (Sandbox Code Playgroud)

第二/作品:

dependencies {compile'no.tornado:tornadofx:1.7.18'}
Run Code Online (Sandbox Code Playgroud)

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

C:\aaa_eric\code\testr\mic\build\libs>java -jar mic-1.0-snapshot.jar 错误:无法找到或加载主类 app.MyApp 引起: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) } }
}
Run Code Online (Sandbox Code Playgroud)

小智 6

更改configurations.compile.collectconfigurations.compileClasspath.collect固定的问题对我来说。

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

显示如何按名称引用给定配置以获取所有依赖项(例如 jars,但仅限)的示例

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'
}
Run Code Online (Sandbox Code Playgroud)

需要注意的一件事是,configurations.compileClasspath.collect即使我使用compile规范而不是implement.