Responsive Ads Here

Wednesday, May 30, 2018

Scala: Create SBT Project



SBT stands for Scala Build Tool used to manage build and dependencies for scala project. I will explain how to create an scala project.
Create Directory Structure:
1.       Create project directory, says “hello_scala”
2.       Create directory structure “hello_scala/src/main/scala”
3.       Create directory structure “hello_scala/src/main/resources”
4.       Create directory structure “hello_scala/src/test/scala”
5.       Create directory structure “hello_scala/src/test/resources”

Create build.sbt:
              Create build.sbt in project home directory “says hello_scala”
              Edit build.sbt and add following content
                   name:="hello_scala"
version:="0.0.0"
scalaVersion:="2.12.2"

libraryDependencies ++= Seq(
    "junit" % "junit" % "4.12" % Test
)
Create source file:
              Create a package com.example
              Create scala class as HelloWorld.scala with following content
                             package com.example

object HelloWorld {
  def main(args: Array[String]): Unit = {
    println("Hello World")
  }
}
SBT commands:
  • sbt: Command is used to open sbt console.
  • clean: It will delete all generated files.
  • compile: Compiles the main sources (in {project_name}/src/main/scala and {project_name}/src/main/java directories).
  • test: Compiles and runs all tests.
  • run:  Runs the main class for the project.
  • package: Creates a jar file containing the files in {project_name}/src/main/resources and the classes compiled from {project_name}/src/main/scala and {project_name}/src/main/java.
  • help: Displays detailed help for the specified command. Only help without any command will display brief descriptions of all commands.
  • reload Reloads the build definition (build.sbt, project/*.scala, project/*.sbt files). Needed if you change the build definition.
Import Project in eclipse:  We can import our project in eclipse to make development easy. We need to add a plugin to sbt to make our project eclipse compatible.
Option-1: Go to { user home }/.sbt/{version}/plugins. If plugins directory is not there, please create one.
Create a file plugins.sbt and add addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "5.2.4") line. This will add plugin globally, any sbt plugin can use it.
Option-2: Create plugins directory in your project directory.
Create plugins.sbt file inside it and add addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "5.2.4") line.
After adding eclipse plugin, open command prompt into you project home directory and type “sbt eclipse”. It will download all required dependencies and will make your project eclipse compatible on successful run.
Now, open your eclipse ide, go file -> import -> General -> Existing Project into Workspace, click next, Select your project home directory and Finish.
Your project is ready for development in eclipse ide.

For any further query please give you comments in comment box.

No comments:

Post a Comment