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

define and execute a composed ant task in java

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

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

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

我回复我的问题...

在阅读了 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();