Ant dependency management

2019-02-08 19:17发布

问题:

It seems that ant dependency management isn't great to say the least... But, is there hope? Today there are better choices for build or project management such as maven, ivy but I'm stuck with a bunch of ant project that depend on one another so I was wondering if there's an "ant best practice" for managing dependencies.

Specifically in my case I have:

w.war

a.jar
b.jar
c.jar

w.war is the war file I deploy to the web server. In the war file I use a.jar. a.jar depends on b.jar, so I happily package b.jar into w as well... That's sort of OK until... The problem starts when b.jar depends on c.jar. The author of a.jar knows about its dependency on b.jar so it can package b.jar into w.war, but it is not aware of the dependency of b.jar on c.jar. Moreover, the author of b.jar could later add even more dependencies such as "b.jar depends on e.jar", so the author of a.jar has no chance following these dependencies as they add up.

What I'd like to have is define "a depends on b" and "b depends on c" (in a different build.xml file) and use ant magic to compile them all into w.war. Is that possible? If not, is there a Best-Practice?

I'm this-close to just rewriting is all in maven, but it's a lot of work... Is there hope to ant?

回答1:

If you already use Ant, then your best bet is to use Ivy for dependency management.

http://ant.apache.org/ivy/

It provides a rich set of ant tasks for dependency manipulation.



回答2:

Just to disillusion you: dependency management in combination with a build system is hard! Unless you have a really simple example, this is not a no-brainer, and you will have to invest some work.

If you want to or have to use Ant, Ivy is definitely a good choice. Being an official sub-project of Ant, it integrates relatively smoothly.



回答3:

I've been using Ivy for the last 4 years (way before it joined Apache) and haven't had any regret.

Without knowing much about your Ant files, it's a little bit difficult to give a definitive answer but I guess that the projects are build in one go. This means that they can't individually publish their artifacts (or jar files) into a central repository.

The solution is to use both a central (for common libraries) and local (for your project) repositories. You can take a look at my public projects and especially the ivysettings.xml file because they use exactly this very principle.



回答4:

Here's my take: use the best of both worlds: Maven just for dependencies and Ant for your day-to-day build heavy-lifting:

Why you should use the Maven Ant Tasks instead of Maven or Ivy



回答5:

There's a new open source build system for Java called EBuild (features) that is a great alternative to Ant/Ivy as it is especially good at dependency management.

There's some detailed articles on the deficiencies of Ant and also Maven on the site.



标签: ant