Send module version as command line argument to SBT - scala

I am using TeamCity to run a bash script that is utilizing SBT Native Packager to publish an image to Docker. The sbt portion of the bash script looks something like this:
sbt -DdockerRepository=$repo -DpackageName=$packageName -D myproject/docker:publish
I want to pass on the TeamCity build number as a version number to my package. Today I specify the version number manually in settings in build.sbt:
settings(
version := "0.20",
....,
dockerBaseImage := "example.com:5000/linux/java8:latest",
dockerRepository in Docker := Some("example.com/myoldrepo"),
dockerUpdateLatest := true'
)
I want to be able to do it like this:
activator -Dversion=0.21 -DpackageName=myproject -D myproject/docker:publish
but this does not seem to work. Yet overriding the dockerRepository like I do above is working.
How can I pass my desired version number into SBT from the command line/TeamCity?

You could set version before publish:
sbt 'set version := "1.0"' docker:publish

Try something like this:
val myVersion = util.Properties.propOrNone("version").getOrElse("0.20")
val myDockerBaseImage = util.Properties.propOrNone("dockerBaseImage").
getOrElse("example.com:5000/linux/java8:latest")
lazy val myProject = Project("myProject",file("path")).settings(
version := myVersion,
dockerBaseImage := myDockerBaseImage,
....,
dockerRepository in Docker := Some("example.com/myoldrepo"),
dockerUpdateLatest := true
)
And then call it (depends on your sbt installation):
SBT_OPTS="-Dversion=0.21" sbt
sbt -Dversion=0.21
activator -Dversion=0.21

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.

Is there a way to override packageName in sbt while building dist?

I have an sbt project. I have defined the packageName as follows in the build.sbt
packageName in Universal := "project"
Is there a way to override packageName when we do sbt dist in commmand line?
Something like:
sbt 'set packageName := "newName"' publish # or
sbt 'set packageName in Universal := "newName"' publish
?
Custom commands can be used to modify the build state like so
commands += Command.command("distWithPackageNameOverride") { state =>
"""set packageName in Universal := "foo"""" :: "dist" :: state
}
where executing sbt distWithPackageNameOverride should output foo.zip under
yourapp/target/universal/foo.zip

In sbt, how to make a setting value scoped by a task?

I'm using sbt 0.13.16 and I'm trying to have a the setting version having a different value depending of the task using it. I'm trying to add the postfix -SNAPSHOT with publishLocal
I tried to scope the value
version := appVersion
version in publishLocal := appVersion + "-SNAPSHOT"
or even to create a new config (but I would prefer a solution not involving a new configuration)
val Dev = config("dev") extend Compile
version := appVersion
version in Dev := appVersion + "-SNAPSHOT"
In the second case, typing dev:version in the sbt repl does output a version correctly postfixed but dev:publishLocal still create artifacts that are not postfixed.

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

how to configure 2.9.1 in org.scala-sbt#sbt_2.9.1;0.12.3

I am trying a very simple sbt example; when I compile it with sbt, and always get the following error:
org.scala-sbt#sbt_2.9.1;0.12.3: not found
I found a build.properties file under project folder, where I could change the 0.12.3 part; for example, after I changed it to 0.11.3, it will succeed until another inompatible issue; However, I want to know how to change sbt_2.9.1 to, say, sbt_2.9.2; I don't find a configuration file, and even I update the sbt to the latest version 0.12.3, still no luck.
my build.sbt file:
organization := "com.typesafe.slick"
name := "slick-examples"
version := "1.0.1-RC1"
scalaVersion := "2.10.1"
scalacOptions += "-deprecation"
anyone please help me.
The Scala version sbt is using internally and the one used for your project are totally independent. Which sbt launcher are you using? Make sure you are using an sbt.version property that works for your sbt launcher.
Again, no need to configure the Scala version for your project at that level. Write a build.sbt file and set scalaVersion to, e.g. 2.10.1 (assuming you use sbt 0.12.x): scalaVersion := 2.10.1