I always wanted to make use of the “Post compile ant file” from Eclipse. After hours of searching, trial and (mostly) error i finally got it working. The Ant-MailTask sends a mail from within Eclipse including attachment.
The downside is that i was unable to get this running with my default Ant installation (v.1.7.1) and had to install a newer version of Ant.
I dont want to bore you with details of my odyssey but show you how it gets done in more or less four steps.
First check the version of your current Ant installation by typing
ant -version
in your Terminal which should give you something like:
Apache Ant version 1.8.1 compiled on April 30 2010
If you have the version 1.8.1 installed already you can obviously skip the first step as well as step three.
Here we go:
Step 1.)
Download the latest Ant version (1.8.1 at the time of writing)
and put it wherever you like. As i’m on OS X i put it into /usr/local/apache-ant-1.8.1/
Step 2.)
Next step is to download and extract some additional JAR files. You’ll need the JavaMail API and the NetBeans Activation Framework.
Extract and copy these files into either ~/.ant/lib/
or into the lib
folder of your
Ant installation directory. (To save you some time, i’ve put these files into a zip at
the end of the article. There may changes or updates over time but for now they should do the job)
Step 3.)
The final step is to create or update the alias to the Ant binary. Open the Terminal again and use the following command.
sudo ln -s /usr/local/apache-ant-1.8.1/bin/ant /usr/bin/ant
(fit the paths to your needs if you’ve choosen a different directory)
If you got an error message, rename the current alias in /usr/bin
to e.g. ant-1.7.1
Now the alias should point to your new Ant installation and ant -version
should give you:
Apache Ant version 1.8.1 compiled on April 30 2010
.
If you want to be the previous Ant version your default again, rename your ant-1.7.1
to ant
or create a new alias using the following command in the terminal.
sudo ln -s /usr/share/ant/bin/ant /usr/bin/ant
Step 4.)
The last step is to make the downloaded JAR available in the Eclipse environment. All you need to do is go to:
Preferences > Ant > Runtime > Classpath > Add External JARs
and select the downloaded JAR files.
Thats pretty much all you have to do for preparation.
Now let’s see how an ant-target which makes usage of the MailTask could look like.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
|
I think it is pretty self-explanatory. This would send an e-mail with
the subject:
“Test Message sent via Ant MailTask” from “me@myprovider.com” to “you@myprovider.com”
with “Hello World, i was sent by the Ant MailTask” as message and all zip-files from
the basedir
as attachment.
From: "me@myprovider.com"
To: "you@myprovider.com"
Subject: "Test Message sent via Ant MailTask"
Body: "Hello World, i was sent by the Ant MailTask"
So far so good.
Put this task into a build.xml
(setup a properties
file to make it more generic)
and provide the appropriate credentials for your mail provider. Save it in your project
directory or update your existing build file and run the following command in the terminal:
$ cd path/to/project
$ ant send.mail
If everything worked as expected the output should look like this:
send.mail:
[mail] Sending email: Hello World, i was sent by the Ant MailTask
[mail] Sent email with 1 attachments
[echo] Mail successfully sent at 11/28/2010 04:17 PM
BUILD SUCCESSFUL
Total time: 1 second
Now insert the path to your build.xml
in the Ant Task
tab in your launch-configuration
dialog and you’re done. :)
I use it now (in a more fine-grained setup) e.g. in the launch-configuration for my ‘release-build’ to notify a customer about new versions, updates and so on.
Hopefully you’ve found some useful informations in this post. Thanks for reading and feel free to leave a comment or share it if you like.
cheers, david