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

时间:2015-10-07 07:12:32

标签: sbt

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

附件是我的Build.scala。

应该使用npm构建的accounts-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)

}

1 个答案:

答案 0 :(得分:1)

我能够解决它如下:

df.sort(columns=['A','time'])

关键是将密钥创建为taskKey [File],使用packageBin密钥,并将该任务替换为&lt;&lt;&lt; =运算符。