Patrick Jackson
Patrick Jackson

Reputation: 19446

Test fail from CLI, pass in IDE - loading resources

Project is setup to share resources from one module in other modules. When running tests in Intellij they pass, however on command line the resources are not found and this exception is thrown:

{workspacePath}/projectDir/moduleWithResources/build/libs/module-with-resources.jar!/myJsonFile.json (No such file or directory)

When ran from the IDE the path to resource is: {workspacePath}/projectDir/moduleWithResources/build/classes/test/

Why would the paths be different from IDE vs CLI? The project has multiple modules, and the resources in question are located in src/main/resources/*. To make the resources available in other modules, I've added this gradle task:

task copyResources(type: Copy) {
    from "${projectDir}/src/main/resources"
    into "${buildDir}/classes/main"
}
processResources.dependsOn copyResources

My goal is to be able to add this module to other modules as a 'compile project()' dependency and access the resources.

Any help would be great! Seems this is not a setup that gradle supports very well at this time.

Upvotes: 3

Views: 767

Answers (1)

komelgman
komelgman

Reputation: 6999

Why would the paths be different from IDE vs CLI?

Because IDE run exploded code. Try load resource by classloader

MyCLass.class.getClassLoader().getResourceAsStream(name)

Hope this helps (I had the same problem, but don't remember how exactly fix it).

Upvotes: 2

Related Questions