下面一个精简版的Task类:
import java.io.File;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import org.apache.commons.io.FileUtils;
import org.gradle.api.tasks.TaskAction;
public class MyTask {
private List<Action> actions = new CopyOnWriteArrayList<>();
public void copy(final String source, final String dest) throws Exception {
this.actions.add(new Action() {
@Override
public void execute() throws Exception {
FileUtils.copyFile(new File(source), new File(dest));
}
});
}
public void copyDependencies() {
this.actions.add(new Action() {
@Override
public void execute() throws Exception {
File dir = new File("H:\\sourceDir");//测试代码源目录
for(File file : dir.listFiles()) {
copy(file.getAbsolutePath(), "H:\\destDir\\"+file.getName());
}
}
});
}
@TaskAction
public void execute() throws Exception {
for(Action action : this.actions) {
action.execute();
}
}
public static interface Action {
void execute() throws Exception;
}
}import java.io.File;
import java.util.List;
import java.util.ArrayList;
import org.apache.commons.io.FileUtils;
import org.gradle.api.tasks.TaskAction;
public class MyTask {
private List<Action> actions = new ArrayList<>();
public void copy(final String source, final String dest) throws Exception {
this.actions.add(new Action() {
@Override
public void execute() throws Exception {
FileUtils.copyFile(new File(source), new File(dest));
}
});
}
public void copyDependencies() {
this.actions.add(new Action() {
@Override
public void execute() throws Exception {
File dir = new File("H:\\sourceDir");
for(File file : dir.listFiles()) {
//不再调用copy方法,而是直接进行文件复制,这样就避免了仅向actions列表中添加元素
//列表类型也可以使用ArrayList了
FileUtils.copyFile(file, new File("H:\\destDir\\"+file.getName()));
}
}
});
}
@TaskAction
public void execute() throws Exception {
for(Action action : this.actions) {
action.execute();
}
}
public static interface Action {
void execute() throws Exception;
}
}原文:http://blog.csdn.net/xtayfjpk/article/details/43114945