如何将文件复制到Gradle中的平面目录中

时间:2016-11-14 20:25:03

标签: gradle

我正在尝试编写Gradle任务,将特定文件从深树复制到平面文件夹中。

首先尝试:

task exportProperties << {
  copy {
    from "."
    into "c:/temp/properties"
    include "**/src/main/resources/i18n/*.properties"
  }
}

这会复制正确的文件,但不会使结构变平,所以我最终得到原始项目中的每个文件夹,其中大多数都是空的。

第二次尝试,根据我看到herehere

的答案
task exportProperties << {
  copy {
    from fileTree(".").files
    into "c:/temp/properties"
    include "**/src/main/resources/i18n/*.properties"
  }
}

这一次,它没有复制任何东西。

第三次尝试:

task exportProperties << {
  copy {
    from fileTree(".").files
    into "c:/temp/properties"
    include "*.properties"
  }
}

几乎可以工作,除非它只是复制每个* .properties文件,当我只想要特定路径中的文件时。

4 个答案:

答案 0 :(得分:12)

我以类似的方式解决了这个问题:

task exportProperties << {
  copy {
    into "c:/temp/properties"
    include "**/src/main/resources/i18n/*.properties"

    // Flatten the hierarchy by setting the path
    // of all files to their respective basename
    eachFile {
      path = name
    }

    // Flattening the hierarchy leaves empty directories,
    // do not copy those
    includeEmptyDirs = false
  }
}

答案 1 :(得分:7)

我让它像这样工作:

task exportProperties << {
  copy {
    from fileTree(".").include("**/src/main/resources/i18n/*.properties").files
    into "c:/temp/properties"
  }
}

答案 2 :(得分:4)

您可以通过向Copy.eachFile方法(包括目标文件路径)提供闭包来动态修改复制文件的多个方面:

copy {
    from 'source_dir'
    into 'dest_dir'
    eachFile { details ->
        details.setRelativePath new RelativePath(true, details.name)
    }
}

这会将所有文件直接复制到指定的目标目录中,但它也会复制没有文件的原始目录结构。

答案 3 :(得分:0)

我能够以与 Kip 类似的方式解决这个问题,但倒过来了:

distributions {
    main {
        // other distribution configurations here...
        contents {
            into('config') {
                exclude(['server.crt', 'spotbugs-exclusion-filters.xml'])
                from fileTree('src/main/resources').files
            }
        }
    }
}

以这种方式配置 CopySpec 时,空目录没有问题。