Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
411 views
in Technique[技术] by (71.8m points)

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

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...