How to make compile task depend on some other task in sbt? - scala

For example, I need to do some pre-processing of source files before compiling. How do I ensure that sbt always runs this task before compile if compile was requested?

Here's what I do to make sure my tests are run before I publish locally:
publishLocal <<= publishLocal dependsOn (test in Test)
For you I think you'd need something like
compile <<= compile dependsOn (myTask in myContext)

Related

How to run sbt assembly by excluding integration test

I am looking to create fat jar by 'sbt assembly' but i dont wan to run integration test in sbt assembly. I want only jar file.
You can either disable tests in your build.sbt using assembly / test := {} as suggested by #laughedelic, or if you're executing from a terminal and want to skip to the tests for that build use: sbt 'set test in assembly := {}' assembly.
If you have set the value in your build.sbt file you can check that its value is as expected by running sbt show assembly / test which should return something like ().

Override default compile task in sbt

In SBT, compile task does the compilation of the project code and test:compile does compilation of the project's tests. I want a single compile task which does both. I want to override the default compile task and dont want a task with a new name (because want to enforce compilation success of all tests with every code change to project's main code). Am using Build.scala (not build.sbt) and tried the method described in this SO answer. My trial is pasted below and does not work because the return type of the compile task is TaskKey[Analysis]. How should I change this?
val compileInTest = TaskKey[Analysis]("compile the tests")
compileInTest := {
(compile in Compile in <module-name>).value
(compile in Test in <module-name>).value
}
lazy val projectA = Project(
"a",
file("a"),
settings = hwsettings ++ Seq(
compile := compileInTest
))
You can define alias in .sbtrc file:
alias compile=test:compile
which will do both tasks.

SBT plugin how to make a source generator dependent on project's sources?

I'm trying to create a source generator in a SBT plugin that generate code based on the project's sources.
I tried something like this:
sourceGenerators in Compile += (sources in Compile) map { sources => doSomethingWithSources(sources) }
Unfortunately, SBT does not want to load this plugin due to the fact that there exists circular dependency.
Due to this fact I've created another task like this:
lazy val myTask = TaskKey[Unit]("myTask", "Do stuff")
This tasks actually depends on the sources value and generates the files.
Later I override the projectSettings value and add this:
myTask in Compile := {
val sourcesValue = (sources in Compile).value
doSomethingWithSources(sourcesValue)
},
sourcesGenerators in Compile += Def.task(Seq(new File("path/to/myGeneratedSource.scala"))).taskValue
I add this task as the dependency to the compile task in the build.sbt of the project that I want my plugin to do stuff like this:
compile in Compile <<= (compile in Compile) dependsOn (myTask in Compile)
While it works (the file is generated), when I launch the sbt command sbt run, it creates the file but does not compile it.
What is more, when I run just sbt compile run, it compiles only the project on the first (compile) task and generates my source and then on run part it compiles the generated source - so, in matter of speaking, it does somehow work, but it needs two compilations.
I'd like to ask if there is a simpler way to do this and, if not, how to make it work in only one compilation.

File generator task: "sourceGenerators in Compile" added task runs twice

I made a sbt plugin for play frameworks, which has got a sbt task who generates a scala object. Sbt task should run everytime when sources get compiled. Actually when sourceGenerators runs, task get called twice in one compile process.
Code to add task to sourceGenerators:
sourceGenerators in Compile <+= (myFileGeneratorTask in Compile)
I'm using Twirl to generate templates in Playframework. I'm thinking sourceGenerators runs twice because Twirl rerun sourceGenerators process. Am I right or is there another reason why sourceGenerators run my sbt task twice?
How do I have to involve a sbt task which generates a source into sourceManaged, into compile process? So that every time when sourceGenerators in Compile starts, my scala object is known by sourceGenerators without running twice?

Compile tests with SBT and package them to be run later

Im working with SBT and Play! Framework. Currently we have a commit stage in our pipeline where we publish to artifactory our binaries. The binaries are generated with the dist task. The pipeline then runs smoke and acceptance tests that are written in scala. They are run with sbt.
What I want to do is to compile the smoke and acceptance tests as well as the binary and publish them to artifactory. That will allow the pipeline to download these binaries (the test suites) and run them, instead of recompiling them every time, which takes a long time.
I tried sbt test:compile which generates the jar, but then I cant find a way to run the tests.
sbt dont publish test in artifacts
publishArtifact in GlobalScope in Test:== false
source: https://github.com/sbt/sbt/blob/a7413f6415687f32e6365598680f3bb8545c46b5/main/src/main/scala/sbt/Defaults.scala#L1118
this is how to enable it
// enable publishing the jar produced by `test:package`
publishArtifact in (Test, packageBin) := true
// enable publishing the test API jar
publishArtifact in (Test, packageDoc) := true
// enable publishing the test sources jar
publishArtifact in (Test, packageSrc) := true
source: http://www.scala-sbt.org/release/docs/Detailed-Topics/Artifacts
run the test
scala -classpath pipeline.jar classpath scalatest-<version>.jar org.scalatest.tools.Runner -p compiled_tests
where pipeline.jar is the test artifact you receive from the pipeline
or you can setup a test projet via sbt
http://www.scala-sbt.org/release/docs/Detailed-Topics/Testing.html