SBT, Scala, and “Hello, World!”

I am learning to program in Scala these days and plan to put up some random posts about it here.  Here I want to put up the instructions about how I installed Scala, through the Scala Build Tool (SBT), and how I got the hello world program working.  I used CentOS as the operating system.  So you might want to check parallel instructions for your specific operating system.

Installing SBT

I used the RPM packages to install SBT.  Briefly, the steps were as below.

$ curl https://bintray.com/sbt/rpm/rpm | sudo tee /etc/yum.repos.d/bintray-sbt-rpm.repo
$ sudo yum install sbt

If it went well SBT should be installed at /usr/bin/sbt.  You can check the version of SBT installed using sbt about.

$ curl which sbt
/usr/bin/sbt
$ sbt about
[info] Set current project to hariharan (in build file:/home/hariharan/)
[info] This is sbt 0.13.12
[info] The current project is {file:/home/hariharan/}hariharan 0.1-SNAPSHOT
[info] The current project is built against Scala 2.10.6
[info] Available Plugins: sbt.plugins.IvyPlugin, sbt.plugins.JvmPlugin, sbt.plugins.CorePlugin, sbt.plugins.JUnitXmlReportPlugin
[info] sbt, sbt plugins, and build definitions are using Scala 2.10.6

The Hello, World Program in Scala

Let us create a quick and simple “Hello, World” program here.  A more formal program with the recommended directory structure comes in the next section.

$ mkdir hello
$ cd hello
$ vi HelloWorld.scala 

Write the hello world code in this file.

object HelloWorld {
  def main(args: Array[String]) = println("Hello, World!!")
}
~
~

The command sbt compile will compile the code and sbt run will run it.

$ sbt compile
[info] Set current project to hello (in build file:/home/hariharan/work/training/scala/hello/)
[info] Updating {file:/home/hariharan/work/training/scala/hello/}hello...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Compiling 1 Scala source to /home/hariharan/work/training/scala/hello/target/scala-2.10/classes...
[success] Total time: 132 s, completed 15 Oct, 2016 10:51:30 AM

The command sbt run will execute the hello world program.

$ sbt run
[info] Set current project to hello (in build file:/home/hariharan/work/training/scala/hello/)
[info] Running HelloWorld 
Hello, World!!
[success] Total time: 1 s, completed 15 Oct, 2016 10:51:55 AM

Restructuring into the Correct Scala Directory Structure

In real-world Scala projects, the source code would be arranged in a more structured format than placing all files in a single folder.  If not other folder structure is defined, SBT assumes the conventional folder structure as recommended by Maven.  Let us now re-arrange the earlier hwllo-world code into the conventional form expected by SBT.  This should look as below:

hello
|-- build.sbt                      # Build definition for the project
|-- src                            # all source files go here
|   |-- main                       # production source files and resources go under this folder
|   |   |-- resources
|   |   `-- scala                  # Scala source files go here
|   |       `-- HelloWorld.scala
|   `-- test                       # test source files and resources go under this folder
|       |-- resources
|       `-- scala                  # Scala test files go here
|-- target                         # all compiled and downloaded binaries go here

Now build and run again

$ sbt compile
[info] Set current project to hello (in build file:/home/hariharan/work/training/scala/hello/)
[info] Compiling 1 Scala source to /home/hariharan/work/training/scala/hello/target/scala-2.11/classes...
[info] 'compiler-interface' not yet compiled for Scala 2.11.8. Compiling...
[info]   Compilation completed in 18.884 s
[success] Total time: 150 s, completed 15 Oct, 2016 11:43:38 AM
$ sbt run
[info] Set current project to hello (in build file:/home/hariharan/work/training/scala/hello/)
[info] Running HelloWorld 
Hello, World!!
[success] Total time: 1 s, completed 15 Oct, 2016 11:44:05 AM

Set up Version Control

We have the basic Hello world program working in the right folder structure.  Let us commit this into a Git repository now.

First, move to the root folder of the project.  Then create a .gitignore file so that git does not track the target/ folder tree.  Finally initialize a git repo and commit all files.

$ echo 'target/' > .gitignore
$ git init
Initialized empty Git repository in /home/hariharan/work/training/scala/hello/.git/
$ git add .
$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#    new file:   .gitignore
#    new file:   build.sbt
#    new file:   src/main/scala/HelloWorld.scala
#
$ git commit -m "First commit of the hello world project in Scala"
[master (root-commit) ed1500f] First commit of the hello world project in Scala
3 files changed, 11 insertions(+)
create mode 100644 .gitignore
create mode 100644 build.sbt
create mode 100644 src/main/scala/HelloWorld.scala

 

Advertisements