【问题标题】:Gradle implementation vs compile in jar taskGradle 实现与在 jar 任务中编译
【发布时间】:2019-01-29 00:42:06
【问题描述】:

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

第一个/问题:

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

秒/作品:

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 错误:无法找到或加载主类 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) } }
}

【问题讨论】:

    标签: jar build.gradle executable-jar


    【解决方案1】:

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

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

    显示如何通过名称引用给定配置的示例 为了获取所有依赖项(例如罐子,但只有)

    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'
    }
    

    需要注意的一点是,即使我使用的是 compile 规范而不是 implementconfigurations.compileClasspath.collect 也对我有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-18
      • 1970-01-01
      • 1970-01-01
      • 2020-06-02
      • 1970-01-01
      • 2014-06-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多