scala sbt test run setup and cleanup command once on multi project

一曲冷凌霜 提交于 2019-12-04 07:38:37

I wrote a blog post on sequencing tasks, which you might find it useful.

If you want to aggregate tests and make sure things are sequenced, one quick way of doing that is making a custom command. The following defines a command alias called sts:

lazy val commonSettings = Seq(
  scalaVersion := "2.11.4"
)
lazy val specs2Core = "org.specs2" %% "specs2-core" % "2.4.15"
val startTest = taskKey[Unit]("start test")
val stopTest = taskKey[Unit]("stop test")

lazy val root = (project in file(".")).
  aggregate(app, webapp).
  settings(commonSettings: _*).
  settings(addCommandAlias("sts", ";startTest;test;stopTest"): _*).
  settings(
    startTest in ThisBuild := {
      println("starting server...")
      Thread.sleep(500)
    },
    stopTest in ThisBuild := {
      println("stopping server...")
      Thread.sleep(500)
    }
  )

lazy val app = (project in file("app")).
  settings(commonSettings: _*).
  settings(
    libraryDependencies += specs2Core % Test
  )

lazy val webapp = (project in file("webapp")).
  settings(commonSettings: _*).
  settings(
    libraryDependencies += specs2Core % Test
  )

You can substitute the implementation of startTest in ThisBuild and stopTest in ThisBuild as you like. By defining these settings at the ThisBuild level, sts command should work at both the root level as well as at individual subproject level.

root> sts
starting server...
[success] Total time: 1 s, completed Jan 13, 2015 5:20:58 PM
[info] HelloWorldSpec
....
[info] Passed: Total 3, Failed 0, Errors 0, Passed 3
[success] Total time: 1 s, completed Jan 13, 2015 5:20:59 PM
stopping server...
[success] Total time: 1 s, completed Jan 13, 2015 5:20:59 PM 
root> project app
[info] Set current project to app
app> sts
starting server...
[success] Total time: 1 s, completed Jan 13, 2015 5:21:15 PM
[info] HelloWorldSpec
....
[info] Passed: Total 3, Failed 0, Errors 0, Passed 3
[success] Total time: 1 s, completed Jan 13, 2015 5:21:16 PM
stopping server...
[success] Total time: 1 s, completed Jan 13, 2015 5:21:16 PM
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!