Tuesday, May 19, 2009

Maven and Resources

I spent a serious amount of time recently trying to fix a problem that didn't exist due to my not understanding resources and maven. I'm creating a project that has some default resources to load during execution time. In maven you put such projects under the src/main/resources folder. When you use the jar:jar plug-in to build a .jar file for the project, these resources are put at the top level of the .jar.

So, the perceived problem that I had was that I believed during test I would need to determine which environment (maven or execution) I was running in and load the resources specific to that environment. WRONG! Maven will copy the src/main/resources to target/classes/ when building and then test from there, so the resource path is the same.

In both cases, all that is needed is to call class.getResource(""). This greatly simplified the code.