18.1. Creating a Project Directory Structure for SBT

Problem

SBT doesn’t include a command to create a new project, and you’d like to quickly and easily create the directory structure for a new project.

Solution

Use either a shell script or a tool like Giter8 to create your project’s directory structure. Both approaches are shown here.

Use a shell script

SBT uses the same directory structure as Maven, and for simple needs, you can generate a compatible structure using a shell script. For example, the following Unix shell script creates the initial set of files and directories you’ll want for most projects:

#!/bin/sh
mkdir -p src/{main,test}/{java,resources,scala}
mkdir lib project target

# create an initial build.sbt file
echo 'name := "MyProject"

version := "1.0"

scalaVersion := "2.10.0"' > build.sbt

Just save that code as a shell script on Unix systems (or Cygwin on Windows), make it executable, and run it inside a new project directory to create all the subdirectories SBT needs, as well as an initial build.sbt file.

Assuming this script is named mkdirs4sbt, and it’s on your path, the process looks like this:

/Users/Al/Projects> mkdir MyNewProject

/Users/Al/Projects> cd MyNewProject

/Users/Al/Projects/MyNewProject> mkdirs4sbt

If you have the tree command on your system and run it from the current directory, you’ll see that the basic directory structure looks like this:

. |-- build.sbt |-- lib |-- project |-- src | |-- main | | |-- java | | |-- resources | | |-- scala | |-- test | |-- java ...

Get Scala Cookbook now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.