在java中定义并执行一个组合的ant任务

时间:2015-03-27 14:49:04

标签: java ant

可以将组合的ant任务定义并执行到java应用程序中吗? 我需要做的是这样:

<copy todir="dirDest">
  <fileset dir="sourceDir">
    <exclude name="exFile.ext" />
    <exclude name="dirEx" />
  </fileset>
</copy>

该示例解释了我需要在我的java应用程序中执行的操作,或者更确切地说,将目录内容复制到另一个目录中,但有一些例外。 单个任务的执行非常simple,但我无法找到一个教程来做我的例子。

1 个答案:

答案 0 :(得分:0)

我回答我的问题......

在阅读了apache ant class code documents后,我发现了如何构建任务:

import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Copy;
import org.apache.tools.ant.types.FileSet;

//Create the new copy class
Copy copier = new Copy();
//Set the target directory wrapped into File object
copier.setTodir(new File("my/target/directory"));
//The project's definition is requested for the execution of Copy
copier.setProject(new Project());

//Before run its request to define the FileSet object
FileSet fs = new FileSet();

//Now setup the base directory to copy
fs.setDir(new File("my/source/directory"));
//If its require, define the inner file or directory to exclude from the copy action
fs.setExcludes("fileExcluded");
fs.setExcludes("directory/to/exclude");

//Link the FileSet to copy
copier.addFileset(fs);
//And go!
copier.execute();
相关问题