Search code examples
antbuild

Ant file to build project which has dependencies on another project


I need a build file which builds a java project called A. The project A has a dependency to the java project B. The dependency is defined via Eclipse IDE:

Project A -> Properties -> Java Build Path -> Tab Project -> Add -> B

My questions according how to achieve this project build are:

  1. Is it necessary to attach the whole B.jar file to the A.jar?
  2. Or is it enought to link the compiled class files?
  3. How do I link those ressources?
  4. What is Eclipse doing, when I run project A?

Solution

  • This isn't purely an Ant issue. The question is how do you get this dependency (which I assume is a Jar).

    You setup your Ant build.xml script to check for this dependency in a particular location, and not try to do a build if its not there.

    However, the best way to do this is to use Ivy to handle these types of dependencies. Ivy works with Ant to handle dependency management and actually work with another build/dependency system called Maven.

    The nitty gritty of this whole affair is this:

    • You have a release repository. You can use a Maven repository like Nexus or Artifactory. This is your local repository run by your site. This local repository can connect to the World Wide Maven repository, so all dependencies can be handled by Ivy.
    • Project "B" builds, and deploys this dependency to the Release repository.
    • Project "A" has an ivy.xml file that tells the project that it depends upon this artifact.
    • When you build project "A", it automatically fetches the required artifact from the Release Repository and uses it.

    It all sounds simple, except that Ivy's on line documentation is pretty bad. I highly recommend Manning's Ant in Action by Steve Loughran and Erik Hatcher. They have an excellent chapter on using Ivy. If you use Ant, I highly recommend using Ivy. And, there's an Ivy plugin for Eclipse too.