User Libraries in Eclipse

I have a smallish web project in Eclipse. It has a couple of Hibenate persistent classes, and two DAOs. I use Spring and a couple of other components.

I also have test cases for my DAOs. Now, when I write test cases, I often dislike relying on the state of my actual database. So I’ve picked up the habit of using an in-memory database for JUnit test purposes. Usually, I use HSQLDB (also known, slightly inaccurately, as Hypersonic). I create an in-memory database when the suite starts, use Hibernate to create my table schema, and seed it with any test data I’m looking for.

Here’s the annoyance. In order to use HSQLDB, I need to have the hsqldb.jar on my classpath. And because it’s a web project, that jar will get deployed to my JBoss instance when I use MyEclipse to deploy to my application server. And it serves no good purpose when so deployed.

So, as an experiment, I’ve been trying out User Libraries in Eclipse. I can, for example, go into the Preferences dialog of Eclipse, and under Java/Build Path/User Libraries, I can create a user library for HSQLDB.

Further, I can go to the MyEclipse deployment options and change the options for deployment so that MyEclipse doesn’t try to deploy my user libraries when it deploys my application:

Using the “deploy tab”, I tell MyEclipse that user libraries shouldn’t be deployed when I try to deploy my J2EE projects.

The benefits: I get some ability to distinguish items on my build path that need to be deployed from items on my build path that don’t need to be deployed. What’s more, I can reuse my User Library across multiple projects.

The big downside is that my User Library needs to be set up in Eclipse (although I can export a copy of my user library definitions and save those with my project) – I must ensure that other machines have configured the same user library before they can simply check out the project from CVS.

Another example: I use Hibernate for a lot of object-relational persistence. Because I’m deploying to JBoss, and JBoss already comes bundled with Hibernate, I can configure a User Library with reference to my Hibernate jars in my JBoss installation. I don’t need to keep a separate copy of those jars with my project, and I definitely don’t need to wait while those jars deploy with my application. User libraries seem like a neat trick for avoiding building up annoyingly long classpaths. And when we’re dealing with Hibernate, which requires 9 different jar files, keeping extra copies lying around can be pretty irritating. (It’s also easier to set up a new Hibernate project from scratch – just add the User Library).

I’m not entirely sure that I’m sold on this approach, but I have been experimenting with it. It would certainly be nicer if it were easier to cleanly separate the jars you need to build and run tests from the jars you want to deploy. And this use of User libraries seems like an elegant solution.

It's only fair to share...
Share on Facebook
Tweet about this on Twitter
Share on LinkedIn

Leave a Reply