I am wondering if it is possible to dynamically create a source set with gradle. The directory hierarchy of my current project looks as follows:
- dbfit-junit
- module
- foo
- bar
- module
Each of this module folders (foo and bar) should have its own source set assigned. The reason is that I want to dynamically create tasks like dbFitTestFoo and dbFitTestBar. My current approach looks like this:
ext.dbFitModulesDir = "dbfit-junit/module"
ext.dbFitTestSpecs = ["java", "groovy", "scala", "resources"]
ext.dbFitModules = []
file(dbFitModulesDir).eachDir{ module ->
dbFitModules << module.name
}
/** this needs to be done dynamically for each "module" **/
sourceSets {
integrationTest { sourceSet ->
dbFitModules.each{ module ->
dbFitTestSpecs.each { spec ->
if (!sourceSet.hasProperty(spec)) {
return
}
sourceSet."$spec".srcDir file("$dbFitModulesDir/$module/$spec")
}
}
}
}
dbFitModules.each{ module ->
task "dbFitTest${module.capitalize()}"(type: Test) {
group = "Verification"
description = "Run dbFit tests for $module"
doLast {
testClassesDir = sourceSets.integrationTest.output.classesDir
classpath = sourceSets.integrationTest.runtimeClasspath
}
}
}
The creation of the tasks works smoothly, the only thing that is still missing is the dynamic creation and assignment of the sourcesets.
Thanks for any hints!
Yes, you can create source sets dynamically. Here's one example: