编写自己的任务应该是一项简单的任务。根据文档,您只需要扩展 org.apache.tools.ant.Task。该站点提供了一个简单的类示例:
package com.mydomain;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
public class MyVeryOwnTask extends Task {
private String msg;
// The method executing the task
public void execute() throws BuildException {
System.out.println(msg);
}
// The setter for the "message" attribute
public void setMessage(String msg) {
this.msg = msg;
}
}
为了使用 build.xml 使用它:
<?xml version="1.0"?>
<project name="OwnTaskExample" default="main" basedir=".">
<taskdef name="mytask" classname="com.mydomain.MyVeryOwnTask"/>
<target name="main">
<mytask message="Hello World! MyVeryOwnTask works!"/>
</target>
</project>
我的问题是,我应该把 MyVeryOwnTask.java 文件放在哪里,它应该是 .jar 文件吗?它应该以某种方式与 build.xml 文件相关吗?com.mydomain.MyVeryOwnTask 是不是像eclipse中的java项目一样代表文件结构?
我的 ant 目录是 C:\ant。我设置了所有环境变量。
谢谢。