Last Updated: February 25, 2016
·
2.643K
· hab278

How to package non-Java code/files/resources in a JAR with MAVEN

I was using Java in conjunction with Rhino and JavaScript source files for Citero, and I hadn't stopped to think if they were being packaged or not. Turns out they weren't, but luckily I found out how to do it the Maven way.

First and foremost, you must follow the maven directory structure. Turns out, non-java resources don't go in src/main/java/ (duh!). They go in src/main/resources (doi!). Maven will automatically package everything in that directory, and retain the directory structure when you run mvn package.

Secondly, to access those same resources from within your project, you can use the getClass().getResource() or the getClass().getResourceAsStream() methods.

And finally you can use StringWriter and IOUtils (Apache Commons) to convert that sucker to a string!