我有一个多模块播放!使用sbt构建的应用程序和我在CI服务器上有一个并行测试和进化的问题:当sbt开始测试并首先启动进化脚本时,其他失败是因为"数据库'默认'处于不一致状态"一段时间(直到进化脚本停止)。
我知道如何手动执行演进以及如何从sbt运行它们,但我不知道如何将此配置插入sbt以确保演进在所有模块的所有测试之前仅应用一次和。
作为参考,我使用以下方式运行演化:
object ApplyEvolutions extends App {
OfflineEvolutions.applyScript(
new File("."),
this.getClass.getClassLoader,
args.headOption.getOrElse("default"))
}
和我的sbt设置:
val runEvolution = taskKey[Unit]("Applies evolutions")
lazy val runEvolutionTask = runEvolution := {
val log = streams.value.log
val dbName = "default"
Run.executeTrapExit( {
log.info(s"Applying evolutions for database $dbName")
Run.run("controllers.ApplyEvolutions",
(fullClasspath in Compile).value.map { _.data },
Seq(dbName),
log
)(runner.value)
}, log )
}
lazy val runEvolutionsBeforeTests = Seq(
Tasks.runEvolutionTask,
(test in Test) <<= (test in Test) dependsOn Tasks.runEvolution
)
tl;博士使用alias
我使用SBT 0.13.2-M3。
[task-dependson]> about
[info] This is sbt 0.13.2-M3
[info] The current project is {file:/Users/jacek/sandbox/so/task-dependsOn/}task-dependson 0.1-SNAPSHOT
[info] The current project is built against Scala 2.10.3
[info] Available Plugins: sbt.plugins.IvyModule, sbt.plugins.JvmModule, sbt.plugins.GlobalModule, com.typesafe.sbt.SbtGit, com.typesafe.sbt.SbtProguard, growl.GrowlingTests, org.sbtidea.SbtIdeaPlugin, sbtman.Plugin, np.Plugin, com.timushev.sbt.updates.UpdatesPlugin
[info] sbt, sbt plugins, and build definitions are using Scala 2.10.3
这里有一个任务sayHelloT
的例子,在所有子项目中,在test
之前执行一次。我使用Specs2作为测试框架。
import java.util.concurrent._
lazy val a = project
lazy val b = project
lazy val sayHelloT = taskKey[Unit]("Say Hello")
sayHelloT := {
val log = streams.value.log
log.info("Sleeping for 5 secs...")
TimeUnit.SECONDS.sleep(5)
log.info("Hello, my friend")
}
libraryDependencies in ThisBuild += "org.specs2" %% "specs2" % "2.3.10" % "test"
scalacOptions in Test ++= Seq("-Yrangepos")
addCommandAlias("sht", ";sayHelloT; test")
正如你所看到的,有两个子项目——a
和b
——和一个单独的别名sht
,它将执行sayHelloT
,只有在它成功完成后,test
才会启动。
[task-dependson]> sht
[info] Sleeping for 5 secs...
[info] Hello, my friend
[success] Total time: 5 s, completed Mar 12, 2014 10:19:39 PM
[info] ASpec
[info]
[info] A project should
[info] + contain 11 characters
[info]
[info] Total for specification ASpec
[info] Finished in 205 ms
[info] 1 example, 0 failure, 0 error
[info] Passed: Total 0, Failed 0, Errors 0, Passed 0
[info] No tests to run for task-dependson/test:test
[info] Passed: Total 1, Failed 0, Errors 0, Passed 1
[info] BSpec
[info]
[info] B project should
[info] + contain 11 characters
[info]
[info] Total for specification BSpec
[info] Finished in 240 ms
[info] 1 example, 0 failure, 0 error
[info] Passed: Total 1, Failed 0, Errors 0, Passed 1
[success] Total time: 3 s, completed Mar 12, 2014 10:19:42 PM