I wanted to copy files from src/main/resources to a build directory.
Basically, I wanted to “copy” files 3 times (because my envList has 3 files). I could do it with below code. But its bad that I’m registering the task every time I copy, so here 3 times I register copy task I’m looping copy task which is odd.
var envList = mutableListOf("foo.yaml", "bar.yaml", "sample.yaml")
envList.indices.forEach { index ->
tasks.register<Copy>("package") {
parsedFileList[index].forEach {
it.value.forEach { filePath ->
fileTree(file("src/main/resources")).visit {
if (this.path.endsWith(".json") && this.toString().contains(filePath)) {
from(layout.buildDirectory.dir("resources/main/"))
include(this.path)
into(layout.buildDirectory.dir("deployment/${envList[index]}"))
}
}
}
}
}
}
Is there any way that I can just register copy only once and copy files for all the envList?
Any Suggestions?