tags:

views:

2192

answers:

7

Every time I create a new project I copy the last project's ant file to the new one and make the appropriate changes (trying at the same time to make it more flexible for the next project). But since I didn't really thought about it at the beginning, the file started to look really ugly.

Do you have an Ant template that can be easily ported in a new project? Any tips/sites for making one?

Thank you.

+1  A: 

I used to do exactly the same thing.... then I switched to maven. Maven relies on a simple xml file to configure your build and a simple repository to manage your build's dependencies (rather than checking these dependencies into your source control system with your code).

One feature I really like is how easy it is to version your jars - easily keeping previous versions available for legacy users of your library. This also works to your benefit when you want to upgrade a library you use - like junit. These dependencies are stored as separate files (with their version info) in your maven repository so old versions of your code always have their specific dependencies available.

It's a better Ant.

Vinnie
Maven may be better than ant but this does not answer the question. There is not always the luxury of being able to switch to maven.
Martin OConnor
+7  A: 

An alternative to making a template is to evolve one by gradually generalising your current project's Ant script so that there are fewer changes to make the next time you copy it for use on a new project. There are several things you can do.

Use ${ant.project.name} in file names, so you only have to mention your application name in the project element. For example, if you generate myapp.jar:

<project name="myapp">
   ...
   <target name="jar">
      ...
      <jar jarfile="${ant.project.name}.jar" ...

Structure your source directory structure so that you can package your build by copying whole directories, rather than naming individual files. For example, if you are copying JAR files to a web application archive, do something like:

<copy todir="${war}/WEB-INF/lib" flatten="true">
   <fileset dir="lib" includes="**/*.jar">
</copy>

Use properties files for machine-specific and project-specific build file properties.

<!-- Machine-specific property over-rides -->
<property file="/etc/ant/build.properties" />

<!-- Project-specific property over-rides -->
<property file="build.properties" />

<!-- Default property values, used if not specified in properties files -->
<property name="jboss.home" value="/usr/share/jboss" />
...

Note that Ant properties cannot be changed once set, so you override a value by defining a new value before the default value.

Peter Hilton
+1  A: 

I used to do exactly the same thing.... then I switched to maven.

Oh, it's Maven 2. I was afraid that someone was still seriously using Maven nowadays. Leaving the jokes aside: if you decide to switch to Maven 2, you have to take care while looking for information, because Maven 2 is a complete reimplementation of Maven, with some fundamental design decisions changed. Unfortunately, they didn't change the name, which has been a great source of confusion in the past (and still sometimes is, given the "memory" nature of the web).

Another thing you can do if you want to stay in the Ant spirit, is to use Ivy to manage your dependencies.

Damien B
+1  A: 

If you are working on several projects with similar directory structures and want to stick with Ant instead of going to Maven use the Import task. It allows you to have the project build files just import the template and define any variables (classpath, dependencies, ...) and have all the real build script off in the imported template. It even allows overriding of the tasks in the template which allows you to put in project specific pre or post target hooks.

John Meagher
Yes, that is mostly what I'm thinking.. Can you post the template that will be imported with all the real build script please?
pek
A: 

One thing to look at -- if you're using Eclipse, check out the ant4eclipse tasks. I use a single build script that asks for the details set up in eclipse (source dirs, build path including dependency projects, build order, etc).

This allows you to manage dependencies in one place (eclipse) and still be able to use a command-line build to automation.

Scott Stanchfield
A: 

I had the same problem and generalized my templates and grow them into in own project: Antiplate. Maybe it's also useful for you.

Mnementh
+1  A: 

You can give http://import-ant.sourceforge.net/ a try. It is a set of build file snippets that can be used to create simple custom build files.

Miguel Pardal
Aside from the accepted answer, this answer comes the closest to _actually_ trying to answer the question. Not just coming with alternative "better" solutions.
Steen