Disable aggregate for sbt custom task - scala

How can I disable aggregate for a single custom task?
I tried to add the following to my build.sbt:
aggregate in myTaskName:= false
But it doesn't work as I expected - I've got this error:
~\build.sbt:1: error: not found: value myTaskName
aggregate in myTaskName:= false

Working example (sbt 0.13.5):
val hello = TaskKey[Unit]("hello", "Prints 'Hello Zhu'")
val helloTask = hello := {
println("Hello Zhu")
}
aggregate in hello := false
Note, that TaskKey was used - not the Setting[Task] itself. It worth additional notice that this key should be accessable from your build.sbt and, as #Mark Harrah mentioned, hello must be fully-qualified.

Related

How to call an InputKey from within a Task in an SBT Plugin

I am trying to make a task execute several inputKey.
myTask := Def.taskDyn {
val outputOfMyInputKey = myInputKey.[WHAT SHOULD I DO HERE].value
Def.task {
// do something with outputOfMyInputKey
}
}
Anybody knows how to call inputKey with default parameters ?
I tried parsed , evaluated, value, inputTaskValue but none of them works.
Thanks.
Take a look at this section of the sbt docs: Get a Task from an InputTask. You can use .toTask to provide input for your input task:
myInputKey.toTask("").value
Notice that if you provide a non-empty input, it should start with a space:
myInputKey.toTask(" arg1 arg2").value

ScalaJs With Play Framework (Error in Tests)

I am trying to use ScalaJs cross build project with Play Framework 2.5.
I am facing a problem when I am trying to run the tests for Client.scala .
The error I am getting is -
caused by: TypeError: Cannot call method "appendChild" of null .
Client snippet
#JSExport
object DashboardClient extends js.JSApp {
#JSExport
def main(): Unit = {
val dashboard = new Dashboard
dom.document.getElementById("bodyContent").appendChild(dashboard.bodyFrag.render)
}
This bodyFrag is inside a different class
def bodyFrag =
div(
div(
`class` := "row border-bottom",
div(
`class` := "col-md-12",
div(
`class` := "col-md-2 image-alignment",
img(width := "161", src := "/assets/images/mountain.jpg")
),
div(`class` := "col-md-10")
)
),
div(
`class` := "row border-bottom",
div(
`class` := "col-md-12",
div(
`class` := "col-md-12",
h1(`class` := "text-center", "Dashboard")
)
)
)
)
So when I am trying to test is using utest I get the above mentioned error.
Please help.
P.S - I am completely new to Scala and ScalaJs.
Well, the error doesn't appear to have anything to do with bodyFrag. (Or Play, or even Scala.js, really.) The error is literally saying that getElementById("bodyContent") is null, which means it doesn't yet exist. I assume it's declared in the HTML.
This is a common trap in browser code. You need to wait for the DOM to be ready before you try manipulating it; you don't show the HTML page, so I'm not sure whether you're doing that, but I suspect not.
There are various ways to wait for DOM ready -- the common (but slower) onload approach, and the jQuery approach, are discussed in the jQuery documentation. As a quick fix, I would recommend wrapping the call to your Scala.js code inside window.onload.

sbt package separate jars for different test types

We have just upgraded to using sbt 12.2 on our project. I would like to have our tests packaged in separate jars such as: project-unit.jar, project-integration.jar
Our current folder structure is:
src
main
...
test
scala
com.company.unit
com.company.integration
com.company.functional
Any suggestion is very much appreciated.
Thanks
I have found a way to do this by:
Create a packaging task for the appropriate test type:
val integrationPackaging = TaskKey[File]("package-integration")
Add package settings to the test settings:
lazy val integrationTestSettings = createTestSettings("integration", IntegrationTests) ++ createPackageSettings("integration", IntegrationTests, integrationPackaging)
private def createPackageSettings(testType: String, testConfiguration: Configuration, packagingTaskKey: TaskKey[sbt.File]) = {
inConfig(testConfiguration)(Defaults.testSettings) ++
Defaults.packageTaskSettings(packagingTaskKey, mappings in (Test, packageBin)) ++
(artifactName in packagingTaskKey := {(scalaversion, moduleId, artifact) => "%s_%s-%s-%s.jar" format (moduleId.name, scalaversion.binary, buildVersion, testType)}) ++
(mappings in packagingTaskKey ~= { (ms: Seq[(File,String)]) =>
ms.filter { case (file, path) =>{ !file.getName.endsWith(".class") || file.getAbsolutePath.contains(testType) }
}
})
}
Note in the create package settings, I had to:
add default test settings to ensure all test settings are inherited
add default test package task settings (using mappings in Test packageBin)
add artifactName (what you want the jar name to be)
add specific mappings for the package task key (created in step 1 above), this is the crux of it, I had to define a filter that ensures only files with the path containing the testType (e.g. "integration", "unit" or "functional") are selected and resource files (e.g. files that do not end with ".class")

SBT how to run InputTask

I am creating some custom tasks in my SBT project and need to call other tasks for that.
How can i call inputTasks from inside my tasks and support them some input?
Since you can factor your own tasks around this I'm assuming you're trying to use the run task. It took a bit of digging, but I've finally made it work; in a nutshell, this is what you do (assuming your task is named deployTask, tweak to match your needs):
deployTask <<= ( fullClasspath in Compile, runner ) map { ( classpath, runner ) =>
val logger = ConsoleLogger() // Not sure this is optimal
Run.executeTrapExit( {
Run.run( "com.sample.MainClass",
classpath map { _.data },
Seq( "option1", "option2", "..." ), // <-- Options go here
logger )( runner )
}, logger )
}
This doesn't invoke the InputTask directly (I haven't found a way to do that yet), but it at least lets you run arbitrary Java code.

SBT 0.11 InputKey depending on other tasks

In 0.7.x we used dependsOn to declare that a task depends on other tasks - meaning other tasks have to be executed first.
In 0.11.x we use <<= to declare that a task depends on some other task. The SBT wiki says a lot on how to declare a TaskKey[_] which depends on other tasks and settings (there are questions here that deal with that), but not much on InputKey[_]. If I have an input key declared like this:
val benchTask = InputKey[Unit](
"bench",
"Runs a specified benchmark."
) <<= inputTask {
(argTask: TaskKey[Seq[String]]) =>
argTask map {
args =>
// ...
} // xxx
}
How can I make it depend on other tasks, like for example packageBin in Test? I can put dependsOn instead of the xxx comment above, but that gives me type errors.
Thank you.
Map your other task together with argTask:
inputTask {
(argTask: TaskKey[Seq[String]]) =>
(argTask, packageBin in Test) map {
(args, pb) =>
// ...
}
}