How do you manage dependencies and build Scala projects?

Instruction: Explain the tools and practices for managing dependencies and building projects in Scala.

Context: This question tests the candidate's familiarity with Scala's build tools like sbt (Scala Build Tool) or Mill, and how they manage project dependencies, compilation, testing, and packaging for Scala projects, emphasizing best practices.

Updated

For an sbt project, make the build configuration reviewable and the resolved dependencies observable. A practical workflow is:

  1. Record the sbt version in project/build.properties, set scalaVersion, and select a supported JDK consistently in development and CI. Pin plugin and direct library versions; avoid mutable snapshots or open version ranges in a release build.
  2. Declare libraries in libraryDependencies, with test-only libraries in the Test configuration. For Scala libraries, %% appends the Scala binary version to the artifact name. Use % for artifacts without that cross-version suffix, such as ordinary Java libraries. This selects an artifact; it does not establish compatibility among every transitive dependency. Library dependencies
  3. Inspect the resolved graph and eviction reports when versions disagree. Understand which library requested each version before updating, excluding or overriding it. An override or a strict conflict policy is a decision to validate, not proof that the selected binaries work together. Check the resolver and compatibility behavior of the sbt version and plugins in use. Dependency management
  4. Keep module dependencies explicit and put a library only in the subprojects that require it. Compile, run unit and relevant integration tests, and exercise the packaged artifact with its runtime configuration. Incremental compilation improves feedback time; a cached successful task is not evidence that untested behavior works.
  5. Build releases from a clean checkout in CI and record the toolchain, resolved dependency versions and resulting artifact. Use trusted resolvers, protect repository credentials, and review dependency updates. Pinning direct versions alone cannot prevent mutable artifacts or every environmental difference. Resolution and caching

Choose packaging for the deployment target. sbt's ordinary package task creates a JAR; an executable distribution, assembled JAR or container may require additional configuration or an appropriate plugin. Test that actual output rather than assuming a passing compile makes it deployable. Mill or another established project build can serve the same needs; changing tools is not necessary merely to improve dependency discipline.

Related Questions