We have a master build script for 60+ components. The individual components do not have build.gradle files. What I’m trying to do is programmatically (in the master build.gradle) add a resource folder to certain projects. This resource folder contains a file which must be in the classpath when unit tests are ran. I’m trying to add this in the allprojects block like this:
allprojects { proj ->
...
// this is the folder I need in the test task classpath
def resdir = sprintf("%s\\resources", project(':Common').projectDir)
test {
java {
srcDir 'test'
}
resources {
srcDirs = [resdir]
}
}
}
subprojects{ proj->
if(proj.name == "APROJECT"){
proj.tasks['test'].getClasspath().each {
logger.info "COMPILE CLASSPATH: {}", it
}
}
}
However, if I query the classpath of the test task (see above) I do not see the folder in the classpath. Additionally, of course, the test is failing because the folder is not in the classpath.
If I put the sourceSet update in a build.gradle in the component folder, it works as expected.
Am I doing something wrong? How can I get this folder into the classpath for testing?
I think best approach would be to use the java project layout instead of configurating anything. Put the required files into src/test/resources and tests should find them.
But directly to your problem:
I think you must puit your project layout configuration into sourceSets:
allprojects { proj ->
...
// this is the folder I need in the test task classpath
def resdir = sprintf("%s\\resources", project(':Common').projectDir)
sourceSets {
test {
java {
srcDir 'test'
}
resources {
srcDirs = [resdir]
}
}
}
subprojects{ proj->
if(proj.name == "APROJECT"){
proj.tasks['test'].getClasspath().each {
logger.info "COMPILE CLASSPATH: {}", it
}
}
}
}
And I am not sure about configuring it in allprojects.
Is your rootproject a javaproject too?
Great, thanks for the suggestion. As it turns out, that’s exactly what I did, I just didn’t include the full context in my code snippet. Full context is:
Unfortunately, the dev team has already built a kingdom around using resources from one particular global “testing component” so I can’t resolve this by putting the resources in the conventional folder. I did change from allprojects to subprojects as you suggested but the resource path is still not showing up.
I haven’t been able to get this to work by dynamically updating the source set. However, I was able to get it to work by adding the resource path to the testCompile dependency. Thanks, @tiedev for your help.
Update: It’s still not ideal since the “solution” only adds the class folder to the compile path, it doesn’t treat it as a resource (e.g., copy it to the runtime class folder).