【问题标题】:How to copy resources with scala + play + sbt如何使用 scala + play + sbt 复制资源
【发布时间】:2013-11-10 11:03:13
【问题描述】:

我正在使用 sbt [0.13] 编译一个使用 scala [2.10.3] 的 play [2.2] 项目。我有用于数据库迁移的 .sql 文件和 scala 文件。目录结构如下:

app
|-> db
     |-> migration
           |-> V1__init.scala
           |-> V2__newTable.sql

当我从播放控制台 (REPL) 运行编译时,scala 文件 (V1__init.scala) 被编译为 .class 并复制到 classes 文件夹。但是.sql文件没有移动。

我尝试添加unmanagedResourceDirectories in Compile <++= baseDirectory { dir => Seq(dir/"app/db/migration") ++ Seq(dir/"db/migration") },但它没有复制文件。整个区块长这样:

val main = play.Project(appName, appVersion, appDependencies).settings(
    scalaVersion := "2.10.3",
    scalacOptions ++= Seq("-feature"),   // enable feature warnings
    unmanagedResourceDirectories in Compile <++=  baseDirectory { dir => Seq(dir/"app/db/migration") ++ Seq(dir/"db/migration") }
)

我也尝试过使用copyResources,但无法使用。此处描述:http://www.playframework.com/documentation/2.0/SBTSettings

那么有谁知道我如何将 sql 文件复制到 classes 文件夹?

谢谢!

更新

我得到了IO.copyDirectory(new java.io.File("app/db/migration"), new java.io.File("target/scala-2.10/classes/db/migration"), true) 来复制文件,但目标是硬编码的,当我更新 scala 时会改变

【问题讨论】:

  • 您考虑过使用builtin support for database evolutions吗?
  • 是的,我有,但看起来 Evolutions 不支持运行任意 scala 代码,而是需要 .sql 文件
  • 你也可以把 sql 脚本放到 conf 目录中,所以它会自动在类路径中。
  • @Schleichardt 我曾考虑过这一点,但我认为 conf 目录不会自动编译,这是必需的,因此可以用 scala 或 java 编写迁移脚本。不过谢谢

标签: scala playframework-2.0 sbt


【解决方案1】:
  val main = play.Project(appName, appVersion, appDependencies).settings(
    scalaVersion := "2.10.3",
    scalacOptions ++= Seq("-feature"),   // enable feature warnings
    unmanagedResourceDirectories in Compile <+= scalaSource in Compile,
    excludeFilter in unmanagedResources in Compile := "*.scala" || "*.java"
  )

您可以通过以下方式轻松检查类文件夹中的内容:

 sbt clean full-classpath && ls target/scala-2.10/classes/db/migration/

【讨论】:

  • 谢谢!这几乎完全符合我的需要。我需要将".DS_Store" 添加到排除列表中,所以整行看起来像:excludeFilter in unmanagedResources in Compile := "*.scala" || "*.java" || ".DS_Store"
猜你喜欢
  • 2015-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-18
  • 2012-11-06
  • 2011-06-17
相关资源
最近更新 更多