【问题标题】:getResourceAsStream returns null on win10 if used in gradle plugin task如果在 gradle 插件任务中使用 getResourceAsStream 在 win10 上返回 null
【发布时间】:2019-11-28 09:19:23
【问题描述】:

我需要将一个文件从 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); 返回 null。

我尝试使用不同的方式来获取类加载器,但它在 windows 和 linux 上始终是 VisitableURLClassLoader。有人知道为什么它在 Windows 上为空吗?

【问题讨论】:

    标签: java gradle gradle-plugin


    【解决方案1】:

    不要使用File.separator。对于资源,它始终是/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-19
      • 1970-01-01
      • 2023-03-30
      • 2011-07-11
      • 2013-10-02
      相关资源
      最近更新 更多