Is there a way to declare a task to list all of th...
# kotlin-dsl
k
Is there a way to declare a task to list all of the dependencies for a the
runtimeClasspath
configuration? I’m trying to find a transitive dependency that is causing a problem and generating a flat list of dependency would be a great help. With the help of Google, I can find outdated/Groovy ways of declaring a task, but none of these solutions seem to work.
This seems to work. Not sure if it is the best:
Copy code
tasks.register("getDependencies") {
    doLast {
        project.configurations.filter { it.name == "runtimeClasspath" }.map { config ->
            config.resolve()
                .map { it.name }
                .sorted()
                .forEach { println(it) }
        }
    }
}
s
I tend to just either use the built in
depencencies
(list all dependencies) or
dependencyInsight
(find where a specific dependency comes from) tasks. Both accept options for the configuration to filter down to. Another option is always a Gradle Build Scan.
k
Sure, that is what I typically use. But this hunt is particularly egregious. Hunting for a rouge dependency in a tree is difficult. šŸ˜ž
s
For that I'd definitely recommend the
dependencyInsight
task with the configuration filter then. It'll show you all of the paths from which the specified dependency comes from.
k
Yes, I was able to immediately identify the problem with a sorted list. Once I had the problematic artifact, I could use
dependencyInsight
. The first step was the problem. Once I identified the artifact (which was an internal one), I saw the problem and now can identify why the wrong version is getting pulled in using
dependencyInsight
šŸ™‚
s
Ahh gotcha. That's fair. For getting that flat, sorted list, I think what you produced is probably the most suscinct way to achieve that. It's generally what I do when I have that same need.
v
Not really succinct. Just this in Groovy:
Copy code
configurations
   .runtimeClasspath
   .files
   *.name
   .sort()
   .forEach { println(it) }
Besides that, I strongly recommend switching to Kotlin DSL. By now it is the default DSL, you immediately get type-safe build scripts, actually helpful error messages if you mess up the syntax, and amazingly better IDE support if you use a good IDE like IntelliJ IDEA or Android Studio.
šŸ‘ 1
k
The above was in Kotlin DSL. Thanks for the proper way. šŸ™‚
v
Ah, sorry, no idea why I thought it is Groovy DSL, nothing suggest it but all shouts Kotlin. šŸ¤·ā€ā™‚ļø Must have been dizzy. So in Kotlin it would be
Copy code
configurations
    .runtimeClasspath
    .get()
    .files
    .map { it.name }
    .sorted()
    .forEach { println(it) }
Or if you don't have the type-safe accessor
Copy code
configurations["runtimeClasspath"]
    .files
    .map { it.name }
    .sorted()
    .forEach { println(it) }