1. Dependency Resolution Failures

Understanding the Issue

SBT fails to resolve dependencies, causing build failures with errors like "not found" or "could not resolve dependency."

Root Causes

  • Incorrect dependency coordinates in build.sbt.
  • Network issues preventing access to repositories.
  • Corrupt SBT cache or Ivy cache issues.

Fix

Ensure dependencies are correctly specified in build.sbt:

libraryDependencies += "org.apache.commons" % "commons-lang3" % "3.12.0"

Refresh the dependency cache:

sbt update clean

Manually clear the Ivy cache and reload dependencies:

rm -rf ~/.ivy2/cache
rm -rf ~/.sbt/boot
sbt update

2. Slow SBT Build Performance

Understanding the Issue

SBT builds take longer than expected, affecting development productivity.

Root Causes

  • Unoptimized compilation settings.
  • Too many dependencies being resolved frequently.
  • High CPU or memory usage during compilation.

Fix

Enable incremental compilation:

Compile / incOptions := incOptions.value.withNameHashing(true)

Use parallel execution to speed up builds:

parallelExecution in Global := true

Allocate more memory to SBT:

export SBT_OPTS="-Xms512M -Xmx4G -XX:+UseG1GC"

3. SBT Plugin Failures

Understanding the Issue

SBT fails to load or execute plugins, showing errors related to missing tasks or incorrect configurations.

Root Causes

  • Incorrect plugin versions or repository locations.
  • Incompatible SBT version for the specified plugins.
  • Corrupt plugin cache affecting execution.

Fix

Ensure the correct plugin syntax in plugins.sbt:

addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.9.3")

Force plugin updates and reload the SBT shell:

sbt reload update

Manually clear and reinstall plugins:

rm -rf ~/.sbt/1.0/plugins
sbt reload

4. Out-of-Memory Errors in SBT

Understanding the Issue

SBT builds fail due to memory exhaustion, showing "OutOfMemoryError: GC overhead limit exceeded."

Root Causes

  • Insufficient heap space for Scala compiler.
  • Large dependency trees consuming excessive memory.
  • Heavy multi-module project builds causing memory overflow.

Fix

Increase heap memory allocation for SBT:

export SBT_OPTS="-Xms1G -Xmx4G -XX:+UseG1GC"

Enable class garbage collection optimization:

scalacOptions += "-Ycache-plugin-class-loader:none"

Limit parallel task execution to reduce memory consumption:

parallelExecution in Test := false

5. Incorrect Artifact Packaging

Understanding the Issue

Generated JAR files do not contain expected classes, resources, or dependencies.

Root Causes

  • Incorrect output settings in SBT configuration.
  • Required dependencies not bundled properly.
  • Scala versions mismatched between build and runtime.

Fix

Ensure correct artifact packaging settings in build.sbt:

assemblyMergeStrategy in assembly := {
  case PathList("META-INF", xs @ _*) => MergeStrategy.discard
  case x => MergeStrategy.first
}

Manually verify JAR contents:

jar tf target/scala-2.13/myapp.jar

Ensure correct Scala version compatibility:

scalaVersion := "2.13.6"

Conclusion

SBT is a powerful build tool for Scala projects, but troubleshooting dependency resolution failures, slow builds, plugin issues, memory errors, and incorrect artifact packaging is essential for efficient development. By optimizing configurations, managing memory effectively, and ensuring correct dependency management, developers can maintain a smooth and reliable SBT build process.

FAQs

1. Why is SBT failing to resolve dependencies?

Check dependency definitions in build.sbt, refresh the cache, and ensure network connectivity.

2. How can I speed up SBT builds?

Enable incremental compilation, increase memory allocation, and use parallel execution.

3. How do I fix SBT plugin failures?

Verify plugin versions in plugins.sbt, force an update, and clear the SBT cache.

4. Why is SBT running out of memory?

Increase heap memory allocation, optimize class garbage collection, and reduce parallel executions.

5. How do I fix incorrect JAR packaging in SBT?

Ensure proper merge strategies in build.sbt, check JAR contents, and match Scala versions between build and runtime.