Simple Build Tool | Getting with SBT | Setting Up | Running

Reading Time: 5 minutes

Before setting up the SBT (Simple Build Tool) in your LINUX or UBUNTU. Let’s quickly go through the SBT and the Scala Language.

Scala ….What is it ?

Scala is an object-oriented, imperative , functional ,concurrent , strongly typed, static general purpose language. So, It is designed to concise, aimed to address criticism of Java.

SBT…. What is it ?

SBT is an initialism of Simple Build Tool which is an open source source build tool for Scala and Java projects, similar to Maven or Ant. It helps us in managing dependencies and plugins requires for any specific type or can download the right version of dependencies automatically.

Even, SBT has a native support for compiling SCALA code and integrating with different frameworks.

Prerequisites for SBT – JAVA

To install java, first update the package index using the following command

$ sudo apt update

Next, check if Java is already installed:

$ java -version

If Java has not yet been installed,then you’ll seeing the following output:

Execute the following command to install the default Java Runtime Environment (JRE), which will install the JRE from OpenJDK 11:

$ sudo apt install default-jre

The JRE will allow you to run almost all Java software. Verify the installation with:

$ java -version

You’ll see the following output:

Outputopenjdk version "11.0.7" 2020-04-14
OpenJDK Runtime Environment (build 11.0.7+10-post-Ubuntu-2ubuntu218.04)
OpenJDK 64-Bit Server VM (build 11.0.7+10-post-Ubuntu-2ubuntu218.04, mixed mode, sharing)

Installing the JDK (JAVA Development Kit)

It may possible that you need the Java Development Kit (JDK) in addition to the JRE in order to compile and run some specific Java-based software. To install the JDK, execute the following command, which will also install the JRE:

$ sudo apt install default-jdk

Verify that the JDK is installed by checking the version of javac, the Java compiler:

$ javac -version

You’ll see the following output:

Outputjavac 11.0.7

Looks, you have successfully installed Java and you can check the java path by following command :

$ whereis java

Now Let’s move to the installation of SBT which can be done in two ways

  1. By Downloading SBT and creating Directories by yourself (Recommended if you are “Egg Crack”) .
  2. By Downloading any IDE like IntelliJ and start working directly.

Here ,We’ll be discussing both the types one by one .

1. Installing SBT Setup in your UBUNTU or LINUX system

  • Download the SBT (Simple Build Tool) setup. Go to the following page https://www.scala-sbt.org/download.html
  • Select the latest and stable version, try with sbt-1.4.4
  • Download and extract the zip.
  • Set the path in .bashrc file. Open the .bashrc file and at the bottom of the file, add
    export PATH=<Folder Path>:$PATH example : export PATH=/home/gkuldeepak/Tools/sbt-1.4.4/bin:$PATH
  • Save and close the .bashrc file.

We have successfully installed the sbt in the system. Let’s create our first project with the sbt

  • Create directories using the following command

$ mkdir my_project

$ cd my_project

my_project $  mkdir -p src/{main,test}/{java,resources,scala}

  • Create  build.sbt file

name := “hello”

version := “1.0”

scalaVersion := “2.12.12”

  • Run your first sbt project using the command :

$ sbt run

It may take time as your running your project for first time. May possible that you’ll be getting a warning :

No main class found

Nothing to worry about, it’s just because you haven’t any java or Scala main class inside your project. Let’s quickly do the same. Get into the my_project/src/main/scala

Create a file named Hello.scala and add the following snippet into it.

object Hello {

def main(args: Array[String]) = {

println(“Welcome to SBT”)

}

}

run from console :

$ sbt run


2. Installing the sbt using IDE Intellij.

Run the following command in your terminal and IntelliJ will download and install into your system.

$ sudo snap install intellij-idea-ultimate –classic

Start your IntelliJ for first time and setup the sbt using the following steps;

Welcome Screen of IntelliJ Simple Build Tool
Welcome Screen of IntelliJ
Manage your Plugins in IntelliJ Simple Build Tool
Manage your Plugins in IntelliJ
Installing a new Plugin
Install a new Plugin
Searching for Scala Plugin Simple Build Tool
Search for Scala Plugin and Install it

Restart your IntelliJ
Restart your IntelliJ to reflect the changes

Understanding the Directory Structure with SBT :

Dirctory Simple Build Tool
  • my_project – Project definition files
  • src/main – your main code resides here
  • src/main/resources – the static files you want added to your jar
  • src/test – similar to src/main, but for tests
  • target – the destination for generated output

Managing Dependencies

Dependencies management are nothing just to support automatic dependencies to your project. Likewise in other build system, SBT too supports the library dependencies which are resolved automatically, thus you don’t need to worry about downloading and packaging the required library. In SBT, You can define the library dependency as –

libraryDependencies += groupID  %  artifactID  %  revision

Example: libraryDependencies += “mysql”  %  “mysql-connector-java”  %      “5.1.21”

And if you have a number of dependencies, you can define them sequentially as –

libraryDependencies ++= Seq( groupID  %  artifactID  %  revision , groupID  %  artifactID     %  Revision )

In SBT, even you can get the right Scala version by –

libraryDependencies +=  “org.scalatest”  %%  “scalatest”  %  “3.2.1”

Often a dependency is used by your test code at compile time but not by the main code . In such case you can use Per-Configuration Dependencies.

libraryDependencies += groupID  %  artifactID  %  revision % Test

Resolvers

By default, This Build Tool uses the Maven Repository for the dependencies but in case if your dependency is not one from the default repository, you can still add them to your project using the Resolvers. To add additional repository use Resolvers as

resolvers += name at location

Example : resolvers += “Sonatype OSS Snapshots”  at  “https://oss.sonatype.org/content/repositories/snapshots&#8221;

Plugins in SBT

You can define a sequence of SBT settings that are automatically added to all projects or that are explicitly declared for selected projects using Plugins.

You can declare a Plugin by –

addSbtPlugin(“org.scoverage” % “sbt-scoverage” % “1.6.1”)

Plugins can be declared in “plugins.sbt” inside the Project folder parallel to “build.properties”

Multi-Module SBT

However , It may possible that you your project is a multi module project. So, SBT has a nice feature that it supports the Multi-Module and can be written as

lazy val val_name = project.in(file(“module_name”)).settings(….<Add Dependencies>…..)

Running SBT

  • Interactive mode:

$ sbt

[info] welcome to sbt 1.4.4

compile

run

test

  • Batch mode

$ sbt clean compile

$ sbt clean compile test

References

https://www.scala-sbt.org/1.x/docs/

Written by 

Kuldeepak Gupta is a passionate software consultant at Knoldus Inc. Knoldus does niche Reactive and Big Data product development on Scala, Spark, and Functional Java. His current passions include utilizing the power of Scala, Akka, and Play to make Reactive and Big Data systems. He is a self-motivated, enthusiastic person who is recognized as a good team player, dedicated, responsible professional, and a technology enthusiast. His hobbies include playing hockey, participating in Political debates, Reading Tech blogs, and listening to songs.

3 thoughts on “Simple Build Tool | Getting with SBT | Setting Up | Running7 min read

Comments are closed.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading