标签:concurrentmodificati gradle
最近在写一个gradle插件过程中,为Task定义文件复制方法时,发现文件并没有被复制,本来是一个很简单的操作,却出现了如此不解的现象。因编写gradle插件调试不易,所以花了比较多的时间才发现问题,所以谨此记录。下面一个精简版的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; } }
标签:concurrentmodificati gradle
原文地址:http://blog.csdn.net/xtayfjpk/article/details/43114945