2008/11/08

Calling Eclipse Ant builds from the command line

I usually use Eclipse for my software projects and I wanted to do Continuous Integration (CI), so after choosing a CI tool (I chose Hudson, a very powerful yet easy to use tool) I decided to make an Ant build file from my sample Eclipse project.

Even if you only want to make a very simple Ant task, like compiling and JUnit testing, a difficult problem will arise: the classpath. An Eclipse project can have a complex classpath, full of variable references (which are defined in the Preferences of your workspace), references to other projects, and so on. There are some Eclipse ant tasks in Eclipse itself that may be added, like projectBuild, and they take into account the classpath defined in the project, but when you try to add a JUnit task, you have to specify the classpath by hand in the task parameter. This classpath creation is tedious and time-consuming.

I thought it should be easier the creation of an Ant build from an Eclipse project, because the classpath information is in the .classpath project file. After googling a little, I found this great Eclipse Plugin, called eclipse2ant, which "tries to create a buildfile that matches the Eclipse project as close as possible". And it really works. I could very easily create an Ant build file from my Eclipse project, with the usual targets of compilation and it also exported my last JUnit runs! (the ones that you see in the "Run history..." menu of Eclipse).

All the classpath variables were imported in the Ant file, and I could execute the tests of my project without problems with Eclipse closed. Of course, you may want to tweak a little bit the Ant file to make it more portable or to organize better the variables, but with eclipse2ant you have a very good starting point that saves you a lot of time!

Happy continuous integration!