使用了相对路径的模型在其父模块执行 debug maven test 而导致的错误

项目目录结构如下:


目录结构

测试代码如下:

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

但是这种解决方法有弊端,需要为每一处相对路径设置,并且,如果使用了配置文件的路径,有时候无法控制。所以尽量使用绝对路径。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。