views:

615

answers:

5

I'm working on a large C++ system built with ant+cpptasks. It works well enough, but the build.xml file is getting out of hand, due to standard operating procedure for adding a new library or executable target being to copy-and-paste another lib/exe's rules (which are already quite large). If this was "proper code", it'd be screaming out for refactoring, but being an ant newbie (more used to make or VisualStudio solutions) I'm not sure what the options are.

What are ant users' best-practices for stopping ant build files exploding ?

One obvious option would be to produce the build.xml via XSLT, defining our own tags for commonly recurring patterns. Does anyone do that, or are there better ways ?

+9  A: 

you may be interested in:

Check also this article on "ant features for big projects".

dfa
The links don't work anymore. Fixed links: http://ant.apache.org/manual/Tasks/macrodef.html and http://ant.apache.org/manual/Tasks/import.html and http://ant.apache.org/manual/Tasks/subant.html
nhnb
+4  A: 

If the rules are repetitive then you can factor them into an ant macro using macrodef and reuse that macro.

If it is the sheer size of the file that is unmanageable, then you can perhaps break it into smaller files and have the main build.xml call targets within those files.

If it's neither of these, then you may want to consider using a build system. Even though I have not used Maven myself, I hear it can solve many issues of large and unmanageable build files.

Parag
+1  A: 

I would try Ant-Ivy- the agile dependency manager. We have recently started using it for some of our more complex systems and it works like a charm. The advantage here is that you dont get the overhead and transition cost to maven (it uses ant targets so will work with your current set up). Here is a comparison between the two.

Shane C. Mason
+3  A: 

Generally, if your build file is large and complex then this is a clear indication that the way you have your code layed out, in terms of folders and packages, it complex and too complicated. I find that a complex ant script is a clear smell of a poorly laid out code base.

To fix this, think about how your code is laid out. How many projects do you have? Do those projects know how to build themselves with a master build script that knows how to bundle the individual projects/apps/components together into a larger whole.

When you are refactoring code, you are looking at ways or breaking things down so that they are easier to understand--smaller methods, smaller classes, methods and classes that do one thing. You need to apply these same principles to your code base as well.

Create smaller components that are functionally cohesive and are very loosely decoupled from the rest of the code. Use a build script to build that component into a library. Do this with the rest of your code. Now create a master build script that knows how to bundle up all of your libraries and build them into your application. If you have several applications, then create build script for each app and a master one that knows how to bundle the apps into distributables.

You should be able to see and understand the layout and structure of your code base just by looking at your build scripts. If they/it is not clean and understandable then neither is your source code.

Chris Johnston
+3  A: 

Use Antlib files. It's a very clean way to

  1. remove copy/pasted code
  2. define default values

If you want to see an example, you can take a look at some of the build script I'm writing for my sandbox projects.

Vladimir
antlib is a very reusable library, +1
dfa