【问题标题】:Using Gradle to build a jar with dependencies with Kotlin-DSL使用 Gradle 构建带有 Kotlin-DSL 依赖项的 jar
【发布时间】:2017-09-11 13:50:01
【问题描述】:

这个问题已经有了答案:how to include all the dependencies in a jar file 虽然它是针对 Groovy 的

我正在使用带有kotlin-dsl 的 gradle 并且代码不兼容。我尝试使用几种方法使其工作,包括:

tasks.withType<Jar> {
    configurations["compileClasspath"].forEach { file: File ->
        copy {
            from(zipTree(file.absoluteFile))
        }
    }
}

虽然这行不通。那么如何在gradle中包含使用kotlin-dsl的依赖呢?

【问题讨论】:

    标签: gradle kotlin gradle-kotlin-dsl


    【解决方案1】:

    这将起作用:

    tasks.withType<Jar>() {
        configurations["compileClasspath"].forEach { file: File ->
            from(zipTree(file.absoluteFile))
        }
    }
    

    copy { ... } 中不需要,您应该在 JAR 任务本身上调用 from

    注意:Gradle 不允许在解析后更改依赖项。意思是上面的块只有在配置了dependencies { ... }之后才能执行。

    【讨论】:

    • 我总是收到错误:Cannot change dependencies of configuration ':compile' after it has been included in dependency resolution.
    • 您是否在代码示例(或改进的示例)之后添加依赖项。一旦你读取了配置中的依赖关系,它就会被解析并且你不能添加依赖关系。
    • @lu.koerfer 这就是问题所在。在这个之后我有dependency 块。谢谢。
    • @hotkey:您能否更新您的解决方案并指出,依赖项块必须在tasks.withType&lt;Jar&gt;... 之前。我想这可能对其他人有用。
    • @guenhter 完成。
    猜你喜欢
    • 1970-01-01
    • 2019-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-08
    • 2015-06-06
    • 1970-01-01
    • 2018-01-20
    相关资源
    最近更新 更多