【问题标题】:Copy subdirectories if they contain a specific file in Ant如果子目录包含 Ant 中的特定文件,则复制子目录
【发布时间】:2015-06-03 10:28:37
【问题描述】:

对 Ant 来说真的很陌生,我一直在尝试解决这个问题,但无法...

假设我有这样的结构:

根 |-数据 |-目录1 |-include.xml |-子目录1 |-文件1 |-文件2 |-include.xml |-子目录2 |--目录2 |-文件1 |-include.xml |--目录3 |--目录4 |-文件1 |-include.xml |--dir5 |-build.xml |-其他文件

我想复制根目录下的文件(这是非常简单的过滤)。但是麻烦来了:我只想复制包含文件的数据子目录,这里命名为include.xml。这是复制后目标文件夹的样子

根 |-数据 |-目录1 |-include.xml |-子目录1 |-文件1 |-文件2 |-include.xml |--目录2 |-文件1 |-include.xml |--目录4 |-文件1 |-include.xml |-build.xml |-其他文件

如您所见,/data/dir3、/data/dir5 和 /data/dir1/subdir1 没有被复制,因为它们不包含 include.xml 文件。

这可能很简单,但我找不到方法,属性和可用是根据我对全局的理解设置的?

【问题讨论】:

    标签: xml recursion ant conditional-statements


    【解决方案1】:

    我认为 ant 中不存在预定义属性,因为您的要求非常具体。

    您可以使用来自ant-contrib<foreach> 任务并编写一个执行复制的递归目标。

    或者您可以使用 <script language="javascript"> 在 javascript 中实现递归解决方案。在这种情况下,您不需要额外的库。

    另一种解决方案可能是复制所有内容并删除不包含 include.xml 的目录。

    你可以找到一些例子here

    【讨论】:

    • 谢谢!我会尝试用你对脚本的想法来解决这个问题(不知道这种可能性!)。我会尽快将答案标记为正确答案:)
    • 感谢您的指导!在下面找到一个例子:stackoverflow.com/a/30621334/2390493
    • 不客气!是的,有时在 javascript 中做某事比在 ant 中更容易。最终的解决方案也更易于维护。
    【解决方案2】:

    感谢@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>
    

    【讨论】:

      猜你喜欢
      • 2019-10-21
      • 2017-04-29
      • 1970-01-01
      • 1970-01-01
      • 2023-01-04
      • 1970-01-01
      • 1970-01-01
      • 2019-11-01
      • 1970-01-01
      相关资源
      最近更新 更多