项目目录结构如下:
目录结构
测试代码如下:
public class TempTest {
@Test
public void test01() {
String filePath = "target/test-classes/data/file01.txt";
File file = new File(filePath);
boolean exists = file.exists();
System.out.println(exists);
Assert.assertTrue(exists);
}
}
在 modelA 执行 run maven test:
model A执行 run maven test
成功。
在父模块执行run maven test:
父模块prj01执行 run maven test
成功。
在modelA执行debug maven test:
modelA 执行 debug maven test
成功。
在父模块执行debug maven test:
父模块prj01执行 debug maven test
执行失败。
可能存在的原因:user.dir 与 jvm 启动路径不一致时引发的一系列问题
run maven jvm 启动路径是每个模块的路径,debug maven jvm 启动路径是根路径。
临时解决方法:使用new File(path).getSolutePath
使用new File(path).getSolutePath
但是这种解决方法有弊端,需要为每一处相对路径设置,并且,如果使用了配置文件的路径,有时候无法控制。所以尽量使用绝对路径。