Scala build tools (sbt) : Getting started for testing and building
As we know Scala as language is getting popular in data science or general programming field, for its functional programming traits as well object oriented behaviour also Scala runs on JVM so we can mix Scala code in java or java code with Scala project also lot of plugin or java dependency can be use in Scala without much effort. so now we shall see how to create project and build project and unit test our Scala application using `sbt`.
Follow this tutorial to install Scala and SBT.
build.sbt
scalaVersion := "2.11.4"
name := "test"
organization := "com.ashrithgn.app"
version := "1.0"
mainClass in (Compile, packageBin) := Some("com.ashrithgn.app.test.TestApp")
mainClass in (Compile, run) := Some("com.ashrithgn.app.test.TestApp")
resourceDirectory in Compile := file(".") / "./src/main/resources"
resourceDirectory in Runtime := file(".") / "./src/main/resources"
libraryDependencies ++= Seq(
"org.scala-lang" % "scala-library" % "2.13.1",
"org.scala-lang" % "scala-library" % "2.11.4",
"org.scalatest" %% "scalatest" % "3.0.8" % "test"
)
So the first line starts with `scalaVersion` specify the version of the scala code runner available in your system for my system i am using `scalaVersion := "2.11.4"` to find your scala version type `scala -version` in your terminal.
name := "test"
organization := "com.ashrithgn.app"
version := "1.0"
So the above snippet is your project specific just like we specify in maven `:=` is assignment like `==` so sbt have overloaded the operator for assignment.
To specify the main class `mainClass in (Compile, packageBin) := Some("com.ashrithgn.app.test.TestApp")` this main class will be available during compile time and as well as packaging. so i have repeated same for run time also in build.sbt file.
resourceDirectory in Compile := file(".") / "./src/main/resources" will specify the resource directory where we can store properties and other resources. which are required.
libraryDependencies is used for importing the dependency required for our application.
Sbt Commands
- Package
sbt clean package
2. Reload
sbt reload
this use when dependency or build mechanism has changed.
3. Run
sbt run
To run the application using sbt command line
4. Running Test
sbt test
so when we write test case test should be available in `src/test/scala/` and will execute all the tests