创建 SBT 任务以在编译期间复制目录?

发布于 2024-12-09 06:19:18 字数 723 浏览 1 评论 0原文

我对整个 SBT 和 Scala 场景很陌生,正在尝试构建一个使用 Java/Scala 类和 Hibernate 的项目。我正在让项目构建得很好——我只需手动将我的 hibernate 配置文件复制到我的 target/scala/classes 文件夹,以便 hibernate 可以拾取它们。

有没有办法在 SBT 中创建一个任务来在每次编译时复制这些文件夹?这是我的 Build.scala 文件:

import sbt._

object Sportsbook extends Build {
  lazy val project = Project (
    "sportsbook",
    file("."),
    copyConfigTask
  )

  val copyConfig = TaskKey[Unit]("copy", "Copy hibernate files over to target directory")

  /*
  // Something like this
  lazy val copyConfigTask = copyConfig <<=
    val configDir1 = baseDirectory / "config"
    val configDir2 = outputPath / "config"
    IO.copyDirectory(configDir1, configDir2)
  */
}

I'm new to the whole SBT and Scala scene and am trying to build a project that uses Java/Scala classes and Hibernate. I'm getting the project to build fine -- I just have to manually copy over my hibernate config files to my target/scala<version>/classes folder so they can be picked up by hibernate.

Is there a way to create a task in SBT to copy these folders over on each compile? This is my Build.scala file:

import sbt._

object Sportsbook extends Build {
  lazy val project = Project (
    "sportsbook",
    file("."),
    copyConfigTask
  )

  val copyConfig = TaskKey[Unit]("copy", "Copy hibernate files over to target directory")

  /*
  // Something like this
  lazy val copyConfigTask = copyConfig <<=
    val configDir1 = baseDirectory / "config"
    val configDir2 = outputPath / "config"
    IO.copyDirectory(configDir1, configDir2)
  */
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴骨ら 2024-12-16 06:19:18

实现此目的最直接的方法是将文件移动到 ./src/main/resources/config 中。

或者,将 ${base}/config 添加到 Compile 中的resourceDirectories

resourceDirectories in Compile <+= baseDirectory / "config"

不幸的是,其中的文件将被复制到类路径的根目录。您需要将它们移至 ./src/config/config 才能恢复。 (查看资源的映射如何基于资源文件与基本资源目录的相对位置

您希望将文件打包到 JAR 中吗?这两个答案都会导致这个结果。您可以将它们从 packageBin 中的映射中取出来避免这种情况。

mappings in (Compile, packageBin) ~= (_.filter { case (file, outpath) => outpath.startsWith("/config")} )

The most direct means to achieve this is to move the files into ./src/main/resources/config.

Alternatively, add ${base}/config to resourceDirectories in Compile.

resourceDirectories in Compile <+= baseDirectory / "config"

Unfortunately, files in there will be copied to the root of the classpath. You would need to move them to ./src/config/config to restore that. (See how the mappings for resources are based on the relative location of resource files to the base resource directories)

Do you want the files packaged in your JAR? Both of these answers would result in that. You could take them out of mappings in packageBin to avoid this.

mappings in (Compile, packageBin) ~= (_.filter { case (file, outpath) => outpath.startsWith("/config")} )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文