Referencing Java Project Libraries in WARs with Eclipse

A more complete title for this post might be “Referencing another Java Project and Its References in Eclipse When Creating a Web Archive File,” but that's a little long. We'll be doing that here, with a focus on using the Eclipse IDE. In a future post we'll be performing the same task but using Ant.

Note: All source code in this post is available on GitHub.

The Setup

We have a Dynamic Web Project called Janglesweb which references our core Jangles Project. The Jangles Project references a number of JARs in its /lib folder.

We're going to add a reference to the Jangles project and to its /lib folder so we can build our Janglesweb WAR and deploy it to Tomcat.

Adding the References to Jangles and Its /Lib JARS in Janglesweb

Eclipse makes adding references real easy. In Project Properties we select “Java Build Path” and in the Projects tab add Jangles.

Next we select “Deployment Assembly” (shown above at left) and add archives from the File System.

We then select the JARs in our Jangles /lib folder with the result looking like this.

Tying Things Together

We never mentioned how Janglesweb will talk to our Jangles Core Project. We'll be using JAX-WS for that, using Jangles resources in our Janglesweb Web Service and delivering them to our Web JSP pages. Here we can see that our Janglesweb Web Service is successfully referencing the Jangles Core API library as we import com.nixmash.jangles.api.JangleWebApi.

Creating the WAR

We're going to use the Eclipse IDE all the way for this post. As I mentioned, we'll be doing this again with Ant since we nearly always have to fine-tune our WAR, but this approach will get the job done for our development purposes today.

Our Jangles Project and its JAR libraries have been successfully referenced in Janglesweb so we can now create our Web Archive file. Right-click on Janglesweb and select Export.

Then WAR File…

And the details for the WAR file.

You'll notice we're outputting the WAR to our Tomcat /webapps directory so that Tomcat will automatically expand it to /webapps/jangles.

The Proof

We can now go to the http://jangles site we created in Tomcat with the Apache Proxy Mod to prove that Eclipse produced everything we needed for our quick-n-dirty WAR deployment.

We have very little code in our Janglesweb project other than the Web Service we saw earlier. Yet the Jangles Core Project is retrieving content from MySQL, and from it creating a JangleUser data object and saying hello to our JAX-WS JSP Client!

Next up (or at least soon): project referencing and WAR building in Ant.

Posted February 03, 2014 10:59 PM EST

More Like This Post