Resources from src/main/resources not included for test

I have a problem that the files from src/main/resources are not copied and included when i run tests.

I have a persistence.xml file, and I am using eclipse link. The persistence.xml file defines an in memory h2 database. I have a single test that attempts to access these entities. The problem is that no entities are registered with the entity manager.

Checking the build/classes/main directory, I don’t find the persistence.xml file, but it is included in the final jar. This causes my tests to fail.

This is a project I converted from maven2.

This might be some simple problem with the classpath. You can try debugging it if you add a dummy test that just attempts to read this particular file from the classpath, e.g. classLoader.getResource()…

The reason you don’t see resources output in the classes/main is that they end up in a different folder: http://gradle.org/docs/current/dsl/org.gradle.api.tasks.SourceSetOutput.html#org.gradle.api.tasks.SourceSetOutput:resourcesDir

The problem might be something bigger, though, related to the JPA boot strapping via persistence.xml. More info here: http://forums.gradle.org/gradle/topics/regression_with_classloading_in_the_jetty_plugin_with_gradle_1_0_milestone_6?from_gsfn=true

Hope that helps!

The resources won’t be in ‘build/classes/main’. They are put into ‘build/resources/main’.

Are they in that directory?

I have having a similar issue, however I am able to get the files from src/main/resources to show up on build/classes/main with the following:

sourceSets.main.groovy.srcDirs = [‘buildSrc/src/main/groovy’] sourceSets.main.resources.srcDirs =[‘buildSrc/src/main/resources’]

However, even though this is the case, my resource does not show up on the classpath and am therefore unable to load it from my task class using class.getResourceAsStream(…). I am trying to create a custom task that loads a file from the classpath and uses it to generate a configuration file. This sounds similar to the post at the beginning of this thread.

–Jonathan

Ugh…typo on my part. Once I got the correct filename passed to getResourceAsStream, it worked. That doesn’t explain why my resource is showing up in build/classes/main if the belief is that it should be in a different subfolder under build. Any insight would be appreciated.

Seems the problem is as described in what you linked. Since persistence.xml doesn’t end up in build/classes, EclipseLink will not find the entity classes. What EclipseLink does, and I guess the JPA spec, is look for the classpath entry where persistence.xml is located, and search for classes from that root. So I need to put resouces into build/classes/main

I’ll close this one off as there is another issue raised about this particular proble . The end result will be that resources and classes will be by default in the same output directory in some manner.

Found a solution that works for me:

sourceSets {
    main {
       output.resourcesDir = "build/classes/main"
     }
  }
1 Like