4

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:

enter image description here enter image description here

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.

Luke
  • 1,633
  • 3
  • 23
  • 37
  • Have you tried to use `getResourcesAsStream().`? – khmarbaise Mar 18 '18 at 19:12
  • Eclipse Maven plugin puts a classpath exclusion pattern of ** on src/main/resources so getResource methods don't find those files. – Luke Mar 18 '18 at 19:42
  • Sorry that I'm little bit ignorant..but have you run code like a test ? using something like `this.getClass().getResourcesAsStream("/test-resources.txt")`? Furthermore have you checked on plain command line? running your test without Eclipse? – khmarbaise Mar 18 '18 at 19:51
  • 1
    @Luke Have you actually tried that? Because I did and it works. `getResource(...)` or `getResourceAsStream(...)` work fine for `src/main/resources` in Eclipse/Maven projects. Resource copying is handled by m2e. – lexicore Mar 18 '18 at 19:52
  • 1
    @Luke Exclusion pattern is not a problem. Check your `target/classes` directory, your resources will be there. – lexicore Mar 18 '18 at 19:53
  • Please post a simple test case which tries to read the file via getResourceAsStream ...and run that on command line and check if it works... ? – khmarbaise Mar 18 '18 at 20:13

2 Answers2

5

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>
Luke
  • 1,633
  • 3
  • 23
  • 37
-1

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.

enter image description here

Sujeet
  • 47
  • 5