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

Kip*_*Kip 8 gradle

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

第一次尝试:

task exportProperties << {
  copy {
    from "."
    into "c:/temp/properties"
    include "**/src/main/resources/i18n/*.properties"
  }
}
Run Code Online (Sandbox Code Playgroud)

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

第二次尝试,根据我在这里这里看到的答案:

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

这次,它不会复制任何东西.

第三次尝试:

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

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

Jul*_*fin 19

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

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
  }
}
Run Code Online (Sandbox Code Playgroud)


Kip*_*Kip 7

我让它像这样工作:

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


And*_*LED 6

通过将闭包输入Copy.eachFile方法(包括目标文件路径),可以动态修改复制文件的许多方面:

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

这会将所有文件直接复制到指定的目标目录中,尽管它也会复制原始目录结构而不包含文件。

  • 使用`includeEmptyDirs = false`忽略空文件夹 (2认同)