如何以编程方式为邮件任务添加Ant库依赖项 [英] How to programmatically add Ant library dependencies for mail task

查看:126
本文介绍了如何以编程方式为邮件任务添加Ant库依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是为我们的Ant构建脚本创建自动通知。这是因为当有人推出部署时,我们的团队可以自动收到一封电子邮件。



明显的选择是使用 Ant的邮件任务,它与Ant预先定义:

 < target name =notifydescription =notify team> 
< mail subject =最新部署>
< from address =me@gmail.com/>
< to address =jimmy@yahoo.com/>
< message>一个新的构建被推出来生成< / message>
< / mail>
< / target>

但是,这会导致以下运行时异常:

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

这是因为Ant的邮件任务取决于 JavaMail JavaBeans Activation Framework ,这些库明显不包括在其中。为什么蚂蚁会根据图书馆定义一个任务,但不包括该图书馆对我来说不清楚。



这个问题已经在这篇文章中讨论过了:邮件任务在外部使用邮件和激活jar 。基于答案,似乎有两个解决方案。



第一个是将这些库依赖关系手动放在ant类路径上。这可以使用 -lib 命令行参数来完成,或者在eclipse中可以使用 Window>首选项>蚂蚁>运行时>全局条目,然后添加 mail.jar activation.jar (我是非常确定这一点与 -lib 相同,如果我错了就纠正我的意思。但是这个解决方案对我们的团队是不利的,因为这意味着我们每个人都必须手动执行这些步骤。我正在寻找一种方法来简单地提交我的通知代码,它应该在svn更新后的另一个eclipse安装上工作。



链接的其他解决方案提到一个通过编程方式,通过调用Ant自己来执行上述方法:

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

这个问题是,Ant命令行工具显然只包含在完整的安装中,而不是日食分布。所以再也没有办法使任何人想要使用邮件任务的手动操作。



有没有任何方法我可以自动化,而不添加另一个烦人的步骤项目设置?我真的不明白为什么这么难以实现 - 似乎如果邮件任务不是由预定义的Ant这将会更容易。希望我错过了一些东西。

解决方案

邮件是ANT中的可选任务之一。安装这些附加库ANT 1.7添加了一个fetch.xml脚本,如下所示调用:

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

在Windows上可以尝试:

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

有关完整说明,请参阅 ANT手册文档






更新



以下ANT文件有一个 install-jars 目标,可用于安装丢失的邮件任务使用的ANT jar。

 < project name =demodefault =notify> 

< target name =install-jarsdescription =安装ANT可选的jar>
< mkdir dir =$ {user.home} /。ant / lib/>
< get dest =$ {user.home} /。ant / lib / mail.jarsrc =http://search.maven.org/remotecontent?filepath=javax/mail/mail/1.4 0.4 /邮件1.4.4.jar/>
< get dest =$ {user.home} /。ant / lib / activation.jarsrc =http://search.maven.org/remotecontent?filepath=javax/activation/activation/1.1 /activation-1.1.jar\"/>
< / target>

< target name =notifydescription =notify team>
< mail subject =最新部署>
< from address =me@gmail.com/>
< to address =jimmy@yahoo.com/>
< message>一个新的构建被推出来生成< / message>
< / mail>
< / target>

< / project>

我发现ANT邮件任务任务从ANT系统类路径加载它的依赖类。这意味着构建必须分两步运行:

  $ ant install-jars 
$ ant

大多数ANT任务允许您指定类路径引用。邮件任务没有。尝试研究这个问题导致以下邮件列表主题:





解决方案是复杂的,不值得的努力。我建议生活带来不便...(安装目标只需要运行一次)


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.

解决方案

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)

这篇关于如何以编程方式为邮件任务添加Ant库依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆