Maven and JPA Programming

It cannot be overstated just how helpful it is to have a lightweight persistence system in your testing configuration. The problem with JPA annotations like @PersistenceUnit for dependency injection is that a lot of your primary code can only be run from within a full-fledged J2EE application server.

I wanted to take a moment to rant a little about Maven and JPA programming. If you haven’t switched to the Maven way of things (It took me years to give into the dark side!) or you just started doing Maven-ized projects recently, you might still be getting the hang of things.

I won’t lie: it can be relatively hard to get used to. For example, take a look at the traditional directory structure for your project:

standard Maven directory layout

This may look a bit complex (like the first time you looked at a Unix directory structure!?) but in fact it’s a fantastic convention. For example, it’s so amazingly easy for me to create sample data files, drop them into src/test/resources/sampleData and then pull the data will a quick Unit Test using

InputStream sampleData = getClass().getResourcesAsStream("/sampleData/myFile");

and away you go! Continue reading “Maven and JPA Programming”