Running ANT from within an Eclipse Plugin

This is once again one of those things which I tend to forget and I have to put them here in order for me to conveniently reference them when I need to. I am creating a plugin to launch my automated task which I wrote using an ANT script. The ANT script I wrote is customized using a build.properties file. Here is an example of an ant script.

buildxml.jpg

The build.properties file contains the following:

message=Hello World!

To run the build.xml, I only need to right-click it on the Navigator to open the pop-up menu. I choose Run As to get the following sub-menu:

run_as.jpg

I choose selection 2 above to run the default target called “test”. The output looks like the following:

run_console.jpg

Now there can be many properties which I can configure which I do by editing the properties file. However, there will come a time when you will need to pass this program to someone else and you want to make it idiot proof. Or you yourself will get tired of editing the properties file and would want something much easier, like for example choosing from a drop-down menu. So in times like these, you would like to make a simple gui to configure your properties file. This is what I did. I created a simple eclipse plugin which I can just click on the tool bar in order to launch the configurator. Here is a sample configurator for the  build.properties:

simple_gui.jpg
Now, what I would like is after specifying my message and clicking OK, I will be able to run my ant script immediately. The most immediate implementation is to run the ant executable using Runtime.exec() command. However, the code is quite long and you have to specify the ant directory which might be different in other user’s environment. Also, you would want to capture the out put of the ant execution, so you will have to write another code to show the output in a gui. However, what if we could actually use the right-click mechanism, as shown above, programmatically?

Here’s how I did it. I made use of the org.eclipse.debug.core package to programmatically run the ant script. However, I need to generate a launch configuration which I can easily do by righ-clicking in the build.xml file and choosing “Run As-> Ant Build… “. Notice that the “…” means to open the Ant configurator.

ant_config.jpg

Click on the common tab as shown above and click on “Shared file”. You can specify where the save the configuration file. The extension of this file is “.launch”, which you can easily find in the workspace.

In your plugin code, you can then launch this file using the following:

launch_code.jpg

If you are wondering about “config/MyProject build.xml.launch”, this is the launch configuration generated above which I moved to the config directory. Also, the variable “proj” is defined as:

IProject proj = org.eclipse.core.resources.ResourcesPlugin.getWorkspace().getRoot().getProject(“MyProject”);

The code above will launch the ant script using eclipse mechanism and you can view the output in the eclipse console.