https://kotlinlang.org logo
Title
m

Marc Reichelt

01/28/2019, 12:44 PM
Mainly, the
Configuration
class does not have an
isDirectory
property - but how could the Groovy code then work in the first place?
d

Dominaezzz

01/28/2019, 1:45 PM
You might need to do some casting.
m

Marc Reichelt

01/28/2019, 1:47 PM
how should I cast
Configuration
to a
File
?
d

Dominaezzz

01/28/2019, 1:50 PM
I'm not sure what type it really is. You could print it out with
println()
to check and then cast to the expected type with
as
.
m

Marc Reichelt

01/28/2019, 1:55 PM
I took the hard road and I think I solved it, although it involves mutable state for now it seems to work:
from(configurations.compile.map { configuration ->
            var finalFileTree: FileCollection = files().asFileTree
            configuration.asFileTree.forEach {
                if (it.isDirectory.not()) {
                    finalFileTree = finalFileTree.plus(zipTree(it))
                }
            }
            finalFileTree
        })
And with this I also found a more functional way to do it - although I assume there has to be an easier way:
from(configurations.compile.map { configuration ->
            configuration.asFileTree
                .fold(files().asFileTree) { collection, file ->
                    if (file.isDirectory) collection else collection.plus(zipTree(file))
                }
        })
On GitHub @StefMa wrote that there is an official way to do it - and it’s even part of the official Gradle docs (see https://docs.gradle.org/current/userguide/working_with_files.html#sec:creating_uber_jar_example):
dependsOn(configurations.runtimeClasspath)
        from({
            configurations.runtimeClasspath.get().filter { it.name.endsWith("jar") }.map { zipTree(it) }
        })