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

    1. public class NotifyAdministrator {
    2.  
    3. public static void main(String[] args) {
    4. String email = args[0];
    5. notifyAdministratorviaEmail(email);
    6. System.out.println("Administrator "+email+" has been notified");
    7. }
    8.  
    9. public static void notifyAdministratorviaEmail(String email) {
    10. //......
    11. }
    12. }

    这是执行此Java类的简单构建。

    1. <?xml version = "1.0"?>
    2. <project name = "sample" basedir = "." default = "notify">
    3. <target name = "notify">
    4.  
    5. <java fork = "true" failonerror = "yes" classname = "NotifyAdministrator">
    6. <arg line = "admin@test.com"/>
    7. </java>
    8. </target>
    9. </project>

    执行构建后,将产生以下结果

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

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

    4. BUILD SUCCESSFUL

    5. Total time: 1 second

    在此示例中,Java代码做了一件简单的事情-发送电子邮件。我们本可以使用内置的Ant任务来执行此操作。但是,现在您已经有了主意,可以扩展构建文件以调用执行复杂操作的Java代码,例如: encrypts your source code