FileTree and lazy task evaluation

Hi guys,
Please help me with an issue. I have the following code snippet:

class ProcessDescriptors extends DefaultTask {

    FileCollection descriptorsFiles

    DirectoryProperty outputDir

    ProcessDescriptors() {
        outputDir = getProject().objects.directoryProperty() 
        descriptorsFiles = getProject().objects.fileCollection()     

    void exec()
        def tmpFile = new File(temporaryDir, 'kitslist.txt')

        tmpFile.withWriter { writer ->
            descriptorsFiles.each { line ->
                writer.println line

        project.javaexec {
            main = 'com.util.DescriptorTranslator'
            classpath = project.configurations.someConfiguration
            args = ["@$tmpFile", "-d", outputDir.get()]

//used like this
task compileKitsDescriptors(type: ProcessDescriptors) {
    outputDir = file("${workspaceRoot}/compile/");
    descriptorsFiles = project.fileTree(dir: projectDir,
            includes: ["**/source/**/descr/**/*.xml", "DBA/main/resources/**/descr/**/*.xml"],
            excludes: ["SOMEDIR/**", "**/source/**/myxml.xml", "**/source/**/mydir/**"])

The issue with this the task is that when I pass a fileTree to descriptorsFiles property it scans the base directory for files twice: during up-to-date check and when used. So the question: Is there is any way to store its state once evaluated? Using Gradle 5.6.2

You could do

    Set<File> descriptorsFiles

    public void setDescriptorsFiles(FileCollection descriptorsFiles) {
        this.descriptorsFiles = descriptorsFiles.getFiles();

Thanks for you reply, Lance.

I tried something similar, but it means that the files will be scanned during configuration phase, what I would like to avoid.

Have you considered declaring a more efficient FileTree that doesn’t iterate all of your sources?


def tree1 = fileTree("src/main/resources/xml") {
   includes "*.xml"
def tree2 = fileTree("DBA/main/resources") {
   includes "**/descr/**/*.xml" 
descriptorsFiles = 

Thank you Lance.

Indeed using this approach of collecting fileTrees it become much faster, and the issue with long scanning of dirs has gone.