简体   繁体   English

运行ant-target以通过Java发送邮件

[英]Running ant-target to send a mail via java

I´ma pretty new to ant and I´m trying to send an email within an ant-target which is called by Java. 我是ant的新手,我想在Java称为ant的目标内发送电子邮件。 I´m using the Netbeans IDE. 我正在使用Netbeans IDE。

Ant: 蚂蚁:

<project name="AntTargets" default="main" basedir=".">
<description>Builds, tests, and runs the project AntTargets.</description>
<import file="nbproject/build-impl.xml"/>

<target name="run" depends="jar">
    <java classpathref="AntTargets.classpath" fork="true" classname="Client" />
</target>

<target name="main">
    <echo message="******************[MAIN]******************"/>
    <property file="nbproject/build.properties"/>
    <property name="to" value="${builder}" />
    <property name="from" value="${builder}" />
    <property name="server" value="${server}" />
    <property name="port" value="${port}" />

    <mail mailhost="${server}" mailport="${port}" subject="Test build">
        <from address="${from}"/>
        <replyto address="${to}"/>
        <to address="test@domain.de"/>
        <message>The build has been completed</message>
    </mail>

    <echo message="Email to ${to} from ${from} has been sent using the server ${server} on port ${port}."/>
</target>

</project>

Java: Java:

import java.io.File;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.ProjectHelper;

public class AntTargets {

public static void main(String[] args) {
    //get new file
    File buildFile = new File("build.xml");

    //create new project
    Project p = new Project();
    p.setUserProperty("ant.file", buildFile.getAbsolutePath());
    p.init();

    //initialize helper
    ProjectHelper helper = ProjectHelper.getProjectHelper();
    p.addReference("ant.projectHelper", helper);

    //parse and execute
    helper.parse(p, buildFile);
    p.executeTarget(p.getDefaultTarget());
}   
}

When I execute the ant part directly, the properties file is found, read and works overall but the mail-part gives me this error: 当我直接执行ant部分时,找到了属性文件,可以读取并整体运行,但是mail部分给了我这个错误:

Failed to send email: javax.mail.internet.MimeMessage
build.xml:16: 
java.lang.ClassNotFoundException: javax.mail.internet.MimeMessage

When I call it with java I get: 当我用java调用它时,我得到:

Reference AntTargets.classpath not found.
BUILD FAILED (total time: 0 seconds)

I would greatly appreciate any help and tip overall. 我将不胜感激任何帮助和总体小费。

The issue is Ant doesn't know what AntTargets.classpath is. 问题是Ant不知道AntTargets.classpath是什么。

You must define a classpath variable and give it id="AntTargets.classpath . In this path variable you should define the physical locations of the libraries you use. (You should look to creating path variables to reduce redundancies in code, particularly when you want to use the same path in different ant targets.) 您必须定义一个classpath变量,并将其赋予id="AntTargets.classpath 。在此路径变量中,您应该定义所用库的物理位置。(您应该创建路径变量以减少代码中的冗余,尤其是当您需要在不同的蚂蚁目标中使用相同的路径。)

Here's an example: Let's say that your project requires the Gson library at runtime. 这是一个示例:假设您的项目在运行时需要Gson库。

  1. First, create a path for that library. 首先,为该库创建路径。 Let's assume you have your libraries in a lib directory. 假设您的库位于lib目录中。

    <path id="library.gson-2.8.0.classpath"> <pathelement location="${basedir}/lib/gson-2.8.0.jar"/> </path>

    Do the same for other dependencies as well. 对其他依赖项也执行相同的操作。

  2. Next, create a path that will contain all these dependencies, so you can refer to all the dependencies as one variable. 接下来,创建一个包含所有这些依赖项的路径,以便您可以将所有依赖项称为一个变量。 This will be your AntTargets.classpath 这将是您的AntTargets.classpath

    <path id="AntTargets.classpath"> <path refid="library.gson-2.8.0.classpath"/> ...... <!-- Add paths for your other dependencies by refid here--> </path>

  3. Now, when you use AntTargets.classpath Ant will know what that is and use it to run your jar. 现在,当您使用AntTargets.classpath Ant将知道它是什么,并使用它来运行jar。

I notice that you use this in the run target, so if you're trying to run the Client class from your own jar, include that in your classpath as well. 我注意到您在run目标中使用了它,因此,如果您尝试从自己的jar中运行Client类,则也应在类路径中包含该类。

Also keep in mind that "Client" may not be enough information--you must provide the fully qualifying name of the class, including package names, eg something like "com.package.Client". 另外请记住,“客户”可能是不够的信息-您必须提供类的完全合格名称,包括包名称,例如“ com.package.Client”之类的名称。

EDIT: 编辑:

I just noticed the exception you're getting from running directly with Ant. 我只是注意到您直接从Ant运行获得的异常。 You need to include the javax.mail library in this path variable we create. 您需要在我们创建的此路径变量中包括javax.mail库。 That should resolve that issue. 那应该解决那个问题。

EDIT 2: 编辑2:

I seem to have misunderstood. 我似乎误会了。 I thought that your ant task was trying to call your Java program which would send the mail, but it couldn't since it didn't know where the javax.mail.jar is, and so I instructed you to add it to the classpath. 我以为您的ant任务正在尝试调用您的Java程序来发送邮件,但由于它不知道javax.mail.jar的位置,所以无法执行此操作,所以我指示您将其添加到类路径中。

But what you want to do is: 但是您想要做的是:

  1. Use the Ant mail task to send a mail. 使用Ant邮件任务发送邮件。
  2. And in the Java you want to read the build file and execute the target that sends the mail. 在Java中,您想读取构建文件并执行发送邮件的目标。

Here's some information on that: mail is an Ant Task that requires the javax.mail library, if you want to use certain arguments. 这方面的一些信息:如果要使用某些参数, mail是一个Ant任务,它需要javax.mail库。 Looking at a few questions like this and this , I see that you should put ${ANT_HOME}/lib, ie where your ant.jar is, so that Ant's classloader can pick it up and use it. 看着像几个问题这个这个 ,我看你应该把$ {ANT_HOME} / lib目录,即在您的ant.jar是,让Ant的类加载器可以把它捡起来,并使用它。 In that case, if my assumption about what you want is correct, then you should be able to get rid of AntTargets.classpath , because really, your build file doesn't need to know how you call any of it's targets. 在那种情况下,如果我对所需目标的假设是正确的,那么您应该可以摆脱AntTargets.classpath ,因为实际上,您的构建文件不需要知道如何调用任何目标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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