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.

Advertisements

All Things Bright And Beautiful

Today is the 4th anniversary of my mother’s passing away. Ever since that time, every little thing that reminds me of her is very valuable. Like this letter which I’m holding right now. It is actually a love letter by my mother to my father. In the last section of the letter is a poem which my mother learned when she was 10 years old. She committed it to memory since that time and in her letter she dedicates it to my father. I did a google search for this poem and it turns out that it’s a song.
Here is the poem:

All Things Bright And Beautiful

All things bright and beautiful,
All creatures great and small,
All things wise and wonderful:
The Lord God made them all.

Each little flower that opens,
Each little bird that sings,
He made their glowing colors,
He made their tiny wings.

The rich man in his castle,
The poor man at his gate,
He made them, high or lowly,
And ordered their estate.

The purple headed mountains,
The river running by,
The sunset and the morning
That brightens up the sky.

The cold wind in the winter,
The pleasant summer sun,
The ripe fruits in the garden,
He made them every one.

The tall trees in the greenwood,
The meadows where we play,
The rushes by the water,
To gather every day.

He gave us eyes to see them,
And lips that we might tell
How great is God Almighty,
Who has made all things well.

And here is the song:

Congratulations to the new Sun Certified Java Programmers

I am happy to announce that two of my mentees have passed the SCJP (Sun Certified Java Programmer) examination. Ann Mediana and Enjelica Pastoral, in the order shown below, passed the SCJP exam recently. In fact, Ann Mediana passed the exam today. So she is going to treat us to Mannang Mega Mall!

Ann has been very active in the Dining Philosophers group, while Jel is the founder of the Dancing Philosophers. They will be a good addition to the pool of java programmers around the world.
dsc_4363.JPG