Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Useful SBT plugins

Useful SBT plugins

Alexey Novakov

April 30, 2020
Tweet

More Decks by Alexey Novakov

Other Decks in Programming

Transcript

  1. SBT PLUGINS
    Alexey Novakov, Ultra Tendency

    View Slide

  2. ABOUT ME
    ➤ Solution Architect at Ultra Tendency,
    Germany
    ➤ Experience:
    ➤ Scala - 5yrs.
    ➤ Java - 10 yrs.
    ➤ Big Data, Kubernetes
    ➤ My interests:
    ➤ 8 string guitars
    ➤ Astronomy
    ➤ FP
    ➤ Rust

    View Slide

  3. Scala Build Tool
    Download: https://www.scala-sbt.org/download.html
    Alexey Novakov, Ultra Tendency, 2020

    View Slide

  4. SBT Tasks (commands)
    sbt clean - deletes previously built files
    sbt compile - compiles source code
    sbt test - run tests, if any exists
    sbt run - runs detected “Main” class
    sbt package - makes jar file
    … user can define new tasks in *.sbt files Alexey Novakov, Ultra Tendency, 2020

    View Slide

  5. Scala IDEs
    IntelliJ + Scala Plugin VSCode + Metals (Scala LSP)
    Alexey Novakov, Ultra Tendency, 2020

    View Slide

  6. Build Definition
    build.sbt:
    Alexey Novakov, Ultra Tendency, 2020
    import Dependencies._
    ThisBuild / scalaVersion := "2.13.1"
    ThisBuild / version := "0.1.0-SNAPSHOT"
    ThisBuild / organization := "com.example"
    ThisBuild / organizationName := "example"
    lazy val root = (project in file("."))
    .settings(
    name := "akka-http-api",
    libraryDependencies += scalaTest % Test
    )
    Scope: build level, default for all sub-projects

    View Slide

  7. Managed Dependencies
    import Dependencies._
    lazy val root = (project in file("."))
    .settings(
    libraryDependencies += scalaTest % Test
    )
    import sbt._
    object Dependencies {
    lazy val scalaTest =
    "org.scalatest" %% "scalatest" % “3.0.8"
    }
    Alexey Novakov, Ultra Tendency, 2020
    build.sbt
    project/
    Dependencies.scala

    View Slide

  8. libraryDependencies +=
    “org.scalatest" %% "scalatest" % "3.0.8" % Test
    groupID % artifactID % revision % configuration
    "org.scalatest" % “scalatest_2.13” % "3.0.8" % Test
    %% - matches current build Scala version
    Alexey Novakov, Ultra Tendency, 2020

    View Slide

  9. - SBT is using Coursier to download Ivy or Maven dependencies
    Coursier - Pure Scala Artifact Fetching
    Alexey Novakov, Ultra Tendency, 2020

    View Slide

  10. SBT Plugins

    View Slide

  11. 1. Giter8

    View Slide

  12. Giter8 allows to create your own project template and host on GitHub:
    sbt new novakov-alexey/scalaboot.g8
    —> https://github.com/novakov-alexey/scalaboot.g8
    Alexey Novakov, Ultra Tendency, 2020

    View Slide

  13. Project Template

    View Slide

  14. ThisBuild / organization := "com.example"
    ThisBuild / scalaVersion := "$scalaVersion$"
    ThisBuild / description := "$desc$"
    lazy val root = (project in file(".")).
    settings(
    name := "$name;format="lower,hyphen"$",
    libraryDependencies ++= Seq(
    akkaHttp,
    akkaStreams,
    scalaLogging,
    logback,

    parameters

    View Slide

  15. name=Project Name
    desc=Describe your project a bit
    scalaVersion=2.13.1
    src/main/g8/default.properties
    [info] Loading settings for project global-plugins from idea.sbt,gpg.sbt ...
    [info] Loading global plugins from /Users/alexey/.sbt/1.0/plugins
    [info] Set current project to git (in build file:/Users/alexey/dev/git/)
    name [Project Name]: app-1
    desc [Describe your project a bit]: order registration
    scalaVersion [2.13.1]: 2.13.2
    Template applied in /Users/alexey/dev/git/./app-1

    View Slide

  16. 2. sbt-revolver
    - restarts running application automatically, if source files are modified
    addSbtPlugin("io.spray" % "sbt-revolver" % “x.y.z")
    sbt “~reStart”
    - enables a super-fast development turnaround for your Scala applications
    Alexey Novakov, Ultra Tendency, 2020

    View Slide

  17. View Slide

  18. Forked JVM
    Alexey Novakov, Ultra Tendency, 2020

    View Slide

  19. configuration
    javaOptions in reStart += "-Xmx2g"
    mainClass in reStart := Some("com.example.Main")
    Revolver.enableDebugging(port = 5050, suspend = true)
    envVars in reStart := Map(“K8S_NAMESPACE" -> “test")
    Alexey Novakov, Ultra Tendency, 2020

    View Slide

  20. First start
    New start
    Alexey Novakov, Ultra Tendency, 2020

    View Slide

  21. • sbt-revolver -> SBT Triggered Execution
    > ~ testQuick
    > ~ testOnly org.alexeyn.SomeTest
    > ~ test
    > ~ clean; test
    ~tilde
    Alexey Novakov, Ultra Tendency, 2020

    View Slide

  22. 3. sbt-tpolecat
    addSbtPlugin(
    “io.github.davidgregory084" % "sbt-tpolecat" % “0.1.10"
    )
    - enables scalac options according to Rob Noris (doobie) recommendations
    - P.S. the same options can be enabled manually, but why bother
    Alexey Novakov, Ultra Tendency, 2020

    View Slide

  23. scalacOptions ++= Seq(
    "-deprecation",
    "-encoding", "utf-8",
    "-explaintypes",
    "-language:higherKinds",
    "-language:implicitConversions",
    "-unchecked",
    "-Xfatal-warnings",
    "-Xlint:infer-any",
    "-Ywarn-dead-code",
    "-Ywarn-extra-implicit",
    "-Ywarn-inaccessible",
    "-Ywarn-infer-any",
    "-Ywarn-numeric-widen",
    "-Ywarn-unused:implicits",
    "-Ywarn-unused:imports",
    "-Ywarn-unused:locals",
    "-Ywarn-unused:params",
    "-Ywarn-unused:patvars",
    "-Ywarn-unused:privates",
    "-Ywarn-value-discard"

    )
    ~54 Scala Compiler Options
    Enabled Automatically
    Alexey Novakov, Ultra Tendency, 2020

    View Slide

  24. 4. sbt-native-packager
    lets you build application packages in native formats:
    •universal zip, tar.gz, xz archives
    •deb and rpm packages
    •dmg
    •msi
    •docker images
    •graalvm native images
    addSbtPlugin("com.typesafe.sbt" %% "sbt-native-packager" % “x.y.z")
    Alexey Novakov, Ultra Tendency, 2020

    View Slide

  25. usage
    lazy val root = (project in file(".")).
    settings(
    name := "exchange",
    ….
    dockerBaseImage := “openjdk:8-jre-alpine”,
    dockerExposedPorts ++= Seq(8080),
    dockerRepository := Some(“alexeyn")
    ).enablePlugins(AshScriptPlugin)
    // DockerPlugin, JavaAppPackaging

    View Slide

  26. sbt universal:packageBin
    Linux and Windows
    shell scripts
    universal zip archive
    Alexey Novakov, Ultra Tendency, 2020

    View Slide

  27. docker image
    sbt docker:stage sbt docker:publish
    preview docker build / push

    View Slide

  28. [info] * Cmd(FROM,WrappedArray(openjdk:8, as, stage0))
    [info] * Cmd(LABEL,WrappedArray(snp-multi-stage="intermediate"))
    [info] * Cmd(LABEL,WrappedArray(snp-multi-stage-id="b8437d6f-af0a-459c-ae51-cd3b9c5b7404"))
    [info] * Cmd(WORKDIR,WrappedArray(/opt/docker))
    [info] * Cmd(COPY,WrappedArray(opt /opt))
    [info] * Cmd(USER,WrappedArray(root))
    [info] * ExecCmd(RUN,List(chmod, -R, u=rX,g=rX, /opt/docker))
    [info] * ExecCmd(RUN,List(chmod, u+x,g+x, /opt/docker/bin/exchange))
    dockerCommands := Seq(
    Cmd("FROM", "openjdk:8"),
    Cmd("LABEL", s"""MAINTAINER="${maintainer.value}""""),
    ExecCmd("CMD", "echo", "Hello, World from Docker")
    )

    View Slide

  29. 5. sbt-release
    addSbtPlugin(
    "com.github.gseitz" % "sbt-release" % “1.0.12"
    )
    provides customisable release process …
    Alexey Novakov, Ultra Tendency, 2020

    View Slide

  30. Git
    artifacts tag
    increment
    version
    publish
    run tests
    build
    release

    View Slide

  31. releaseProcess := Seq[ReleaseStep](
    checkSnapshotDependencies,
    inquireVersions,
    runTest,
    setReleaseVersion,
    commitReleaseVersion,
    tagRelease,
    publishArtifacts,
    inquireVersions,
    setNextVersion,
    commitNextVersion,
    pushChanges
    )
    Default list of steps:
    sbt 'release with-defaults'
    includes

    View Slide

  32. releaseProcess := Seq[ReleaseStep](
    checkSnapshotDependencies,
    inquireVersions,
    runTest,
    setReleaseVersion,
    commitReleaseVersion,
    tagRelease,
    publishArtifacts,
    inquireVersions,
    setNextVersion,
    commitNextVersion,
    pushChanges
    )
    Alexey Novakov, Ultra Tendency, 2020
    $PROJECT_ROOT/version.sbt
    version in ThisBuild := "0.1.1-SNAPSHOT"
    version in ThisBuild := “0.1.1"
    version in ThisBuild := “0.1.2-SNAPSHOT"
    build.sbt:
    sbt 'release with-defaults'
    1
    2
    3

    View Slide

  33. commit 99b1094dce14bf99b6f38a8ff9870edaf7c728d3 (HEAD -> master, origin/master)
    Date: Fri Feb 7 09:20:03 2020 +0100
    Setting version to 0.1.2-SNAPSHOT
    commit cb9ec293a11a5f6d989c936b18922d3f3ec40bcd (tag: v0.2.2)
    Date: Fri Feb 7 09:16:59 2020 +0100
    Setting version to 0.1.1
    commit 63abea7141901419ad732d354dc703f884e53010
    Merge: b180810 a1c0c14
    Date: Fri Feb 7 08:57:00 2020 +0100
    Merge pull request #35 from novakov-alexey/add-cookier-attributes
    add string property attributes to put user defined parameters into th…
    setNextVersion
    setReleaseVersion
    time

    View Slide

  34. other useful plugins
    sbt-updates
    sbt-scalafmt
    sbt-mdoc
    sbt-scoverage

    View Slide

  35. Thank you! Questions?
    Alexey Novakov
    email:
    - alexey.novakov at ultratendency.com
    - novakov.alex at gmail.com
    Blog:
    https://novakov-alexey.github.io/
    https://medium.com/se-notes-by-alexey-novakov
    Code: https://github.com/novakov-alexey
    Twitter: @alexey_novakov

    View Slide