使用 gradle 以编程方式将资源文件夹添加到测试 compile/runtime 类路径
Using gradle to programmatically add resource folder to test compile/runtime classpath
我们有一个用于 60 多个组件的主构建脚本。各个组件没有 build.gradle
个文件。我想做的是以编程方式(在 master build.gradle
中)向某些项目添加资源文件夹。此资源文件夹包含一个文件,当单元测试为 运行 时,该文件必须位于类路径中。我正在尝试将其添加到 subprojects
块中,如下所示:
subprojects { 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]
}
}
}
}
...
if(proj.name == "APROJECT"){
proj.tasks['test'].getClasspath().each {
logger.info "COMPILE CLASSPATH: {}", it
}
}
}
但是,如果我查询 test
任务的类路径(见上文),我在类路径中看不到该文件夹。此外,当然,测试失败是因为该文件夹不在类路径中。
如果我将 sourceSet
更新放在组件文件夹的 build.gradle
中,它会按预期工作。
我是不是做错了什么?如何将此文件夹放入类路径进行测试?
我无法通过动态更新 sourceSet
使其正常工作,但是,我 能够通过添加必要的资源使其正常工作testCompile
依赖项的路径。请参阅 this 以将 class 文件夹添加到依赖项。
更新:它仍然不是一个理想的解决方案,因为 "solution" 只将 class 文件夹添加到编译路径,它没有将其视为资源(例如,将其复制到runtime class 文件夹)。
更新 #2:它实际上按预期工作。事实证明,不同的测试引用的资源路径略有不同。如上所述动态添加所有资源路径工作正常!
我们有一个用于 60 多个组件的主构建脚本。各个组件没有 build.gradle
个文件。我想做的是以编程方式(在 master build.gradle
中)向某些项目添加资源文件夹。此资源文件夹包含一个文件,当单元测试为 运行 时,该文件必须位于类路径中。我正在尝试将其添加到 subprojects
块中,如下所示:
subprojects { 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]
}
}
}
}
...
if(proj.name == "APROJECT"){
proj.tasks['test'].getClasspath().each {
logger.info "COMPILE CLASSPATH: {}", it
}
}
}
但是,如果我查询 test
任务的类路径(见上文),我在类路径中看不到该文件夹。此外,当然,测试失败是因为该文件夹不在类路径中。
如果我将 sourceSet
更新放在组件文件夹的 build.gradle
中,它会按预期工作。
我是不是做错了什么?如何将此文件夹放入类路径进行测试?
我无法通过动态更新 sourceSet
使其正常工作,但是,我 能够通过添加必要的资源使其正常工作testCompile
依赖项的路径。请参阅 this 以将 class 文件夹添加到依赖项。
更新:它仍然不是一个理想的解决方案,因为 "solution" 只将 class 文件夹添加到编译路径,它没有将其视为资源(例如,将其复制到runtime class 文件夹)。
更新 #2:它实际上按预期工作。事实证明,不同的测试引用的资源路径略有不同。如上所述动态添加所有资源路径工作正常!