scalabeginner

SBT Build Configuration

Configure Scala projects with SBT: dependencies, plugins, multi-project builds, and custom tasks.

scala
// build.sbt - Main build file

// Project settings
lazy val root = (project in file("."))
  .settings(
    name := "my-scala-app",
    version := "0.1.0",
    scalaVersion := "3.3.1",
    organization := "com.example",

    // Compiler options
    scalacOptions ++= Seq(
      "-deprecation",
      "-feature",
      "-unchecked",
      "-Xfatal-warnings",
      "-explain"
    ),

    // Dependencies
    libraryDependencies ++= Seq(
      // Core
      "org.typelevel"   %% "cats-core"    % "2.10.0",
      "org.typelevel"   %% "cats-effect"  % "3.5.2",

      // JSON
      "io.circe"        %% "circe-core"    % "0.14.6",
      "io.circe"        %% "circe-generic" % "0.14.6",
      "io.circe"        %% "circe-parser"  % "0.14.6",

      // HTTP
      "com.softwaremill.sttp.client3" %% "core" % "3.9.1",
      "com.softwaremill.sttp.client3" %% "circe" % "3.9.1",

      // Database
      "org.tpolecat"    %% "doobie-core"    % "1.0.0-RC4",
      "org.tpolecat"    %% "doobie-hikari"  % "1.0.0-RC4",
      "org.tpolecat"    %% "doobie-postgres" % "1.0.0-RC4",

      // Config
      "com.typesafe"     % "typesafe-config" % "1.4.3",

      // Logging
      "ch.qos.logback"   % "logback-classic" % "1.4.11",

      // Testing
      "org.scalatest"   %% "scalatest"       % "3.2.17"  % Test,
      "org.scalacheck"  %% "scalacheck"      % "1.17.0"  % Test,
      "org.typelevel"   %% "cats-effect-testing-scalatest" % "1.5.0" % Test
    ),

    // Test settings
    Test / fork := true,
    Test / parallelExecution := true,

    // Assembly settings
    assembly / mainClass := Some("com.example.Main"),
    assembly / assemblyMergeStrategy := {
      case PathList("META-INF", _*) => MergeStrategy.discard
      case _ => MergeStrategy.first
    }
  )

// Multi-project build
lazy val core = (project in file("core"))
  .settings(
    name := "core",
    libraryDependencies ++= Seq(
      "org.typelevel" %% "cats-core" % "2.10.0"
    )
  )

lazy val api = (project in file("api"))
  .dependsOn(core)
  .settings(
    name := "api",
    libraryDependencies ++= Seq(
      "com.softwaremill.sttp.tapir" %% "tapir-core" % "1.9.2"
    )
  )

// project/plugins.sbt:
// addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "2.1.4")
// addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.2")
// addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.11.1")
// addSbtPlugin("com.github.sbt" % "sbt-native-packager" % "1.9.16")

// Common SBT commands:
// sbt compile          - Compile
// sbt test             - Run tests
// sbt run              - Run main class
// sbt assembly          - Create fat JAR
// sbt clean compile    - Clean and compile
// sbt "testOnly *Spec" - Run specific test
// sbt console          - Open REPL with project classpath
// sbt dependencyTree   - Show dependency tree
// sbt reload           - Reload build definition

Use Cases

  • Scala project setup
  • Dependency management
  • Multi-module build configuration

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.