This is the second post on referencing projects and libraries when building Java WAR files in Eclipse. The first post demonstrated using Eclipse exclusively for the task. This post will focus on using Ant build files for the job, with a little help from Eclipse.
Note: All source code in this post is available on GitHub.
Project Layout and Ant Build Strategy
Here’s our Project Layout, where we have a Janglesweb Dynamic Web Project which references the Jangles Core Project and its support libraries like the MySQL Connector, Logger, Caching and so on. Both Jangles and Janglesweb will contain a primary build.xml and a supplemental Ant .xml build file to perform project-specific tasks.
Creating the Project Ant Build Files
We’re going to let Eclipse create the Ant files which will perform our project builds. The idea is to hand the job over to Eclipse for basic compile and build that we won’t need to touch or modify. Our custom logic will be in our supplemental build files, distrib.xml and war.xml.
The goal of the Eclipse generated build.xml files is to build the projects only. Eclipse generated build.xmls also build any referencing projects, so there is some housecleaning to perform if you wish. Retaining the original build.xml won’t cause any problems. We don’t need to execute anything here, so we can remove any java tasks like the one below.
If we’ve assigned our references in Janglesweb to Jangles and its libraries correctly and used Eclipse to create our Janglesweb build.xml we should have everything we need to deploy our WAR with the exception of one JAR: the Jangles Project JAR! So we want to create a Jangles.JAR that we can add to the WAR WEB-INF/lib directory. We’re going to add an Ant distrib.xml build file to do that. Sidenote: We’re naming the build file distrib.xml because we may want to add other files in the distrib folder later.
Notice we import the Jangles build.xml. We’ll run distrib.xml as an Ant Build.
With the happy result of a Jangles.JAR file in our /dist folder.
Building the Janglesweb WAR file
We used Eclipse to build our Janglesweb build.xml file, so in keeping with the same logic of adding a supplemental Ant build file we create a war.xml in Janglesweb. Again, we’re importing the project’s Eclipse-generated build.xml file. You’ll also notice we are adding jangles.jar to our WAR /lib folder.
We run war.xml, generate our WAR…
and find Jangles.JAR and all of its references in our WEB lib.
I should subtitle this post as “One Man’s Approach” because there are certainly other ways to generate a WAR with referenced project JARs. I chose this approach because I’m leveraging Eclipse to generate basic build.xmls that I do not need to change, isolating custom task logic in the supplemental build files.