问题
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:
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.
So the question is: how do i read a resource file from src/main/resources?
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.
回答1:
I write this answer in case someone else has the same problem.
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>
回答2:
step 1. delete the project from eclipse step 2. import the project freshly step 3. Go to the folder required to be added as source folder.
来源:https://stackoverflow.com/questions/49351806/eclipse-adds-an-exclusion-pattern-of-to-src-main-resources-how-to-read-a-res