Run tests before sbt dist in sbt-native-packager - sbt

I don't wanna create any deployable without running tests. Is there anyway in sbt-native-packager / sbt where I could run sbt test in inside sbt dist, which would fail when any test fails?

There are multiple ways to achieve this.
Create a command alias
This is my recommended approach as it is very explicit, easy to understand and extend. In your build.sbt add
addCommandAlias("buildDist", "; compile ; test ; dist")
Now you can call sbt buildDist
Depend on test
You can add dependencies between tasks. In your build.sbt
dist := (dist dependsOn test).value
This will add the test task as a dependency
Cheers,
Muki

Related

Migrate from activator 0.13.x to sbt 1.x

I am migrating from activator from 0.13.x to sbt 1.x.
I used to compile my modules like this $ activator clean compile publish-local -Dversion=1
Now, I am trying to do it with sbt since activator has been deprecated, but I can not find how I should migrate to something similar like $ sbt clean compile publish-local -Dversion=1?
Activator (the CLI part) was just a wrapper around sbt with some custom commands. So what you wrote should work the same, expect that the snake-case was deprecated in favor of the camelCase:
sbt clean compile publishLocal
If you need to pass a var to the Java runtime with -D you have to place it before any commands: sbt -Dversion=1 ....
Notice that you use batch mode to run the commands:
Running in batch mode requires JVM spinup and JIT each time, so your build will run much slower. For day-to-day coding, we recommend using the sbt shell or Continuous build and test feature described below.
To follow this recommendation, just run sbt and then enter those commands one by one. Or to run them all sequentially, enter ; clean; compile; publishLocal.

How to run sbt Revolver in test scope?

In a Akka project we're using the SBT Revolver plugin to run the application.
During development it would be useful if it would be possible to run the application in test scope so log- and application configuration get loaded which helps during development.
However, running 'sbt test:re-start' does not seems to use the test classpath and therefore does not run the correct application and does not use the correct configuration files.
Looking at the Revolver page it looks like the plugin creates it's own scope.
Does anyone know how to be able to use the test scope for running the Revolver plugin?
Try to configure the fullClasspath setting of revolver and add the Test classpath to it:
fullClasspath in Test in reStart <<= Classpaths.concatDistinct(fullClasspath in Test, fullClasspath in Runtime)

How to invoke docker:publishLocal as a test dependency in sbt

My project uses sbt-native-packager's Docker plugin to generate Docker containers. I'd like containerization to occur before running unit tests. (The command to do this is 'sbt docker:publishLocal')
How can I wire in my Build.scala file so that the test task in sbt will run docker:publishLocal first, before its normal test activities?
(Keys.test in Test) <<= (Keys.test in Test) dependsOn (publishLocal in Docker)

SBT Plugin to modify compile and test tasks

I am in the process of creating a plugin that will modify both the compile:compile and test:test tasks. My ultimate aim is to be able to do sbt monkjack or sbt monkjack:test (either is fine). In the compile:compile scope I need to add a compiler plugin, and in the test:test scope I need to run some code after the tests have finished.
My first attempts were around trying to create a custom configuration but which to extend, compile or test, was unclear as both are needed (At the moment I have two, and I copy the CustomTest into the CustomCompile and then run monkjack:test). My second attempts were focusing on a custom task that in turn invoked (compile in Compile).value and (test in Test).value after setting various options.
I realize my knowledge of SBT tasks and how they are related/inherited/scoped is not great.
Q1. Is there a chain of tasks like in maven? In maven if you execute test, it will execute the other phases in order. So mvn clean test will automatically run prepare-sources, compile, etc etc. So in SBT if I run sbt test how are the other tasks automatically executed.
Q2. If you execute a task with a custom config, eg sbt millertime:test will that config propagate to the other tasks that run. Eg, is this the same as sbt monkjack:compile monkjack:test or the same as sbt compile monkjack:test or neither :)
Q3. How do tasks know which is their default config? If I do sbt compile how does SBT know that means sbt compile:compile?
Q4. Which is the best way to go here, a custom configuration or a new task.

How to execute tests in a single project only in multi-module build?

I have a multi-module build, and would like to run tests for different sub-projects independently.
Is there a way to do this in sbt, e.g. if my multi-project build has a core and commons projects, I'd like to only run test in the commons project.
Run sbt commons/test. See detailed explanation in Scopes.
You may also use the combination of two commands from sbt - changing the current project using project and executing test afterwards.
sbt "project commons" test
You can also use
sbt "; project commons; test"
It you are running sbt in interactive mode:
> project commons
> test
You can switch back to core with:
> project core
Never mind, I came across this:
How to execute package for one submodule only on Jenkins?
sbt "project core" test
Another way to do this.
Get into SBT interactive mode
> sbt
sbt:core> commons / test
No need to switch between projects.
to run sbt test only for ONLY the submodules having added, modified on deleted files, if you use git:
while read -r proj ; do sbt "project $proj" test ; \
done < <(git status --porcelain | cut -c 3- | cut -d/ -f1
There is another way to have sbt open for a particular module.
Go to the location of the module directory and type in the "sbt" command.
sbt will then open in interactive mode for that module only

Resources