SBT dependsOn usage - migration from 0.12 to 0.13 - scala

I have a command like this in build.sbt
run <<= (run in Compile) dependsOn npmBuildTask
According to documentation <<= is deprecated so I want to use := this one.
I tried with;
run in Compile := ((run in Compile).dependsOn(npmBuildTask).value)
run in Compile := (run in Compile).dependsOn(npmBuildTask).value
run in Compile := run.dependsOn(npmBuildTask).value
But whole of them are not working for me. Could you please help me?

Finally I found the solution.
compile := ((compile in Compile) dependsOn npmBuildTask).value
This is working for me. The problem was in the following code:
run := ((run in Compile) dependsOn npmBuildTask).value
compile and run are different. compile has a return type as sbt.TaskKey[sbt.inc.Analysis] and run has a return type as sbt.InputKey[scala.Unit]. Because of this you should use this command:
run := ((run in Compile) dependsOn npmBuildTask).evaluated
Now everything is working fine.

Related

Set mainClass for sbt native packager Universal

I have a project that has the following build.sbt:
addCommandAlias("package", "dist")
lazy val actual = (project in file("."))
.enablePlugins(UniversalPlugin, JavaServerAppPackaging)
.settings(
name := "DeployerPod",
mainClass := Some("com.myself.executable.Runner"),
Compile / mainClass := Some("com.myself.executable.Runner"),
Compile / run / mainClass := Some("com.myself.utils.Pipeline"),
Universal / mainClass := Some("com.myself.executable.Runner"),
Universal / compile / mainClass := Some("com.myself.executable.Runner"),
)
We have a CICD which runs a Dockerfile.
There I have sbt run as one of the steps, which will execute com.myself.utils.Pipeline class to run a Scala class and do the pre requisites for the pipeline.
As one of the last sbt based steps, I'm also running sbt package, which eventually runs an sbt dist command. At this point, inside the extracted ZIP's bin folder, I see two BAT files corresponding to the two main classes. Unfortunately I only want the Runner class BAT instead of Pipeline BAT.
For this I tried running sbt package -main com.myself.executable.Runner but that failed saying Not a valid command: -
Is there a way I can specify the mainClass only for this Universal plugin somehow? Because the way I've tried in my build.sbt doesn't seem to work.

How to make the sbt assembly task depend on test

When I run sbt assembly the tests are not run. How can I make the tests to run before running the assembly task?
From the documentation at https://github.com/sbt/sbt-assembly#assembly-task:
To run the test during assembly,
lazy val app = (project in file("app"))
.settings(
assembly / test := (Test / test).value,
// more settings here ...
)

Excluding a src file from sbt compile?

I've been trying to exclude a file from "sbt run" and "sbt compile". The file is called SheetsQuickstart.java, and is in src/main/java. I'm using intelliJ's sbt shell to run these commands. (Perhaps it's not reading the build.sbt file?)
In build.sbt, I've tried lines like:
excludeFilter in (Compile, managedSources) := "SheetsQuickstart.java"
excludeFilter in managedSources := "SheetsQuickstart.java"
excludeFilter in unmanagedSources := "SheetsQuickstart.java"
excludeFilter in managedSources := "SheetsQuickstart.class"
excludeFilter in managedSources := "*.java" `
I still get compilation errors in SheetsQuickstart.java when I compile using sbt, despite having tried to exclude it.

How to execute Main-Class as part of compile in SBT

I wanted to execute a task as part of SBT compile, I tried runMain in compile but it is not executing the main class that I am providing. Below is how task looks like in build.sbt
lazy val scalaGeneratorPlugin = Project("scala-generator", file("scala-generator"))
.settings(
libraryDependencies += "org.freemarker" % "freemarker" % "2.3.23",
runMain in compile := Some("com.my.MyMainClass")
)
I am running following command:
sbt scala-generator/compile
Although it gives me success message, it does not execute my MainClass
I am copying laughedelic's answer in comment here:
I think you should use source generation in sbt for that, i.e. there should be different compilation stages.

sbt console - set scala-version for all sub-projects

I have a multi-project build with scalaVersion := "2.11.8" for each module.
I want to run test and publish-local for Scala 2.12.0-RC1 without touching the build file. I thought the following would work:
$ sbt
> set scalaVersion in ThisBuild := "2.12.0-RC1"
> test
But that does not alter the Scala version used, sbt still compiles with Scala 2.11.8. This only works for single module builds (without project definitions).
How do I effectively override scalaVersion for all sub-modules from the sbt console?
Your attempt doesn't work because the setting for the module takes priority over the setting for ThisBuild; it would work if you used scalaVersion in ThisBuild in the build file instead of setting it separately for each module. I don't know if there is a way to do this with anything except scalaVersion, but:
As a final note, you can use ++ to temporarily switch the Scala version currently being used to build. should be either a version for Scala published to a repository, as in ++ 2.10.0 or the path to a Scala home directory, as in ++ /path/to/scala/home. See Command Line Reference for details.
> ++2.12.0-RC1
> test