Java CLI测试失败,传入IDE-加载资源
项目设置为在其他模块中共享来自一个模块的资源。在Intellij中运行测试时,测试通过,但在命令行上找不到资源,并引发此异常: {workspacePath}/projectDir/moduleWithResources/build/libs/module-with-resources.jar/myJsonFile.json(没有这样的文件或目录) 从IDE运行时,到资源的路径为: {workspacePath}/projectDir/moduleWithResources/build/classes/test/ 为什么路径与IDE与CLI不同?该项目有多个模块,相关资源位于src/main/resources/*。为了使其他模块中的资源可用,我添加了此gradle任务:Java CLI测试失败,传入IDE-加载资源,java,unit-testing,android-studio,intellij-idea,gradle,Java,Unit Testing,Android Studio,Intellij Idea,Gradle,项目设置为在其他模块中共享来自一个模块的资源。在Intellij中运行测试时,测试通过,但在命令行上找不到资源,并引发此异常: {workspacePath}/projectDir/moduleWithResources/build/libs/module-with-resources.jar/myJsonFile.json(没有这样的文件或目录) 从IDE运行时,到资源的路径为: {workspacePath}/projectDir/moduleWithResources/build/clas
task copyResources(type: Copy) {
from "${projectDir}/src/main/resources"
into "${buildDir}/classes/main"
}
processResources.dependsOn copyResources
我的目标是能够将此模块作为“compile project()”依赖项添加到其他模块中,并访问资源
任何帮助都会很好!看起来gradle目前并不很支持这种设置
为什么路径与IDE与CLI不同
因为IDE运行分解代码。
尝试按类加载器加载资源
MyCLass.class.getClassLoader().getResourceAsStream(name)
希望这能有所帮助(我也遇到过同样的问题,但不记得该如何解决)