在打包android项目时,用到了ant,于是简单的看了下,用java调用ant脚本,或ant脚本调用java代码
java调用ant脚本:
package com.hilary.zc;
import java.io.File;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.ProjectHelper;
/**
* @Author: hilary
* @Date: 2011-7-18
**/
public class ZC {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println(System.currentTimeMillis());
File file = new File("createfile.xml"); //文件名任意,只要能找到就可以
Project project = new Project();
try {
project.fireBuildStarted();
project.init();
ProjectHelper projectHelper = ProjectHelper.getProjectHelper();
projectHelper.parse(project, file);
// project.executeTarget(project.getDefaultTarget()); //调用默认的target标签
project.executeTarget("delete"); //调用删除target标签
project.fireBuildFinished(null);
} catch (Exception e) {
System.out.println("#############");
e.printStackTrace();
}
System.out.println(System.currentTimeMillis());
}
}
xml:
<?xml version="1.0" encoding="utf-8"?> <project name="antTest" default="init" basedir="."> <property name="filePath" value="newFile" /> <!-- ================================= target: createFile ================================= --> <target name="createFile" depends="" description="description"> <mkdir dir="${filePath}"/> </target> <target name="init"> <mkdir dir="${filePath}"/> </target> <target name="delete"> <delete dir="${filePath}"></delete> </target> </project>
在执行上面代码时,需要引入两个ant jar包:ant.jar 和ant_launcher.jar。
如果不引用ant_launcher.jar,在初始化init()时会报错:
java.lang.ClassNotFoundException: org.apache.tools.ant.launch.AntMain