加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 如何在sbt中所有模块的所有测试之前运行任务

发布时间:2020-12-16 08:43:39 所属栏目:安全 来源:网络整理
导读:我有一个多模块播放!使用sbt构建的应用程序我在CI服务器上有并行测试和演进的问题:当sbt开始测试并首次启动evolution脚本时,其他因为“数据库’默认’处于不一致状态”一段时间失败(直到进化脚本停止). 我知道如何手动执行evolutions以及如何从sbt运行它们
我有一个多模块播放!使用sbt构建的应用程序我在CI服务器上有并行测试和演进的问题:当sbt开始测试并首次启动evolution脚本时,其他因为“数据库’默认’处于不一致状态”一段时间失败(直到进化脚本停止).

我知道如何手动执行evolutions以及如何从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; dr使用别名

我用的是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–在测试之前在所有子项目中执行一次.我使用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 – 以及一个将执行sayHelloT的别名sht,并且只有在成功完成后才需要5秒,测试开始.

[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,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 error
[info] Passed: Total 1,Passed 1
[success] Total time: 3 s,2014 10:19:42 PM

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读