sbt uses a standardized directory structure based on Maven conventions. Understanding this structure is essential for organizing your Scala projects effectively.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/sbt/sbt/llms.txt
Use this file to discover all available pages before exploring further.
Standard Project Layout
A typical sbt project has the following structure:Source Directories
Main Sources
Primary Scala source files for the
Compile configuration.Default location: baseDirectory.value / "src" / "main" / "scala"Primary Java source files for the
Compile configuration.Default location: baseDirectory.value / "src" / "main" / "java"Resource files (configuration, images, etc.) for the
Compile configuration.Resources are copied to the classpath during compilation.Default location: baseDirectory.value / "src" / "main" / "resources"Test Sources
Scala test sources for the
Test configuration.Default location: baseDirectory.value / "src" / "test" / "scala"Java test sources for the
Test configuration.Default location: baseDirectory.value / "src" / "test" / "java"Test resource files for the
Test configuration.Default location: baseDirectory.value / "src" / "test" / "resources"Cross-Version Sources
sbt supports Scala version-specific source directories:scalaVersion:
scala-2/: Used for Scala 2.x versionsscala-2.13/: Used for Scala 2.13.x versionsscala-3/: Used for Scala 3.x versions
More specific directories (e.g.,
scala-2.13) take precedence over general ones (e.g., scala-2).Build Definition
build.sbt
The main build definition file written in sbt’s DSL (Scala-based).
project/
The meta-build directory containing the build definition’s build definition.This directory is itself an sbt project used to build your main project.
Specifies the sbt version to use for the build.
Declares sbt plugins for the build.
Optional file for organizing dependencies.
Output Directories
target/
Main directory for all files generated by the build.Default location: Defined by
BuildPaths.DefaultTargetName (“target”)target/scala-<version>/
Version-specific output directory:Compiled classes and resources for the
Compile configuration.Setting: Compile / classDirectoryCompiled test classes and test resources for the
Test configuration.Setting: Test / classDirectoryGlobal Directories
sbt uses global directories for caching and configuration:Global sbt directory for user-level configuration and plugins.Structure:Property:
sbt.global.baseIvy cache for dependency resolution (default).Structure:
Coursier cache for downloaded dependencies (used by modern sbt).Setting:
csrCacheDirectoryUnmanaged Dependencies
Unmanaged JAR dependencies that are not managed by a dependency manager.Default location: JAR files in this directory are automatically added to the classpath.
baseDirectory.value / "lib"Multi-Project Layout
For multi-module builds:Customizing Directory Structure
Custom Source Directories
Custom Output Directory
Sources in Base Directory
sourcesInBase := true is the default for single-project builds but disabled for multi-project builds to avoid conflicts.Best Practices
1. Follow Standard Conventions
Stick to the standard directory structure when possible:2. Ignore Generated Files
Always addtarget/ to .gitignore:
3. Commit Build Definition
Always commit these files:build.sbtproject/build.propertiesproject/plugins.sbtproject/*.scala
4. Use Cross-Version Directories Sparingly
Only create version-specific directories when necessary:5. Organize Large Projects
For large multi-module projects:Directory Reference
| Directory | Purpose | Setting |
|---|---|---|
src/main/scala | Main Scala sources | Compile / scalaSource |
src/main/java | Main Java sources | Compile / javaSource |
src/main/resources | Main resources | Compile / resourceDirectory |
src/test/scala | Test Scala sources | Test / scalaSource |
src/test/java | Test Java sources | Test / javaSource |
src/test/resources | Test resources | Test / resourceDirectory |
target/ | Build output | target |
target/scala-<version>/classes | Compiled classes | Compile / classDirectory |
project/ | Build definition | N/A |
lib/ | Unmanaged JARs | unmanagedBase |
See Also
- Settings and Tasks - Configuring directory settings
- Configurations - Understanding configuration-specific directories
- Command Line - Working with the build from the command line