Travis CI和带有Gradle的JUnit无法找到类路径资源
我有一个项目,其完整源代码可以在其GitHub页面找到这里上的 feat / config
分支。我遇到的问题是 ClassName.class.getClassLoader()。getResourceAsStream()
总是返回 null
,并创建具有完整路径(包括源目录)的 File
也返回 FileNotFoundException
。
I have a project whose complete source you can find at its GitHub page here on the feat/config
branch. The problem I am having is that ClassName.class.getClassLoader().getResourceAsStream()
always returns null
, and creating a File
with the complete path (including the source directory) also returns a FileNotFoundException
.
该代码应该从类路径资源中加载默认配置值,并将其写入 File
。它在本地工作,但在Travis CI上失败。
This code should load the default configuration values out of a classpath resource and write it to a File
. It works locally but fails on Travis CI.
public static void writeDefault(String res, File out) throws IOException {
if (!out.exists()) {
out.getParentFile().mkdirs();
out.createNewFile();
}
OutputStream stream = new FileOutputStream(out);
InputStream in = FileConfig.class.getClassLoader().getResourceAsStream(res);
if (in == null) {
stream.close();
throw new FileNotFoundException(res);
}
IOUtils.copy(in, stream);
stream.close();
}
@Test
public void testFileConfig() throws IOException {
System.out.println("Creating default config file");
File testConfigFile = new File("configs/test.txt");
if (!testConfigFile.exists())
FileConfig.writeDefault("configs/test.txt", testConfigFile);
System.out.println("Loading config file");
Config testConfig = FileConfig.loadConfig(testConfigFile);
System.out.println("Loaded value: " + testConfig.getValue("value-1"));
System.out.println("Deleting config file");
testConfigFile.deleteOnExit();
}
上述测试失败,出现以下堆栈跟踪:
The above test fails with the following stack trace:
jtrial.config.TestConfig > testFileConfig STANDARD_OUT
Creating default config file
jtrial.config.TestConfig > testFileConfig FAILED
java.io.FileNotFoundException: configs/test.txt
at jtrial.config.FileConfig.writeDefault(FileConfig.java:111)
at jtrial.config.TestConfig.testFileConfig(TestConfig.java:24)
.travis.yml
配置文件:
language: java
jdk:
- oraclejdk8
notifications:
email: false
install:
- chmod +x ./gradlew
- ./gradlew --info assemble
script: ./gradlew --info check
Ahem ...准备行事和面子!
Ahem... PREPARE TO CRINGE AND FACEPALM!
我刚才意识到...那......我在 .gitignore
configs /
/ code>文件。这不仅忽略了根项目目录中的 configs /
目录,而且还忽略了 configs /
的每个实例。我用 ./ configs /
替换它,然后ACTUALLY将文本文件添加到新的提交中。 * facepalms到一个新的维度*
I just realized... that... I had configs/
on the .gitignore
file. This wasn't only ignoring the configs/
directory in the root project directory, but every instance of configs/
. I just replaced it with ./configs/
and then ACTUALLY added the text files to a new commit. *facepalms to a new dimension*