7

There is already an answer to the question: how to include all the dependencies in a jar file though it's for Groovy

I'm using gradle with kotlin-dsl and the code is not compatible. 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?

eskatos
  • 4,174
  • 2
  • 40
  • 42
guenhter
  • 11,255
  • 3
  • 35
  • 66

2 Answers2

8

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.

Note: Gradle does not allow changing the dependencies after they have been resolved. It means that the block above should be executed only after the dependencies { ... } are configured.

hotkey
  • 140,743
  • 39
  • 371
  • 326
2

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")
    }
}
seunggabi
  • 1,699
  • 12
  • 12