Home > Net >  Gradle task to print out files in a configuration
Gradle task to print out files in a configuration

Time:01-21

I'm new to Gradle and trying to convert a large Maven build over to Gradle. I'm trying to debug a dependency problem and I would like to print out all the files in a configuration. This is a multi module build and I have tried defining a task at both the top level or in the module I am interested in. In both cases, I see output from the task, but no files listed.

At the top level I tried something like this:

task printFilesRoot {
  dependsOn 'build'
  doLast {
    println "From root, printing artifacts"
    project(":some-module").configurations.myconfig.allArtifacts.each { art -> println "Artifact: "   file }

    println "From root, printing files"
    project(":some-module").configurations.myconfig.allArtifacts.getFiles().each { file -> println "File: "   file }
  }
}

Within the module, I tried something similar:

task printFilesModule {
  doLast {
    println "From module, printing artifacts"
    configurations.myconfig.allArtifacts.each { art -> println "Artifact: "   file }

    println "From module, printing files"
    configurations.myconfig.allArtifacts.getFiles().each { file -> println "File: "   file }
  }
}

I have tried invoking the tasks directly, or making another task (like build) dependent on one of the tasks. In every case, the only output I see is the "printing artifacts" and "printing files" statements, no actual data.

I assume I have something wrong with the way I'm trying to print out this data, but what? I'm aware that Gradle has two phases - configuration and execution. I thought that by placing my task code inside a "doFirst" or "doLast" block, that the code would be executed when the task runs, rather than during the configuration phase, but have I misunderstood something here?

CodePudding user response:

First, are you sure that you need to create a custom task?

Listing all files in a configuration

Configuration.getAllArtifacts() will only return artifacts, not dependencies. Artifacts are built by a project, and are not dependencies that your project fetches.

Since a Configuration just implements FileCollection, which in turn implements java.lang.Iterable<java.io.File>, you can just iterate over the configuration.

(Since I'm not familiar with Gradle Groovy, I've adapted an example in the Gradle docs for building a fat jar.)

// build.gradle

tasks.register("printFilesModule") {
  doLast {
    configurations.runtimeClasspath.each { ... }
  }
}

I have tried invoking the tasks directly, or making another task (like build) dependent on one of the tasks. In every case, the only output I see is the "printing artifacts" and "printing files" statements, no actual data.

Aside from using getAllArtifacts(), since you're using a custom configurations named myconfig it's possible they're just empty and don't have any dependencies? It'd be best to stick to the configurations supplied by the standard Gradle plugins (for example, the Java Plugin creates implementation, compileOnly, runtimeOnly, and a few more.

I thought that by placing my task code inside a "doFirst" or "doLast" block, that the code would be executed when the task runs, rather than during the configuration phase, but have I misunderstood something here?

Nope, you have correctly defined tasks, there's no problem there.

  • Related