Ant tutorial

How to extract common targets from many projects?

Where did I got idea from?
I started to look for the sources of JBoss I've noticed they have huge number of projects, and every of the projects uses its own Ant file. It would be very difficult and wrong to manage common procedures (removing temp files, settings properties, etc) in every project. So they extracted their common targets into one common, available Ant file (entity) and imported it in every custom project.

I copied the idea and made my "common" Ant script, because it happened that I had to manage five projects at one time.

What I want to show you here?
How to import external file into build.xml?
Use XML "entity" feature. Learning by example:
<?xml version="1.0" encoding="iso-8859-2"?>
<!DOCTYPE project [ 
   <!ENTITY common-config SYSTEM "../build-common.xml"> 
]>

<project name="pat" basedir="." default="run">
    <description>
        PAT - Prevayler Automated Transactions nthx at irc.pl
    </description>

    &common-config;
    ...
</project>
    
Assuming that you have '../build-common.xml' file, this file will be "copied" into the place of '&common-config.xml' word.
"The XML entities" idea is similar to defining properties of variables. Just remember, that the whole contents of the file will be pasted here. So the entity file cannot begin with "<xml>" - or sth similar.
BTW: the name of "imported" file could be whatever you like. It is also common to use '.ent' extension for such files.
What common targets can we extract?
I have found useful to extract these targets (and give them special names):
  • common:read_params - for reading build.property file, defining tasks, setting variables
  • common:clean - for removing temp files from 'build' directory
  • common:prepare - for making directories and time stamping
  • common:resources - for copying ${etc.dir} configuration files into ${build.dir} (log4j.properties for example)
  • common:init - calls: common:read_params, common:clean, common:prepare, common:resources
Because all these targets have "common:" as a prefix, so I know that they are extracted into other file.
All your custom targets should depend on common:init target
All of my targets in every project depend on common:init. Now, I don't have to worry about making ${build.dir} every time, or doing some automated tasks. For every project, they are written only once..
Projects directory structure
When I develop few projects simultaneously, then I like to have all the projects in the same directory. Example:
    Ilybra/
        build.xml
    JavaPetStore/
        build.xml
    JBossAOPTest/
        build.xml
    MyDirtyTest/
        build.xml
    Pat/
        build.xml
    
As you see, every of the projects uses its own <build.xml file. It's normal and OK. Because we don't want to repeat ourselves, in every of our projects we "import" one common "build-common.ent" file.
Still, every project is unique. It may have different amount of targets, and different targets. In one project "running" application would depend on "javac compiling" only, but in other (say, AOP project) it would depend on "compiling annotations, compiling sources by aop and using javac at the beginning". But all of them have sth in common, and that we extracted.
Sources
There is no more to say about this. You will experiment by yourself with these. Only thing I can help you with is to provide example sources for extracting targets from several projects. Good luck!
Nice JBoss' style XML comments
One other thing to mention. I saw some nice style of XML comments in JBoss' sources. You may like them maybe:
    <!--
       |
       |  Some explaining information here...
       |  in nice comment style
       |
     -->
    

Designed with CSS| Created with GIMP| Get Firefox!| | Opel Omega Forum|

Copyright © Tomasz Nazar 2007
Revision: $Id: ant_extracting_by_jboss.gtml 3768 2005-07-25 23:47:33Z nthx $