How can I use -D variables in build.scala using SBT?

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

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

This results in a JAR with a full instability classifier

What I need to do is specify “unstable” or “stable” for SBT (using -D, I suppose) from Jenkins (build server) to change the classifier. If variable expansion was performed in the same way as in Maven, the dependency would look like this:

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

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

I really don’t understand how to do this with SBT and the build.scala file.

+6
source share
1 answer

You can simply access sys.props : "Bidirectional, Modified Map representing 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 a file in 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 just write Scala code if you need something very ordinary. You would like to use Build.scala instead of build.sbt in this case.

ps myproj.build.properties file looks like this:

 sbt.version=0.13.1 scalaVersion=2.10.4 parallelExecution=true branch=stable 
+10
source

Source: https://habr.com/ru/post/976812/


All Articles