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!
Written by Hannan
Related protips
Have a fresh tip? Share with Coderwall community!
JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.
Security insights from Cside