Gradle: Copying all multi-module dependencies to a local folder

November 30th, 2021

Here’s a couple of Gradle snippets that you might find useful. Both Radiance and Aurora are multi-module projects, and both have the SVG transcoder that converts SVG images into Java / Kotlin classes that contain pure canvas commands without any additional runtime dependencies on libraries such as Batik. In order to run the transcoder locally, you need to point its classpath to all the dependencies, and this is what this post is about.

To get all multi-module dependencies into a single local folder, I use a build.gradle snippet that goes along these lines:


task getFlatDependencies(type: Copy) {
    into 'build/libs'
    from {
        subprojects.findAll { it.getSubprojects().isEmpty() }.
                collect { it.configurations.compileClasspath }
    }
}

Note the usage of findAll { it.getSubprojects().isEmpty() } that filters out folders that are not modules themselves, but rather contain other modules.

Now, what about Gradle Kotlin DSL? It took me a bit to find the way to do it, as there are slight behavior changes in accessing classpaths for different configurations. Here is what I ended up with in build.gradle.kts:


tasks.register("getFlatDependencies") {
    subprojects {
        val runtimeClasspath =
            project.configurations.matching { it.name == "desktopRuntimeClasspath" }
        runtimeClasspath.all {
            for (dep in map { file: File -> file.absoluteFile }) {
                project.copy {
                    from(dep)
                    into("${rootProject.projectDir}/build/libs")
                }
            }
        }
    }
}