Resources missing from test jar during test

I am writing a plugin that copies resources bundled in the jar (src/main/resources) to the file system. I get the jar using this code:

String jarPath = getClass().getProtectionDomain()

But during the unit test I use the GradleRunner and the jar file being used (build/caches/jars-9/743fc57b9bb678be7a9e07f8300ebfe5/main.jar) does not have the resources. Only the final jar does. So the tests just fail. How do I fix this without having different source code between the real jar and the test jar?

Please, do not tell me to use getResourceAsStream() I don’t want to explicitly list all the resources in the source code. I just want to copy a tree of resources starting from a given resource folder.

I should add that I try to use this code from mkyong in my application to load the resources.

I don’t want to explicitly list all the resources in the source code.

Well, you could simply list those resources at build time and put it to a file that is packaged too.
Then you can read that file and iterate over that list to get all intended resources using getResourceAsStream. :slight_smile:

But that file itself would be a resource, bringing me back to the original problem: the jar file used in testing (build/caches/jars-9/…/main.jar) does not contain the resources. So I don’t know how to write the production code to read from its own jar… in the end the resources are in the production jar in build/libs/name-1.0.0.jar. You can tell I am a bit lost.

Hard to say without an MCVE.
Actually when running tests there should be no jar at all but the classes and resources used from folders.
And using getResourceAsStream would then work either way.
I have no idea why it should there use a half-baked jar and especially one from build/caches.