Gather outputs of each top-level subproject

I’m trying to gather files to copy them into dist directory. I think i’m supposed to list each subprojects’s output dir, list its contents, check each filename against regex to select all *.war and *.jar files, merge them into a list and feed to a copy task. Alas, I can’t even get to work first section of this algorithm.

task copyOutputs(dependsOn: build) {
    	subprojects.each { s ->
        	file("${s.buildDir}/libs").each { f -> println f }

Expected behavior: list of files

Actual result:

F:\cj>gradle dist

FAILURE: Build failed with an exception.

  • Where:
    Build file ‘F:\cj\build.gradle’ line: 16

  • What went wrong:
    A problem occurred evaluating root project ‘crown_jewels’

F:\cj\emerald\build\libs (??? ? ???)

What these question marks mean?

You do not want file("${s.buildDir}/libs"), what you want is fileTree(${s.buildDir}/libs")

What you did is open the directory as if it was a file and read it printing each byte…

I’ve got feedback somewhere else that this whole approach is invalid. Each task in the build script is supposed to gather and process outputs of tasks it’s dependent on, not to list directories and collect files using regex pattern. Alas, I haven’t got any example on how exactly it could be done.

I think you’re saying you want to recursively visit all task dependencies of a Copy and use any jars or wars that you find as the source. If so, it’s a bit ugly, but I think this will do what you want:

ext.recursiveTaskDependencies = { Task task ->
    task.taskDependencies.getDependencies(task).collect { recursiveTaskDependencies(it) + it.outputs }

task copyOutputs(type: Copy, dependsOn: build) {
    from recursiveTaskDependencies(it)
    into "${buildDir}/dist"
    include '*.jar', '*.war'