简体   繁体   中英

ant mail task using mail and activation jar in external location

I have following ant mail task -

<target name="sendtestreport" > 
    <mail mailhost="smtp.com" mailport="1025" subject="Test build" >
        <from address="test@gmail.com" />           
        <replyto address="test@gmail.com" />
        <to address="test@gmail.com" />
        <message>test message</message>
        <attachments>
        </attachments>
    </mail>
</target>

And I have both activation and mail jar available in my project "lib" folder. I want to be able to send mail with out having to keep these jars in ant download location. Is it possible to let ant know about my "lib" folder to see javaxmail and activation jars. Some thing like If I could specify class path from with in this target.

Right now when I execute this target I always encounter exception -

BUILD FAILED
java.lang.ClassNotFound
Exception: javax.mail.internet.MimeMessage

Unforunately you can't. You have to put Library Dependencies into the ant classpath somehow.

This could be the ANTH_HOME/lib dir or a change to the command line arguments.

Ant has a command-line argument to specify a lib dir:

-lib <path> specifies a path to search for jars and classes

You could also call ant from ant itself with that info, which may be a bit ugly:

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

If you execute this task from eclipse, you can add the libs to the run definition of the task and share this run definition with other developers.

See this answer to a similar question for a solution of library dependencies in ant.

In short, put all your ant addon libs, eg mail.jar,activation.jar, commons in a special folder and make it available for all your ant scripts via ANT_ARGS .
Being forced to use the exec task in ant to resolve a library dependency seems VERY strange.

In Eclipse common practice is to create a module in your repository (cvs,subversion,git). Then every team member has that module in his workspace and simply uses Window > Preferences > Ant > Runtime > Global Entries to make those dependencies available for ant.

There is a nice approach which is already answered here . It solves this problem with the Ant Tasks Classloader :

<taskdef resource="net/jtools/classloadertask/antlib.xml" 
    classpath="ant-classloadertask.jar"/>
<!-- now you can load activation.jar and mail.jar into the project's classpath -->
<classloader loader="project">
    <classpath>
        <pathelement path="lib/activation.jar"/>
        <pathelement path="lib/mail.jar"/>
    </classpath>
</classloader>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM