简体   繁体   English

使用 Gradle 构建 jar 与 Kotlin-DSL 的依赖关系

[英]Using Gradle to build a jar with dependencies with Kotlin-DSL

There is already an answer to the question: how to include all the dependencies in a jar file though it's for Groovy这个问题已经有了答案:如何在 jar 文件中包含所有依赖项,尽管它适用于 Groovy

I'm using gradle with kotlin-dsl and the code is not compatible.我将 gradle 与kotlin-dsl一起使用,并且代码不兼容。 I tried to make it work using a few ways including:我尝试使用几种方法使其工作,包括:

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

Though this doesn't work.虽然这不起作用。 So how to include the dependencies using kotlin-dsl in gradle?那么如何在 gradle 中包含使用kotlin-dsl的依赖项?

This will work: 这将有效:

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

There's no need in copy { ... } , you should call from on the JAR task itself. copy { ... }没有必要,你应该from JAR任务本身调用。

Note: Gradle does not allow changing the dependencies after they have been resolved. 注意:Gradle在解析后不允许更改依赖项。 It means that the block above should be executed only after the dependencies { ... } are configured. 这意味着只有在配置了dependencies { ... }之后才能执行上面的块。

my case我的情况

withType<Jar> {
    enabled = true
    isZip64 = true
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE

    archiveFileName.set("$project.jar")

    from(sourceSets.main.get().output)
    dependsOn(configurations.compileClasspath)
    from({
        configurations.compileClasspath.get().filter {
            it.name.endsWith("jar")
        }.map { zipTree(it) }
    }) {
        exclude("META-INF/*.RSA", "META-INF/*.SF", "META-INF/*.DSA")
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM