Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I consume -D variables in build.scala using SBT?

Tags:

scala

sbt

I have a build.scala file that has a dependency that looks like this:

"com.example" % "core" % "2.0" classifier "full-unstable"

This pulls in a JAR with the classifier of full-unstable

What I need to do is specify either "unstable" or "stable" to SBT (using a -D I presume) from Jenkins (the build server) to change the classifier. If variable substitution worked like it does in Maven, the dependency would look like:

"com.example" % "core" % "2.0" classifier "full-${branch}"

And I would do "-Dbranch=unstable" or "-Dbranch=stable"

I'm very unclear how I do this with SBT and a build.scala file.

like image 540
Christopher Ambler Avatar asked Oct 16 '14 00:10

Christopher Ambler


People also ask

What is build sbt in Scala?

sbt is a popular tool for compiling, running, and testing Scala projects of any size. Using a build tool such as sbt (or Maven/Gradle) becomes essential once you create projects with dependencies or more than one code file.

What does sbt clean do?

clean – delete all generated sources, compiled artifacts, intermediate products, and generally all build-produced files. reload – reload the build, to take into account changes to the sbt plugin and its transitive dependencies.

What is the use of sbt in Scala?

sbt is an open-source build tool for Scala and Java projects, similar to Apache's Maven and Ant. Its main features are: Native support for compiling Scala code and integrating with many Scala test frameworks. Continuous compilation, testing, and deployment.


1 Answers

You can simply access sys.props: "A bidirectional, mutable Map representing the current system Properties." So, you can do something like this:

val branch = "full-" + sys.props.getOrElse("branch", "unstable")
"com.example" % "core" % "2.0" classifier branch

If you want to have more advanced custom properties from file in your Build.scala:

import java.io.{BufferedReader, InputStreamReader, FileInputStream, File}
import java.nio.charset.Charset
import java.util.Properties

object MyBuild extends Build {

  // updates system props (mutable map of props)
  loadSystemProperties("project/myproj.build.properties")

  def loadSystemProperties(fileName: String): Unit = {
    import scala.collection.JavaConverters._
    val file = new File(fileName)
    if (file.exists()) {
      println("Loading system properties from file `" + fileName + "`")
      val in = new InputStreamReader(new FileInputStream(file), "UTF-8")
      val props = new Properties
      props.load(in)
      in.close()
      sys.props ++ props.asScala
    }
  }

  // to test try:
  println(sys.props.getOrElse("branch", "unstable"))
}

SBT is more powerful than Maven because you can simply write Scala code if you need something very custom. You would want to use Build.scala instead of build.sbt in such case.

p.s myproj.build.properties file looks like this for example:

sbt.version=0.13.1

scalaVersion=2.10.4

parallelExecution=true

branch=stable
like image 121
yǝsʞǝla Avatar answered Oct 11 '22 10:10

yǝsʞǝla