Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically add Ant library dependencies for mail task

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="[email protected]" />
        <to address="[email protected]" />
        <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.

like image 218
Paul Bellora Avatar asked Feb 07 '12 15:02

Paul Bellora


3 Answers

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="[email protected]" />
            <to address="[email protected]" />
            <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:

  • http://ant.1045680.n5.nabble.com/Mail-task-with-mail-jar-amp-activation-jar-out-of-lib-folder-td3347501.html

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)

like image 199
Mark O'Connor Avatar answered Oct 24 '22 07:10

Mark O'Connor


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>
like image 28
bro Avatar answered Oct 24 '22 07:10

bro


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.

like image 1
Philipp Wendler Avatar answered Oct 24 '22 09:10

Philipp Wendler