如何在多模块项目中通过sbt调用npm

  • 本文关键字:sbt 调用 npm 项目 模块 sbt
  • 更新时间 :
  • 英文 :


我正在使用sbt构建一个多模块项目。其中一个模块是使用npm构建的JavaScript应用程序。我想通过shell脚本简单地执行npm作为包任务的一部分,并使用结果输出文件作为该模块的工件。我可以将shell命令作为包任务的一部分运行,但是由于某种原因,当我运行publish或publishLocal时,这个任务被忽略了。

附件是我的Build.scala。

它是accounts-ui项目,应该使用npm来构建。现在实际的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)

关键是将密钥创建为taskKey[File],使用packageBin密钥,并用<<=操作符替换任务。

相关内容

  • 没有找到相关文章

最新更新