Lukasz G.
Lukasz G.

Reputation: 131

How to list only the runtimeClasspath resolved dependencies in Gradle during a build?

I am trying to create a task in my build that will list all the runtimeClasspath dependencies before it builds the project. So basically, it should be the equivalent of the following command but for all the subprojects:

gradle dependencies --configuration runtimeClasspath

So far I have managed to come up with a task that lists all the dependencies:

subprojects {
   task listDependencies(type: DependencyReportTask) {}

   complileJava.dependsOn listDependencies
}

It works great in the sense that it lists all the dependencies of all the subprojects, but as mentioned above, it also lists a lot of stuff I don't need to see.

How do I limit the output of the above task to just runtimeClasspath?

Thanks!

Upvotes: 2

Views: 5147

Answers (2)

Lukasz G.
Lukasz G.

Reputation: 131

A Kotlin version of the above accepted answer:

val listDeps = tasks.register<DependencyReportTask>("listDependencies") {
    setConfiguration("compileClasspath")
}

tasks.withType<JavaCompile> {
    dependsOn(listDeps)
}

Upvotes: 2

Louis Jacomet
Louis Jacomet

Reputation: 14500

Since you are defining a task of type DependencyReportTask, you can configure its configuration property, which will be the equivalent of the --configuration flag on the CLI.

So something like:

subprojects {
    def listDeps = tasks.register("listDependencies", DependencyReportTask) {
        setConfiguration("runtimeClasspath")
    }

    tasks.withType(JavaCompile).configureEach {
        dependsOn(listDeps)
    }
}

Note however that printing the runtimeClasspath before executing compileJava is a bit weird. The classpath used by the compile task will be compileClasspath.

Edited to use lazy task API as there is otherwise an ordering problem with plugin application

Upvotes: 4

Related Questions