[英]Copy subdirectories if they contain a specific file in Ant
对Ant来说真的很新,我一直在努力弄清楚,但无法...
假设我有一个像这样的结构:
Root |-data |-dir1 |-include.xml |-subdir1 |-file1 |-file2 |-include.xml |-subdir2 |--dir2 |-file1 |-include.xml |--dir3 |--dir4 |-file1 |-include.xml |--dir5 |-build.xml |-other files
我想在根目录复制文件(这是非常简单的过滤)。 但是麻烦来了:我只想复制数据的子目录,只要它们包含一个文件,在这里名为include.xml。 这是复制后目标文件夹的外观
Root |-data |-dir1 |-include.xml |-subdir1 |-file1 |-file2 |-include.xml |--dir2 |-file1 |-include.xml |--dir4 |-file1 |-include.xml |-build.xml |-other files
如您所见,/ data / dir3,/ data / dir5和/ data / dir1 / subdir1没有被复制,因为它们不包含include.xml文件。
这可能很简单,但我找不到方法,该属性和可用项是根据我对全局的了解来设置的?
我不认为ant中存在预定义的属性,因为您的要求非常具体。
您可以使用ant-contrib中的 <foreach>
任务并编写一个执行复制的递归目标。
或者,您可以使用<script language="javascript">
在javascript中实现递归解决方案。 在这种情况下,您不需要其他库。
另一个解决方案可能是复制所有内容并删除不包含include.xml的目录。
您可以在此处找到一些示例。
由于@Oleg的指导,这是一个示例
<target name="copy">
<!-- Remove files if they are not neighbour of required ${data.checkfile} -->
<script language="javascript"> <![CDATA[
var getInclude = function(list) {
var o = {};
for (i=0; i<list.length; i++) {
var f = list[i];
if(f.indexOf(inc_file) > 0) {
var folder = f.split("/").slice(0,-1).join("/");;
o[folder] = f;
}
}
return o;
}
importClass(java.io.File);
// Access to Ant-Properties by their names
data_dir = project.getProperty("data.dir"); // The directory where you want to check for subdirectory including X
copy_dir = project.getProperty("copy.dir"); // The directory where you want to check for subdirectory including X
inc_file = project.getProperty("data.required"); // The file which says if a folder should be copie
// Create a <fileset dir="" includes=""/> to retrieve everything from this folder
fs = project.createDataType("fileset");
fs.setDir( new File(data_dir) );
fs.setIncludes("**");
ds = fs.getDirectoryScanner(project); // Get the files (array) of that fileset
files = ds.getIncludedFiles(); // Get only the files
//Create destination and sourceDir File instances
basedir = new File(".");
destination = new File(basedir, [copy_dir, data_dir].join("/"));
source = new File(basedir, data_dir);
//We create an object where key are folder containing said inc_file
exist = getInclude(files);
includes = [];
for (i=0; i<files.length; i++) {
filename = files[i];
folder = filename.split("/").slice(0,-1).join("/");
if(exist[folder]) {
f = new File(source, filename);
copy = project.createTask("copy");
copy.setTofile(new File(destination, filename));
copy.setFile(f);
copy.perform();
}
}
]]>
</script>
</target>
</project>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.