How to attach custom task to execute before the test task in sbt?

前端 未结 2 731
一生所求
一生所求 2020-12-15 19:06

I\'m using SBT with Play Framework.

I created a custom TaskKey to run JavaScript tests in my project:

import sbt._
import sbt.Process._
         


        
相关标签:
2条回答
  • 2020-12-15 19:28

    Play 2.2.x uses SBT 0.13 (see What’s new in Play 2.2). That brings some new means of composing tasks in build.sbt itself (without resorting to a Scala file in project/ subdirectory).

    If you happen to use Play 2.2.x you could define the dependency between the tasks in build.sbt as follows:

    lazy val testJsTask = taskKey[Unit]("Run JavaScript tests.")
    
    testJsTask := {
      println("Running JavaScript tests...")
      java.util.concurrent.TimeUnit.SECONDS.sleep(3)
      println("...done.")
    }
    
    test in Test := {
      testJsTask.value
      (test in Test).value
    }
    

    See Tasks in the official documentation of SBT for more details.

    0 讨论(0)
  • 2020-12-15 19:39

    This is one way to do it:

    Define the task key:

    val testJsTask = TaskKey[Unit]("testJs", "Run javascript tests.")    
    

    Define the task in your projects settings:

    testJsTask <<= testJs
    

    Make test dependent on it:

    (test in Test) <<= (test in Test) dependsOn (testJs)
    

    testJs can be defined as follows:

      def testJs = (streams) map { (s) => {
        s.log.info("Executing task testJs")
        // Your implementation
      }
    

    [EDIT] You have to define the task dependencies within the projects settings. For a "normal" project, you would do it the following way:

      lazy val testProject = Project(
        "testProject",
        file("testProject"),
        settings = defaultSettings ++ Seq(
          testJsTask <<= testJs,
          (test in Test) <<= (test in Test) dependsOn (testJsTask)
        )
      )
    
    0 讨论(0)
提交回复
热议问题