Ant 简明教程

Ant - Executing Java code

您可以使用 Ant 来执行 Java 代码。在以下示例中,Java 类接收一个参数(管理员的电子邮件地址)并发送电子邮件。

You can use Ant to execute the Java code. In the following example, the java class takes in an argument (administrator’s email address) and send out an email.

public class NotifyAdministrator {
   public static void main(String[] args) {
      String email = args[0];
      notifyAdministratorviaEmail(email);
      System.out.println("Administrator "+email+" has been notified");
   }
   public static void notifyAdministratorviaEmail(String email {
      //......
   }
}

这是一个简单的构建,它执行该 Java 类。

Here is a simple build that executes this java class.

<?xml version="1.0"?>
<project name="sample" basedir="." default="notify">
   <target name="notify">
      <java fork="true" failonerror="yes" classname="NotifyAdministrator">
         <arg line="admin@test.com"/>
      </java>
   </target>
</project>

当构建执行时,它将生成以下结果:

When the build is executed, it produces the following outcome −

C:\>ant
Buildfile: C:\build.xml

notify: [java] Administrator admin@test.com has been notified

BUILD SUCCESSFUL
Total time: 1 second

在此示例中,Java 代码执行简单的操作,即发送电子邮件。我们可以使用内置于 Ant 任务中的任务来执行此操作。

In this example, the java code does a simple thing which is, to send an email. We could have used the built in the Ant task to do that.

不过,现在您已了解想法,便可扩展您的构建文件,以便调用执行复杂操作的 Java 代码。比如:加密您的源代码。

However, now that you have got the idea, you can extend your build file to call the java code that performs complicated things. For example: encrypts your source code.