如何执行bash脚本作为sbt任务?

Son*_*123 28 sbt

我想自动为我的Java Play 2.3应用程序构建文档.目前,我使用Makefile从*.dot文件生成图像,并将Markdown源合并为Html/PDF:

dot diagram1.dot -Tpdf -o diagram1.pdf
dot diagram2.dot -Tpdf -o diagram2.pdf
pandoc doc1.markdown -o doc1.pdf
# ...
Run Code Online (Sandbox Code Playgroud)

现在我想直接从SBT运行这些简单的bash命令.最好的方法是什么?

我在SBT参考中找到了一些SBT Documentation插件,但没有任何东西可以运行简单的shell脚本.

Jac*_*ski 61

您可以在sbt的官方文档中找到外部流程中的一些答案,例如

要运行外部命令,请使用感叹号进行操作!:

 "find project -name *.jar" !
Run Code Online (Sandbox Code Playgroud)

不要忘记使用,import scala.sys.process._所以!可以解决作为一种方法String.

在激活器控制台(也称为 sbt shell)中执行以下操作yourshell.sh- 请注意eval命令和脚本名称周围的引号:

eval "yourshell.sh" !
Run Code Online (Sandbox Code Playgroud)

要将其作为任务提供build.sbt,请在项目中添加以下内容:

lazy val execScript = taskKey[Unit]("Execute the shell script")

execScript := {
  "yourshell.sh" !
}
Run Code Online (Sandbox Code Playgroud)


Ste*_*fen 25

我们要求执行一些npm脚本作为sbt任务,如果其中一个npm脚本失败,则让构建失败.花了我一些时间来找到一种方法来创建一个适用于Windows和Unix的Task.所以这就是我想出来的.

lazy val buildFrontend = taskKey[Unit]("Execute frontend scripts")

buildFrontend := {
      val s: TaskStreams = streams.value
      val shell: Seq[String] = if (sys.props("os.name").contains("Windows")) Seq("cmd", "/c") else Seq("bash", "-c")
      val npmInstall: Seq[String] = shell :+ "npm install"
      val npmTest: Seq[String] = shell :+    "npm run test"
      val npmLint: Seq[String] = shell :+    "npm run lint"
      val npmBuild: Seq[String] = shell :+   "npm run build"
      s.log.info("building frontend...")
      if((npmInstall #&& npmTest #&& npmLint #&& npmBuild !) == 0) {
        s.log.success("frontend build successful!")
      } else {
        throw new IllegalStateException("frontend build failed!")
      }
},
(run in Compile) <<= (run in Compile).dependsOn(buildFrontend)
Run Code Online (Sandbox Code Playgroud)