Scala Starter Pack

Last update: 2024-01-11 06:19:43 +0800 +0800
tags: Jvm | Scala | Sbt
Available translations: 日本語 / 中文(简体) / 中文(繁體)

Scala compiles to JVM bytecode. A build requires a Scala compiler at compile time and the Scala standard library on the application runtime classpath. Scala binary versions are compatibility boundaries: an artifact ending in _2.13 is not interchangeable with its _3 counterpart.

Versions and Toolchains #

Scala 2 and Scala 3 have distinct compilers and language rules. Use the binary version selected by the project for every cross-published library. The Java runtime must meet the Scala release’s documented JDK requirements; compile and test with the JDK used in deployment.

For a small program:

sh
scala-cli run Main.scala
scala-cli test .

Scala CLI manages compiler and dependency resolution for source-oriented projects. Commit a build definition for maintained applications rather than depending on a developer’s local command history.

sbt #

sbt is Scala’s build tool. The launcher starts on the JVM and downloads the sbt version declared by the project. Pin sbt in project/build.properties and define the Scala version once:

scala
ThisBuild / scalaVersion := "3.5.2"
ThisBuild / organization := "com.example"

lazy val root = (project in file("."))
  .settings(
    name := "orders",
    libraryDependencies += "org.typelevel" %% "cats-core" % "2.12.0"
  )

Run sbt test, sbt compile, and sbt package from the project root. The %% operator chooses the artifact suffix matching the active Scala binary version. sbt itself can run on a different Scala version from the project; the launcher uses the version required by sbt internals.

Coursier #

Coursier resolves JVM artifacts and installs Scala tools. It can launch an application by Maven coordinate, cache dependencies, and install a named command:

sh
cs launch scala:3.5.2 -- -version
cs install scala-cli

Use explicit application coordinates and versions in automation. Coursier’s cache improves repeatability only when coordinates and repositories are controlled; it does not replace dependency locking or artifact verification.

Coordinates, Repositories, and Channels #

Coursier reuses the Maven coordinate model and extends it for Scala-specific artifacts:

  • group:app:version for a Java application;
  • group::app:version for a Scala artifact;
  • group:::app:version for a cross-version Scala artifact.

It resolves dependencies from configured repositories, then downloads the artifacts into its local cache. Repositories can come from the command line, from a local directory, or from a channel descriptor source.

A channel is a collection of application descriptor JSON files. A channel may be served over HTTP, backed by a local directory, or packaged in a JAR file. That is why Coursier can discover installable apps without a handwritten build definition.

Launchers and Packaging #

Coursier installs executable launchers, not plain source trees. The installed command is usually a script-prepended JAR: a small launcher script followed by JAR bytes. The shell wrapper passes -J options to the JVM and then runs the main class from the embedded application.

sh
cs install scala-cli
cs launch scala:3.5.2 -- -version

This packaging lets a user run ./tool directly while still keeping the JAR format intact for java -jar. The practical tradeoff is that the launcher still depends on the cached artifacts and on the chosen repository set.

Comments

Read or comment on GitHub (sign-in required to comment)