Eclipse 将 ** 的排除模式添加到 src/main/resources: 如何读取资源文件?
Eclipse adds an exclusion pattern of ** to src/main/resources: how to read a resource file?
我使用 Eclipse (Oxygen.2 Release (4.7.2)) 创建了一个带有标准 src/main/resources 文件夹的简单 Maven 项目,并将其添加到类路径中。问题是 Eclipse 将 **
的排除模式添加到 src/main/resources 文件夹。这里有一些图片可以更好地说明情况:
你可以自己重现这种情况,只要记得运行Maven -> 更新项目...
根据 this 的回答,这不是错误,而是正确的行为。
所以问题是:如何从 src/main/resources 读取资源文件?
我不能使用显式路径 src/main/resources 因为编译结果中没有这样的路径 我不能使用 .getResource 或 .getResourceAsStream 因为 Eclipse 排除了该路径上 **
的模式 .
我写这个答案以防其他人遇到同样的问题。
我发现 src/main/resource 文件夹上的 Eclipse 中的排除模式是正常的(参见上面链接的答案)。排除意味着它不是 Eclipse 处理 src/main/resources 文件夹编译,而是 Maven(准确地说是 Eclipse 的 Maven 插件,M2Eclipse)。在类路径中找不到这些资源的事实是由于 pom.xml:
中存在排除项
<resource>
<directory>src/main/resources</directory>
<excludes>
<!-- these resources will be excluded from the classpath; they will not go in to the target/classes folder and will not be packaged into the artifact -->
<exclude>**/*</exclude>
</excludes>
</resource>
步骤 1. 从 eclipse 中删除项目 步骤 2. 重新导入项目 步骤 3. 转到需要添加为源文件夹的文件夹。
我使用 Eclipse (Oxygen.2 Release (4.7.2)) 创建了一个带有标准 src/main/resources 文件夹的简单 Maven 项目,并将其添加到类路径中。问题是 Eclipse 将 **
的排除模式添加到 src/main/resources 文件夹。这里有一些图片可以更好地说明情况:
你可以自己重现这种情况,只要记得运行Maven -> 更新项目...
根据 this 的回答,这不是错误,而是正确的行为。
所以问题是:如何从 src/main/resources 读取资源文件?
我不能使用显式路径 src/main/resources 因为编译结果中没有这样的路径 我不能使用 .getResource 或 .getResourceAsStream 因为 Eclipse 排除了该路径上 **
的模式 .
我写这个答案以防其他人遇到同样的问题。
我发现 src/main/resource 文件夹上的 Eclipse 中的排除模式是正常的(参见上面链接的答案)。排除意味着它不是 Eclipse 处理 src/main/resources 文件夹编译,而是 Maven(准确地说是 Eclipse 的 Maven 插件,M2Eclipse)。在类路径中找不到这些资源的事实是由于 pom.xml:
中存在排除项<resource>
<directory>src/main/resources</directory>
<excludes>
<!-- these resources will be excluded from the classpath; they will not go in to the target/classes folder and will not be packaged into the artifact -->
<exclude>**/*</exclude>
</excludes>
</resource>
步骤 1. 从 eclipse 中删除项目 步骤 2. 重新导入项目 步骤 3. 转到需要添加为源文件夹的文件夹。