[英]No such File exception in Jenkins, but runs fine locally
I have a JUnit test file for my code that reads an xml file and converts it to string: 我的代码有一个JUnit测试文件,该文件读取xml文件并将其转换为字符串:
String xml = new String(Files.readAllBytes(Paths.get("src\\test\\resources\\testfile.xml")));
The test runs and passes locally, but when I run a Jenkins build, it fails with java.nio.file.NoSuchFileException: src\\test\\resources\\testfile.xml 该测试在本地运行并通过,但是当我运行Jenkins构建时,它将失败并显示java.nio.file.NoSuchFileException:src \\ test \\ resources \\ testfile.xml
Do I need to change my file path when pushing? 推送时是否需要更改文件路径?
you should use resource as stream for this: 您应该为此使用资源作为流:
InputStream is = getClass().getClassLoader().getResourceAsStream(fileName);
if (is != null) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String xml = reader.lines().collect(Collectors.joining(System.lineSeparator()));
}
(code can be improved a lot ... but it will give you a direction) (代码可以改进很多……但是它将为您提供指导)
(check out try by resource to handle streams) (签出按资源尝试处理流)
Alright, so I figured out my problem and I feel pretty silly. 好了,所以我发现了问题,觉得很傻。 When Java builds on my windows machine, using \\ to seperate files is fine;
当Java在Windows机器上构建时,使用\\分隔文件就可以了; however when Jenkins builds, files need to be seperated with /
但是,当詹金斯(Jenkins)生成时,文件必须用/分隔
ie my file names should be 即我的文件名应该是
src/test/resources/testfile.xml
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.