運行方式
- 通過ant 直接運行, 如:ant app (其中app是target的名字<target name="app>)
- java代碼運行,java代碼運行前准備ant-launcher.jar, ant.jar, apache-xml-xerces.jar
初始化project
public void init(String buildFile, String baseDir) throws Exception {
project= new Project();
project.init();
DefaultLogger consoleLogger = new DefaultLogger();
consoleLogger.setErrorPrintStream(System.err);
consoleLogger.setOutputPrintStream(System.out);
consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
project.addBuildListener(consoleLogger);
// Set the base directory. If none is given, "." is used.
if (baseDir == null) {
baseDir = new String(".");
}
project.setBasedir(baseDir);
if (buildFile == null) {
buildFile = new String(ConnectCvs.BUILDPATH);
}
project.setBaseDir(new File(ConnectCvs.BASEDIR));
ProjectHelper.configureProject(project, new File(buildFile));
}
運行相應的target
public void runTarget(String target) throws Exception {
if (project == null) {
throw new Exception("No target can be launched because the project has not been initialized. Please call the 'init' method first !");
}
if (target == null) {
target = project.getDefaultTarget();
}
// Run the target
project.executeTarget(target);
}
給build.xml里的參數賦值
- 通過ant: ant app -Dfilesource="aaa" (其中app是target的name, filesource是要被賦值的參數, aaa值,注:D和參數不能有空格)
2. java傳參: project.setProperty("filesource", StringUtil.join(syncFiles.toArray(), ","))
當傳入的值是有多個需要遍歷,先對其分割, 如:
使用foreach的前提
- 准備ant-contrib-1.0b3.jar
2. 配置
注:這個jar包只支持ant1.6版本以下的
