SBT fundamentals

To install the SBT in the Mac environment

brew install sbt@1

Say you have a following run.scala file in your directory,

package world
object Hello extends App {
  print("Hello World")
}

now start the sbt and run the compile command first, after that

runMain world.Hello

Now you can have a build.sbt file as follows

name := "Greetings"
organization := "ojitha.blogspot.com.au"
version := "1.0.0"

To show resolvers

show resolvers

To show dependencies

show libraryDependencies

To publish locally

publishLocal

You can add the dependencies as follows to the build.sbt

name := "Greetings"
organization := "ojitha.blogspot.com.au"
version := "1.0.0-SNAPSHOT"

resolvers += "Sonatype releases repo" at "https://oss.sonatype.org/content/repositories/Releases"

{
    val liftVersion = "3.1.0"
    libraryDependencies ++= List(
        "net.liftweb" %% "lift-util" % liftVersion,
        "net.liftweb" %% "lift-json" % liftVersion
    )
}

publishMavenStyle := true
pomExtra := <issue>1234</issue>

Multi Project SBT

In the root directory say you have two modules such as hello and the hello-runner: which are folders. In addition to that in the root you have to have root build file build.sbt which reference to the module sbt files.

name := "example1"
organization in ThisBuild:= "ojitha.blogspot.com.au"
version in ThisBuild := "1.0.0-SNAPSHOT"

resolvers += 
  "Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots"


lazy val hello =  project
lazy val helloRunner = (project in file("hello-runner")).dependsOn(hello)

As shwon in the above source last two lines, both multi projects are listed and created the dependency as well. For examp,e helloRunner is depends on hello project.

name := "Hello Library"
version := "2.0.1-SNAPSHOT"

libraryDependencies ++= Seq(
    "ch.qos.logback" % "logback-classic" % "1.2.3",
    "com.typesafe.scala-logging" %% "scala-logging" % "3.9.0"
)

As shown in the above, you can add dependencies. Here the logging dependencies.

Here the other module dependency:

name := "Hello Runner"

Both the modules has package structure as src/main/scal/hello. Here the existing file in the hello module:

package hello

import com.typesafe.scalalogging.Logger

class Hello {
  val logger = Logger(classOf[Hello])
  def greet() = {
    logger.debug("Show logging")
  }
}

Here is the exiting file in the module hello-runner:

import com.typesafe.scalalogging.Logger
import hello.Hello
object Run extends App {
  val logger = Logger("main")
  logger.debug("Show logging")

  val myObj = new Hello()
  myObj.greet()
    
}

The dependency com.typesafe.scalalogging.Loggerhas been added in the hello.sbt and the resolver is available in the root sbt build file. This project you can import to the IntelliJ idea CE simply.

These are the notes created from the video tutorial1. I would like to recommend this for beginners who want more details.

Reference


  1. Scala Beginner Programming Recipes, by Antonio Salazar Cardozo, Published by Packt, Publishing, 2017 

Comments

Popular posts from this blog

How To: GitHub projects in Spring Tool Suite

Spring 3 Part 7: Spring with Databases

Parse the namespace based XML using Python