我能够弄清楚如何在 Scalatra 测试项目中执行此操作,因此我只需要修改以下代码以使其适应 Play 的文件夹结构。但是,它确实有效。它的作用是定义一个新的 sbt “插件”,并在编译时将 /src/main/html 中的所有文件复制到 /resource_managed/main/views。
希望有人会发现这很有用!
import sbt._
import Keys._
import java.io.File
import org.apache.commons.io.FileUtils._
object CopyViews extends sbt.Plugin {
import CopyViewsKeys._
object CopyViewsKeys {
val copy = TaskKey[Unit]("copy-views", "Copy views into resourceManaged.")
}
private def copyViewsTask = (streams, sourceDirectory in copy, resourceManaged in copy) map {
(out, source, destination) =>
out.log.info("Copying Views to " + destination.getAbsolutePath())
copyDirectory(source, destination)
}
def copyViewsSettingsIn(c: Configuration): Seq[Setting[_]] =
inConfig(c)(Seq(
sourceDirectory in copy <<= (sourceDirectory in c) { _ / "html" },
resourceManaged in copy <<= (resourceManaged in c) { _ / "views" },
copy <<= copyViewsTask
)) ++ Seq(
compile in c <<= (compile in c).dependsOn(copy in c)
)
def copyViewsSettings: Seq[Setting[_]] =
copyViewsSettingsIn(Compile)
}
object ModFallBuild extends Build {
import CopyViews._ // Import in our Build so we can use in our build.sbt file.
lazy val modfall = Project("modfall", file("."))
}
现在,在我们的 build.sbt 文件中,我们可以添加
seq(copyViewsSettings:_*)
现在在编译时复制视图:)
我敢肯定,可能有一种更简单的方法可以做到这一点,但这对我有用,并且能够让我了解构建 sbt 插件!:P