0

I have a Gradle build file which uses ProtoBuffer plugin and runs some tasks. At some point some tasks are run for some files, which are inputs to tasks.

I want to modify the set of files which is the input to those tasks. Say, I want the tasks to be run with files which are listed, one per line, in a particular file. How can I do that?

EDIT: Here is a part of rather big build.gradle which provides some context.

configure(protobufProjects) {
    apply plugin: 'java'

    ext {
        protobufVersion = '3.9.1'
    }

    dependencies {
        ...
    }

    protobuf {
        generatedFilesBaseDir = "$projectDir/gen"

        protoc {
            if (project.hasProperty('protocPath')) {
                path = "$protocPath"
            }
            else {
                artifact = "com.google.protobuf:protoc:$protobufVersion"
            }
        }

        plugins {
            ...
        }

        generateProtoTasks {
            all().each { task ->
                ...
            }
        }

        sourceSets {
            main {
                java {
                    srcDirs 'gen/main/java'
                }
            }
        }
    }

    clean {
        delete protobuf.generatedFilesBaseDir
    }

    compileJava {
        File generatedSourceDir = project.file("gen")
        project.mkdir(generatedSourceDir)
        options.annotationProcessorGeneratedSourcesDirectory = generatedSourceDir
    }
}

The question is, how to modify the input file set for existing task (which already does something with them), not how to create a new task.

EDIT 2: According to How do I modify a list of files in a Gradle copy task? , it's a bad idea in general, as Gradle makes assumptions about inputs and outputs dependencies, which can be broken by this approach.

1 Answers1

0

If you would have added the gradle file and more specific that would have been very helpful. I will try to give an example from what I have understood:

fun listFiles(fileName: String): List<String> {
    val file = file(fileName).absoluteFile
    val listOfFiles = mutableListOf<String>()
    file.readLines().forEach {
        listOfFiles.add(it)
    }
    return listOfFiles
}

tasks.register("readFiles") {
    val inputFile: String by project
    val listOfFiles = listFiles(inputFile)
    listOfFiles.forEach {
        val file = file(it).absoluteFile
        file.readLines().forEach { println(it) }
    }
}

Then run the gradle like this: gradle -PinputFile=<path_to_the_file_that_contains_list_of_files> readFiles

kaushik
  • 2,308
  • 6
  • 35
  • 50
  • Thank you. Added a piece of build.gradle, hope that helps. The main question remaining is how to adjust the files set for already existing task, not registering a brand new task. – Very Belly Jan 31 '20 at 18:49