8

I've been tasked with creating automated notifications for our Ant build scripts. This is so that when someone pushes out a deployment, our team can automatically receive an email about it.

The obvious choice was to use Ant's mail task, which comes predefined with Ant:

<target name="notify" description="notify team">
    <mail subject="latest deployment">
        <from address="me@gmail.com" />
        <to address="jimmy@yahoo.com" />
        <message>A new build has been pushed out to prod</message>
    </mail>
</target>

However this results in the following runtime exception:

java.lang.ClassNotFoundException: javax.mail.internet.MimeMessage

This is because Ant's mail task depends on JavaMail and JavaBeans Activation Framework, libraries which are apparently not included with its distribution. Why Ant would define a task that depended on a library but not include that library is unclear to me.

This issue has already been discussed on this post: ant mail task using mail and activation jar in external location. Based on the answers there seem to be two solutions.

The first is to manually put these library dependencies on the ant classpath. This can be done using the -lib command line argument, or in eclipse one can use Window > Preferences > Ant > Runtime > Global Entries, and then add mail.jar and activation.jar (I'm pretty sure this amounts to the same thing as -lib, correct me if I'm wrong). But this solution is undesirable for our team because it would mean every one of us would have to manually carry out these steps. I'm looking for a way to simply commit my notification code, and it should work on another eclipse install after svn update.

The other solution in the linked post mentions a way to do the above programmatically, by calling Ant from itself:

<exec executable="ant">
    <arg value="-lib"/>
    <arg value="PATH_TO_MY_LIB"/>
    <arg value="target"/>
</exec>

The problem with this is that the Ant command line tool is apparently only included with the full install, not the eclipse distribution. So again there's no way to make it work without some manual action by anyone wanting to use the mail task.

Is there any way I can automate this without adding another annoying step to project setup? I really don't understand why this is so hard to achieve - it seems like if the mail task wasn't predefined by Ant this would be easier. Hopefully I'm missing something.

Community
  • 1
  • 1
Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
  • Have you considered using a CI for this kind of tasks? They comes with mails and much improved reporting. My votes is for http://jenkins-ci.org/ – Jayan Feb 08 '12 at 09:25

5 Answers5

5

With the example from this mail thread I got a working solution that sends email when the tests fail.

You need to download ant-classloadertask.jar, and mail.jar from javamail and place them in test.libs.dir.

<property name="test.libs.dir" location="${basedir}/lib/unit-test"/>
<property name="test.results.dir" location="${basedir}/test-results/"/>

<path id="project.classpath.tests">
    <pathelement location="${build}"/> 
    <path refid="project.lib.path"/>
</path>

<target name="unit-tests" depends="">
    <mkdir dir="${test.results.dir}"/>
    <junit fork="false" showoutput="yes" includeantruntime="false"
        errorproperty="test.error" failureproperty="test.error" 
        haltonerror="false" haltonfailure="false">
        <classpath refid="project.classpath.tests"/>
        <formatter type="plain" usefile="true" />
        <batchtest fork="no" todir="${test.results.dir}">
            <fileset dir="${build}/test/">
                <include name="package/dir/path/to/tests/TestFile.java"/>
            </fileset>
        </batchtest>
    </junit>
    <antcall target="sendMail"/> 
</target>

<path id="mail.path">
    <pathelement location="${test.libs.dir}/mail.jar"/>
</path>

<!-- http://enitsys.sourceforge.net/ant-classloadertask/ -->
<taskdef name="classloadertask"
    classname="org.apache.tools.ant.taskdefs.ClassloaderTask" 
    classpath="${test.libs.dir}/ant-classloadertask.jar"/>
    <classloadertask classpathRef="mail.path" loader="thread"/> 

<target name="sendMail" if="test.error">
    <mail mailhost="smtp.gmail.com"
        mailport="587"
        user=""
        password=""
        ssl="yes"
        failonerror="true"
        from=""
        tolist=""
        subject="Unit tests have failed"/>
</target>
bro
  • 51
  • 1
  • 2
5

Mail is one of the optional tasks in ANT. To install these additional libraries ANT 1.7 added a fetch.xml script, invoked as follows:

ant -f $ANT_HOME/fetch.xml -Ddest=user -Dm2.url=http://repo1.maven.org/maven2 

On windows you could try:

ant -f %ANT_HOME%/fetch.xml -Ddest=user -Dm2.url=http://repo1.maven.org/maven2 

For a full explanation, see the ANT Manual documentation.


Update

The following ANT file has an install-jars target that can be used to install the missing ANT jars used by the mail task.

<project name="demo" default="notify">

    <target name="install-jars" description="Install ANT optional jars">
        <mkdir dir="${user.home}/.ant/lib"/>
        <get dest="${user.home}/.ant/lib/mail.jar"       src="http://search.maven.org/remotecontent?filepath=javax/mail/mail/1.4.4/mail-1.4.4.jar"/>
        <get dest="${user.home}/.ant/lib/activation.jar" src="http://search.maven.org/remotecontent?filepath=javax/activation/activation/1.1/activation-1.1.jar"/>
    </target>

    <target name="notify" description="notify team">
        <mail subject="latest deployment">
            <from address="me@gmail.com" />
            <to address="jimmy@yahoo.com" />
            <message>A new build has been pushed out to prod</message>
        </mail>
    </target>

</project>

I discovered that the ANT mail task task loads it's dependent classes from the ANT system classpath. This means the build has to be run in two steps:

$ ant install-jars
$ ant

Most ANT tasks allow you to specify a classpath reference. The Mail task does not. Trying to research this issue lead me to the following mailing list thread:

The solution is convoluted and not worth the effort. I'd recommend living with the inconvenience... (The install-jars target only needs to be run once)

Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185
  • And how might this be done programmatically through the build file, so that after doing an svn checkout of the changes (and jars) in eclipse it would work without any manual setup? – Paul Bellora Feb 08 '12 at 01:44
  • After seeing how insurmountable it is to truly automate this, my team and I decided to give up and swallow the pill of manually setting up Ant per eclipse install. Your install target came close, +1 for that and the link - it's just unfortunate Ant needs to restart but it seems there's truly no way around that without wading into the classloader mess. – Paul Bellora Feb 10 '12 at 00:42
  • @PaulBellora just an additional idea, you could check if the files exists and only run install-jars, if they do not. [check if file exists](http://stackoverflow.com/questions/520546/ant-task-to-check-if-a-file-exists). This will integrate those steps transparently in your workflow. – oers Feb 10 '12 at 10:38
  • 1
    @oers I thought of doing a check on the existence of the classes (rather than files), but it doesn't get away from having to run ANT twice.... That was why I gave up :-( The root cause here is an old ANT task that doesn't support a classpathref attribute. Hacking the ANT classloader seems so clunky by comparison, and that option is also denied to us (using standard ANT) – Mark O'Connor Feb 10 '12 at 13:06
1

With Ant 1.8.2 and ant-classloadertask.jar solution mentioned above (by bro) I needed to change loader attribute of classloadertask from:

loader="thread"

into:

loader="project"

otherwise classes in activation.jar and mail.jar were not found by the classloader.

Community
  • 1
  • 1
libor
  • 131
  • 1
  • 4
1

With the taskdef command, you can dynamically load new tasks and specify a classpath from where they are loaded. I use this successfully for loading new tasks from an external library which is not on the ant class path. Whether it also works when the task already exists, I don't know.

I would probably start with trying to re-define the mail task with a new name:

<taskdef name="mymail" classname="class.of.mail.task" classpath="mail.jar;activation.jar">

Perhaps you need to include the original ant class path in the class path here so that the class of the mail task can be found.

If this doesn't work, perhaps you can take the source code of the mail task, rename the class, bundle it together with the classes from mail.jar and activation.jar into your own jar and load this task from the JAR. This should work just as it works with any task not shipped with ant.

Philipp Wendler
  • 11,184
  • 7
  • 52
  • 87
  • @oers Could you please give some more details why this doesn't work? – Philipp Wendler Feb 08 '12 at 09:01
  • Sry I may have been to hasty. I misread your answer as something that I tried. This approach could work. – oers Feb 08 '12 at 09:18
  • 2
    [This seems to be possible](http://ant.1045680.n5.nabble.com/Mail-task-with-mail-jar-amp-activation-jar-out-of-lib-folder-td3347501.html), but requires an additonal ant task due to class loading issues. – oers Feb 08 '12 at 09:43
  • 1
    I tried this, doesn't work. Still get the ClassNotFoundException because the mail task is loading it's dependent classes from the system classpath. Looks like @oers discovered the same mail thread I did! – Mark O'Connor Feb 08 '12 at 22:06
1

You can put these jars as external jars to the classpath of the ant run config (this is my ftp example):

enter image description here

and save this run config to the project(Tab: Common--> Shared File, I usually take something like resources/eclipse for that) and commit it to svn.

  1. it will be available for all developers who check this project out from svn
  2. you have to deliver the jars in that project, too. So that they will always be available
  3. Everybody can run it easily from the external tools menu

Of course you could write your own mail task, to take care of that.

oers
  • 18,436
  • 13
  • 66
  • 75