我有一个子模块,通过调用外部命令编译。我想将生成的文件包含在jar中。所以我写了一个任务: ```
myTask := {
import sys.process.stringSeqToProcess
Seq("my", "command") !
}
unmanagedResourceDirectories in Compile += baseDirectory.value / "dist"
cleanFiles <+= baseDirectory { base => base / "dist" }
Keys.`package` <<= (Keys.`package` in Compile) dependsOn npmBuildTask.toTask
当我调用mySubmodule/package
任务时,它运作良好。但是当我从stage
调用sbt-native-packager
任务时,我的任务被忽略(未执行)。
答案 0 :(得分:2)
有几种方法可以解决这个问题。我假设您要将dist
文件夹添加到生成的应用程序jar中。
您的配置不起作用,因为stage
不依赖于package
。这导致npmBuildTask
未被调用。
解决此问题的最简单方法是将npmBuildTask
作为依赖项添加到stage
stage <<= stage dependsOn npmBuildTask.toTask
我不推荐这种方法。
为此目的准确定义了SBT Resoure Generators。内联版本可能看起来像这样
resourceGenerators in Compile += Def.task {
streams.value.log.info("running npm generator")
val base = (resourceManaged in Compile).value / "dist"
// A resource generator returns a Seq[File]. This is just an example
List("index.js", "test.js").map { file =>
IO.writeLines(base / file, List("var x = 1"))
base / file
}
}.taskValue
或者您可以在AutoPlugin
中提取此内容以将“what”和“how”分开。
创建project/NpmPlugin.scala
并添加以下内容
import sbt._
import sbt.Keys._
import sbt.plugins.JvmPlugin
object NpmPlugin extends AutoPlugin {
override val requires = JvmPlugin
override val trigger = AllRequirements
object autoImport {
val npmBuildTask = TaskKey[Seq[File]]("npm-build-task", "Runs npm and builds the application")
}
import autoImport._
override def projectSettings: Seq[Setting[_]] = Seq(
// define a custom target directory for npm
target in npmBuildTask := target.value / "npm",
// the actual build task
npmBuildTask := {
val npmSource = (target in npmBuildTask).value
val npmTarget = (resourceManaged in Compile).value / "dist"
// run npm here, which generates the necessary values
streams.value.log.info("running npm generator")
// move generated sources to target folder
IO.copyDirectory(npmSource, npmTarget)
// recursively get all files in the npmTarget
(npmTarget ***).get
},
resourceGenerators in Compile += npmBuildTask.taskValue
)
}
build.sbt
将如下所示
name := "resource-gen-test"
version := "1.0"
enablePlugins(JavaAppPackaging)
很干净:))
最后但并非最不重要的是可以使用mappings
。它们是低级细节,在sbt中驱动了大量的包生成。这个解决方案的主要思想是
Seq[(File, String)]
)这种方法的优点是,您可以更灵活地放置映射。
import sbt._
import sbt.Keys._
import sbt.plugins.JvmPlugin
import com.typesafe.sbt.SbtNativePackager.Universal
import com.typesafe.sbt.SbtNativePackager.autoImport.NativePackagerHelper._
object NpmMappingsPlugin extends AutoPlugin {
override val requires = JvmPlugin
override val trigger = AllRequirements
object autoImport {
val npmBuildTask = TaskKey[Seq[(File, String)]]("npm-build-task", "Runs npm and builds the application")
}
import autoImport._
override def projectSettings: Seq[Setting[_]] = Seq(
// define a custom target directory for npm
target in npmBuildTask := target.value / "npm" / "dist",
// the actual build task
npmBuildTask := {
val npmTarget = (target in npmBuildTask).value
// run npm here, which generates the necessary values
streams.value.log.info("running npm generator")
// recursively get all files in the npmTarget
// contentOf(npmTarget) would skip the top-level-directory
directory(npmTarget)
},
// add npm resources to the generated jar
mappings in (Compile, packageBin) ++= npmBuildTask.value,
// add npm resources to resulting package
mappings in Universal ++= npmBuildTask.value
)
}
正如您在此方法中所看到的,我们可以轻松地将生成的文件添加到不同的映射中。
但是,如果您需要这种灵活性,我只推荐这种方法,因为它需要更多的本机包装器知识。