Reputation: 4044
I want to add few tasks depends on directory listing. I am using this code to achieve it:
gradle.projectsEvaluated({
def packagesDir = "${project.projectDir}/src/androidTest/java/com/site/myapp/mypackage"
def dir = file(packagesDir)
for (file in dir.listFiles()) {
tasks.register(file.name) {
group = 'report'
description = "Collect info from " + file.name
doLast {
collectInfo(file.name)
}
}
}
})
This code successfully creates all needed tasks. But all of this tasks work as last. For example, directory contains:
dir1
dir2
dir3
dir4
dir5
And when I launch dir3
task or dir4
(or any other) it works the same as dir5
task (as if I launch collectInfo(dir5)
).
How to add several gradle tasks from a code correctly?
Upvotes: 1
Views: 43
Reputation: 20699
In my project (with Gradle 6.x) to create a series of gradle tasks per directory, I'm using smth like this:
List<File> frontEnds = file 'src/main/frontends' listFiles filter( { it.directory } as FileFilter )
frontEnds.each{ File f ->
task "npmBuild-$f.name"( type:YarnTask ) {
group = 'node'
description = "Builds production version of $f.name frontend"
workingDir = f
args = [ 'build' ]
}
}
Upvotes: 1