我使用 Eclipse(Oxygen.2 Release (4.7.2))和标准 src/main/resources 文件夹创建了一个简单的 Maven 项目,并将其添加到类路径中。问题是 Eclipse 添加了一个排除模式**
到 src/main/resources 文件夹。以下是一些图片,可以更好地说明情况:
您可以自己重现这种情况,只需记住运行 Maven -> 更新项目...
根据this https://stackoverflow.com/questions/7754255/maven-m2eclipse-excludes-my-resources-all-the-time回答这不是错误,而是正确的行为。
所以问题是:如何从 src/main/resources 读取资源文件?
我无法使用显式路径 src/main/resources 因为编译结果中不会有这样的路径我无法使用 .getResource 或 .getResourceAsStream 因为 Eclipse 设置了排除模式**
在那条路上.
我写这个答案是为了防止其他人遇到同样的问题。
我发现 Eclipse 中 src/main/resource 文件夹中的排除模式是正常的(请参阅上面链接的答案)。排除意味着处理 src/main/resources 文件夹编译的不是 Eclipse,而是 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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)