如果在 gradle 插件任务中使用,win10 上的 getResourceAsStream returns null
getResourceAsStream returns null on win10 if used in gradle plugin task
我需要将文件从 gradle 插件 jar 复制到项目的根目录,该项目将插件作为 gradle 任务的一部分应用。
我在 gradle 插件 jar 中有一个文件放在 src/main/resources/data/file
我在 gradle 任务中有以下代码:
String filename = "data" + File.separator + "file";
final InputStream stream= classLoader.getResourceAsStream(fileName);
final Path filePath = new File(filename).toPath();
new File("data").mkdirs();
try {
Files.copy(stream, filePath , StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new PluginApplicationException("Failed to apply plugin", e);
}
我希望这段代码从 gradle 插件 jar 中获取一个文件并将其放入 /data/file。
当我 运行 插件任务时,它在 linux 上工作正常,但它在 windows 上崩溃,因为 classLoader.getResourceAsStream(fileName);
returns null.
我尝试使用不同的方法来获取 class 加载程序,但在 windows 和 linux 上它始终是 VisitableURLClassLoader。有人知道为什么它在 windows 上为空吗?
不要使用 File.separator
。对于资源,它总是 /
.
我需要将文件从 gradle 插件 jar 复制到项目的根目录,该项目将插件作为 gradle 任务的一部分应用。
我在 gradle 插件 jar 中有一个文件放在 src/main/resources/data/file
我在 gradle 任务中有以下代码:
String filename = "data" + File.separator + "file";
final InputStream stream= classLoader.getResourceAsStream(fileName);
final Path filePath = new File(filename).toPath();
new File("data").mkdirs();
try {
Files.copy(stream, filePath , StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new PluginApplicationException("Failed to apply plugin", e);
}
我希望这段代码从 gradle 插件 jar 中获取一个文件并将其放入 /data/file。
当我 运行 插件任务时,它在 linux 上工作正常,但它在 windows 上崩溃,因为 classLoader.getResourceAsStream(fileName);
returns null.
我尝试使用不同的方法来获取 class 加载程序,但在 windows 和 linux 上它始终是 VisitableURLClassLoader。有人知道为什么它在 windows 上为空吗?
不要使用 File.separator
。对于资源,它总是 /
.