scalabuildsbtsbt-0.13

SBT: execute task using other task value


I redefine my tests to supply some arguments from configuration to test suite:

This is an excerpt from my Build.scala:

object Build extends Build {
  lazy val myProject = (project in file("my_project")).settings(
    test in Test := myProjectTest.value
  )

  val myProjectTest = Def.task {
    (testOnly in Test).toTask(" tests.Suites -- " +
      s"-Ddbserver=localhost " +
      s"-Ddbport=3306 ").value
  }
}

This works ok.

Now, I wanted to give my test suite the name of an artifact like this:

val myProjectTest = Def.task {
  val art = (Keys.artifactPath in (Compile, packageBin)).value

  (testOnly in Test).toTask(" tests.Suites -- " +
    s"-Dartifact=${art.getCanonicalPath} " +
    s"-Ddbserver=localhost " +
    s"-Ddbport=3306").value
}

But it shows the following error message:

[error] /tmp/aa/project/Build.scala:17: Illegal dynamic reference: art
[error]     s"-Dartifact=${art.getCanonicalPath} " +
[error]                    ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed

I know something about SBT internals, macros, task dependency graph, and I even managed to solve some of my tasks using scopes. Here I tried to use map or flatMap on (Keys.artifactPath in (Compile, packageBin)) but cannot achieve the desirted result. Whenever I try to access .value I get Illegal dynamic reference.

Please, guide me. I just need to pass the value of task to other task (inputKey) parameters.

SBT version: 0.13.5


Solution

  • By trial and error I've done what I wanted eventually using Def.taskDyn (dynamic task):

    object Build extends Build {
      lazy val myProject = (project in file("my_project")).settings(
        test in Test := myProjectTest.value
      )
    
      lazy val myProjectTest = Def.taskDyn {
        val art = (Keys.artifactPath in (Compile, packageBin)).value
    
        (testOnly in Test).toTask(" tests.Suites -- " +
          s"-Dartifact=${art.getCanonicalPath} " +
          s"-Ddbserver=localhost " +
          s"-Ddbport=3306")
      }
    }