如何使用 SBT 将一些文件复制到构建目标目录? [英] How to copy some files to the build target directory with SBT?

查看:52
本文介绍了如何使用 SBT 将一些文件复制到构建目标目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将一些源文件(例如 /src/main/html/*.html)复制到构建输出目录(例如 /target/scala-2.11/)>) 使用 SBT,以便文件最终位于 目标根目录 中而不是 classes 子目录中(如果我将源目录添加到 unmanagedResourceDirectories 会发生这种情况)?

How can I copy some source files (e.g. /src/main/html/*.html) to the build output directory (e.g. /target/scala-2.11/) with SBT so that the files end up in the target root and not in the classes subdirectory (which is what happens if I add the source directory to unmanagedResourceDirectories)?

推荐答案

一种方法是首先收集您想要复制的所有文件,例如使用 PathFinder 并使用 copy 方法之一在 sbt.io.IO 结合 Path.rebase

One way would be to first collect all files you wish to copy, using for example a PathFinder and use one of the copy methods in sbt.io.IO combined with Path.rebase

对于问题中的具体示例:

For the specific example in the question:

// Define task to  copy html files
val copyHtml = taskKey[Unit]("Copy html files from src/main/html to cross-version target directory")

// Implement task
copyHtml := {
  import Path._

  val src = (Compile / sourceDirectory).value / "html"

  // get the files we want to copy
  val htmlFiles: Seq[File] = (src ** "*.html").get()

  // use Path.rebase to pair source files with target destination in crossTarget
  val pairs = htmlFiles pair rebase(src, (Compile / crossTarget).value)

  // Copy files to source files to target
  IO.copy(pairs, CopyOptions.apply(overwrite = true, preserveLastModified = true, preserveExecutable = false))

}

// Ensure task is run before package
`package` := (`package` dependsOn copyHtml).value

这篇关于如何使用 SBT 将一些文件复制到构建目标目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆