我正在使用 sbt 构建一个多模块项目。其中一个模块是使用 npm 构建的 JavaScript 应用程序。我想简单地通过 shell 脚本执行 npm 作为包任务的一部分,并使用生成的输出文件作为该模块的工件。我可以运行 shell 命令作为包任务的一部分,但由于某种原因,当我运行publish或publishLocal时,该任务被忽略。
附件是我的 Build.scala。
这是应该使用 npm 构建的 account-ui 项目。现在,实际的 npm 构建由一个简单的脚本表示。
import sbt._
import Keys._
import play.Play.autoImport._
import PlayKeys._
import play.PlayScala
import sbtassembly._
import AssemblyKeys._
import net.virtualvoid.sbt.graph.Plugin.graphSettings
import ohnosequences.sbt.SbtS3Resolver.autoImport._
object Build extends Build {
lazy val commonSettings = Seq(
organization := "myorg",
scalaVersion := "2.11.5"
)
lazy val publishSettings = Seq(
publishTo := {
val prefix = if (isSnapshot.value) "snapshots" else "releases"
Some(s3resolver.value("MyOrg " + prefix + " S3 bucket", s3(prefix+".repo.myorg.com")))
}
)
lazy val root = Project(id = "root", base = file(".")).settings(commonSettings).settings(
name := "accounts-root"
).settings(publishSettings).aggregate(api, ui)
val _apiName = "accounts-api"
lazy val api = Project(id = "api", base = file("./api")).settings(commonSettings).settings(
name := "accounts-api",
libraryDependencies ++= Seq(
specs2
)
).settings(publishSettings).settings(graphSettings).settings(
mainClass in assembly := Some("play.core.server.NettyServer"),
fullClasspath in assembly += Attributed.blank(PlayKeys.playPackageAssets.value),
test in assembly := {},
assemblyExcludedJars in assembly := {
val cp = (fullClasspath in assembly).value
cp filter { (el) => {
val name = el.data.getName
name.contains("mockito") || name.contains("commons-logging") || name.contains("specs2")
}
}
}
).settings(addArtifact(Artifact(_apiName, "assembly"), assembly)
).enablePlugins(PlayScala)
val npmBuildTask = taskKey[Unit]("some custom task")
lazy val ui = Project(id = "ui", base = file("./ui")).settings(commonSettings).settings(
name := "accounts-ui",
npmBuildTask := {
val processBuilder = Process("npm-build.sh")
val process = processBuilder.run()
if(process.exitValue() != 0)
throw new Error(s"custom task failed with exit value ${process.exitValue()}")
},
Keys.`package` <<= (Keys.`package` in Compile) dependsOn npmBuildTask
).settings(publishSettings)
}
最佳答案
我能够按如下方式解决它:
val npmPackageTask = taskKey[File]("npm package task")
lazy val ui = Project(id = "ui", base = file("./ui")).settings(commonSettings).settings(
name := "accounts-ui",
npmPackageTask := {
val processBuilder = Process("npm-build.sh")
val process = processBuilder.run()
if(process.exitValue() != 0)
throw new Error(s"custom task failed with exit value ${process.exitValue()}")
file(".")
},
packageBin in Compile <<= npmPackageTask
).settings(publishSettings)
关键是创建 key 为taskKey[File],使用packageBin key ,并用<<=运算符替换任务。
关于sbt - 如何在多模块项目中通过 sbt 调用 npm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32985929/