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

11,301

Solution 1

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)
    )
  )

Solution 2

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.

Share:
11,301
Piotr Kukielka
Author by

Piotr Kukielka

Twitter Github

Updated on June 18, 2022

Comments

  • Piotr Kukielka
    Piotr Kukielka almost 2 years

    I'm using SBT with Play Framework.

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

    import sbt._
    import sbt.Process._
    import PlayProject._
    
    object ApplicationBuild extends Build {
    
      val testJsTask = TaskKey[Unit]("testJs", "Run javascript tests.") := {}
    
      val main = PlayProject("xxx", 1.0, Seq())
        .settings(defaultScalaSettings: _*)
        .settings(testJsTask)
    }
    

    So far so good.

    I want to run this testJsTask always when someone executes the test task.

    I guess it should be something as follows:

    test in Test <<= (test in Test).dependsOn(testJsTask)
    

    I've no idea how it should be defined exactly. How to add a dependency to an existing task like 'test' or 'build'?

    UPDATE

    After changes proposed by @Christian the build definition looks as follows:

    object ApplicationBuild extends Build {
      val testJsTask = TaskKey[Unit]("testJs", "Run tests for javascript client.")
      def testJs = {}
    
      val main = PlayProject("xxx", 1.0, Seq())
        .settings(defaultScalaSettings: _*)
        .settings(testJsTask := testJs)
    
      (test in Test) <<= (test in Test) dependsOn (testJs)
    }
    

    Unfortunately, the solution doesn't work either:

    [error] /xxx/project/Build.scala:21: not found: value test
    [error]   (test in Test) <<= (test in Test) dependsOn (testJs)
    [error]    ^
    [error] one error found
    [error] {file:/xxx/project/}default-f468ae/compile:compile: Compilation failed
    
  • Piotr Kukielka
    Piotr Kukielka over 12 years
    Somehow this looks wrong on incomplete to me. What I want to do is ensure, that when someone will run 'test' command, then except all other test it will execute also testJs. Also I see you didn't used ':= ' symbol at all, which I know is needed when building Tasks - or am I wrong?. EDIT: I see your edit, now it starts to make more sense, I'll try it right away ;)
  • Christian
    Christian over 12 years
    You can directly define a task with :=. I prefer the other possibility, because then I can separate the task keys from the actual tasks.
  • Piotr Kukielka
    Piotr Kukielka over 12 years
    I still have the same problem as before: Build.scala:19: not found: value test. [error] (test in Test) <<= (test in Test) dependsOn (testJsTask)
  • Christian
    Christian over 12 years
    Could you post your current build script?
  • Christian
    Christian over 12 years
    I never used the play framework, but maybe a PlayProject does not have (test in Test)?
  • Piotr Kukielka
    Piotr Kukielka over 12 years
    In PlayProject source code I don't see any changes for tests unlucky (btw. github.com/playframework/Play20/wiki/SettingsSBT). Also, I edited my first post.
  • sksamuel
    sksamuel over 9 years
    Does this run the tasks sequentially or in concurrently?
  • htomek
    htomek over 8 years
    Sequentially. First testJsTask, then test task.
  • trudolf
    trudolf over 7 years
    As far is I understand this is not correct @htomek scala-sbt.org/release/docs/… Use Def.sequential() to run tasks in order scala-sbt.org/release/docs/Howto-Sequencing.html
  • arussell84
    arussell84 over 6 years
    This answer is outdated. As of SBT 1.1.0-RC1, the key line to make test dependent on testJs should be: (Test / test) := ((Test / test) dependsOn testJs).value