Saturday 13 June 2015

Steps to create Scala project using Maven / Sbt and import in Eclipse ScalaIDE

If you are Java developer and accustomed to use Eclipse IDE, you can consider below steps for getting started with creating new Scala project.

Prerequisites

You can create Scala project structure using your choice of build tool - Maven / SBT (scala's simple build tool) - as per below steps. Then import the existing Scala project in Scala IDE for getting started with Scala programming.

Create Scala project with Maven

If you already know Maven, then below are quick steps to get you started with building and running your Scala applications using Maven.

First of all, install "m2eclipse-scala" plugin in Scala IDE. Then go with one of below options.

Option 1: Create project using Scala IDE by following instructions given @ http://scala-ide.org/docs/tutorials/m2eclipse/

Option 2: Create project by executing maven commands as below (Reference @ https://github.com/davidB/scala-archetype-simple).

(1) Generate "scala-mvn-helloworld" project structure using below Mavan command. Ensure to change group id, artifact id and package as per your need. Once you execute this command, it would create directory structure including "pom.xml" file.

D:\Tirthal-LABs\Learning-Scala>mvn archetype:generate -B -DarchetypeGroupId=net.alchim31.maven -DarchetypeArtifactId=scala-archetype-simple -DarchetypeVersion=1.5 -DgroupId=com.tirthal.learning.scala -DartifactId=scala-mvn-helloworld -Dversion=0.1-SNAPSHOT -Dpackage=com.tirthal.learning.scala

(2) Execute maven command to compile scala --- mvn scala:compile

(3) Execute maven command to run and ensure correct package name, which should give "Hello World!" output towards the bottom--- mvn scala:run -DmainClass=com.tirthal.learning.scala.App

(4) Import maven project in Scala IDE using  --- File -> Import -> Maven -> Existing Maven Projects. Then run "App.scala" by right click option--- Run As -> Scala Application.


Create scala project with Sbt (simple build tool)

If you want to try Sbt (interactive build tool which has build-in defaults - compile, test, run and many more…), then below are quick steps to get you started with building and running your Scala applications using Sbt.

First of all, install sbt and add "D:\Softwares\scala\sbt-0.13.7\bin" in path variable of OS. Next,

(1) Create application folder --- D:\Tirthal-LABs\Learning-Scala> mkdir scala-sbt-helloworld

(2) Create necessary sbt files in application folder using below commands  in Windows (or you may create those files manually) --- D:\Tirthal-LABs\Learning-Scala\scala-sbt-helloworld>

(2.1) Run this command and add below three lines in build.sbt file --- touch build.sbt && start build.sbt    

name := "scala-sbt-helloworld"
version := "0.1"
scalaVersion := "2.11.6"

Note:  Here "scala-sbt-helloworld" name will be used for Eclipse project name, while we'll execute "sbt eclipse" command further to create project scaffolding. Also ensure to mention correct Scala version which you have installed.

(2.2) Create "project/build.properties" file by running this command and add below one line in it --- mkdir project && touch project\build.properties && start project\build.properties

sbt.version = 0.13.7

Note: Here folder name must be "project", which you cannot change to different name. Also ensure to mention correct Sbt version which you have installed.

(2.3) Create "project\plugins.sbt" file by running this command and add below line in it --- touch project\plugins.sbt && start project\plugins.sbt

addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "3.0.0")

Note: This internally using Apache Ivy - group name % artifact ID % version. Also ensure to mention correct version compatible with your sbt version as given here - https://github.com/typesafehub/sbteclipse/.

(3) Now run this "sbt eclipse" command to create default eclipse project scaffolding ---  D:\Tirthal-LABs\Learning-Scala\scala-sbt-helloworld> sbt eclipse

Note: You should see below lines on command prompt, on successful project creation.
[info] Updating {file: /D:/Tirthal-LABs/Learning-Scala/scala-sbt-helloworld/ }scala-sbt-helloworld...
[info] Resolving org.scala-lang#scala-library;2.11.6 ...
  [info] Resolving org.scala-lang#scala-compiler;2.11.6 ...
  [info] Resolving org.scala-lang#scala-reflect;2.11.6 ...
  [info] Resolving org.scala-lang.modules#scala-xml_2.11;1.0.3 ...
  [info] Resolving org.scala-lang.modules#scala-parser-combinators_2.11;1.0.3 ...
  [info] Resolving jline#jline;2.12.1 ...
[info] Done updating.
[info] Successfully created Eclipse project files for project(s):
[info] scala-sbt-helloworld

(4) Finally import maven project in Scala IDE using  --- File -> Import -> General -> Existing Projects into Workspace option. Then, under the folder "src/main/scala" create "App.scala" hello world scala program and run it by right click option--- Run As -> Scala Application.

object App {
  def main(args : Array[String]) {
    println( "Hello World!" ) 
  }
}

(5) Optional extra steps --- As per your project need, you can add more SBT plugins - https://github.com/sbt. For example, to add dependency in project for test add "libraryDependencies" in build.sbt file and run sbt, eclipse commands…

D:\Tirthal-LABs\Learning-Scala\scala-sbt-helloworld> start build.sbt
libraryDependencies ++= Seq("org.scalatest" % "scalatest_2.11" % "2.2.4" %"test", "com.novocode" % "junit-interface" % "0.11" % "test");

> sbt (this will load sbt)

> eclipse (run eclipse task to incorporate dependencies in Eclipse project configuration)

> ~test (this will compile, run the test and show result. it will rerun again, when any change is done in the test source code)
[info] Compiling 1 Scala source to D:\Tirthal-LABs\Learning-Scala\scala-sbt-helloworld\target\scala-2.11\classes...
[info] ScalaTest
[info] Run completed in 151 milliseconds.
[info] Total number of tests run: 0
[info] Suites: completed 0, aborted 0
[info] Tests: succeeded 0, failed 0, canceled 0, ignored 0, pending 0
[info] No tests were executed.