问题描述
我使用标准src/main/resources文件夹使用Eclipse(Oxygen.2版本(4.7.2))创建了一个简单的Maven项目,并将其添加到类路径中.问题是Eclipse在src/main/resources文件夹中添加了**
的排除模式.以下是一些图片,可以更好地说明这种情况:
I created a simple Maven project with Eclipse (Oxygen.2 Release (4.7.2)) with the standard src/main/resources folder and added it to the classpath. The problem is that Eclipse adds an exclusion pattern of **
to the src/main/resources folder. Here are some pics to better explicate the situation:
您可以自己重现这种情况,只记得运行Maven->更新项目...
You can reproduce the situation yourself, just remember to run Maven -> Update project...
根据此回答,这不是一个错误但这是正确的行为.
According to this answer this is not a bug but it's the correct behaviour.
所以问题是:我如何从src/main/resources中读取资源文件?
我不能使用显式路径src/main/resources,因为在编译结果中将没有这样的路径,并且我不能使用.getResource或.getResourceAsStream,因为Eclipse将排除模式设置为在该路径上.
I can't use the explicit path src/main/resources since there will be no such path in the compiled results and I can't use .getResource or .getResourceAsStream since Eclipse put an exclusion pattern of **
on that path.
推荐答案
如果有人遇到同样的问题,我会写这个答案.
I write this answer in case someone else has the same problem.
我发现src/main/resource文件夹中Eclipse中的排除模式是正常的(请参见上面链接的答案).排除意味着不是Eclipse处理src/main/resources文件夹编译,而是Maven(确切地说是Eclipse的Maven插件,M2Eclipse).在类路径中找不到这些资源的事实是由于pom.xml中存在排除项:
I found that the exclusion pattern in Eclipse on src/main/resource folder is normal (see the answer linked above). The exclusion means that it's not Eclipse handling the src/main/resources folder compilation but it's Maven (the Maven plugin of Eclipse to be precise, M2Eclipse). The fact that those resources weren't found in the classpath was due to an exclusion present in the 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>
这篇关于Eclipse在src/main/resources中添加了**排除模式:如何读取资源文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!