Scala sbt Project Directory Walkthrough

For the past five months, I’ve worked on some Scala projects. Most of them have mature codebases, which means I didn’t need to set up a Scala project from scratch. But I always wanted to know how to do it and also wanted to understand project directory structure. So I did it this week by setting up a HelloWorld project. This post will show you how I did it and also explain Scala sbt project directory structure.

Create Scala sbt project

First thing first, let’s create a Scala sbt project. Open Intellij and click on Create New Project.

Select Scala -> sbt, then click Next.

Give the project a name and click Finish.

Congrats 🥳 You’ve successfully created your 1st Scala sbt project from scratch. You should now see the project directory in the Project panel in Intellij. If not, hit the shortcut command + 1 (Mac) to open it.

The nitty-gritty of project directory

Now I’m going to explain to you the usage of each directory path.

.idea/— contains IntelliJ’s project specific settings files. According to JetBrains, normally there is no need to edit the contents of this folder.

build.sbt — contains build definition of the project. For example, project name, project version, scalaVersion, libraryDependencies, etc. Note that this file is written in Scala.

project/—because build.sbt is written in Scala, we need to build build.sbt. Therefore, this folder contains all the files needed for building build.sbt.

project/build.properties — this file contains build definition of build.sbt. For example, it defines sbt version.

project/target/— contains artifacts from building build.sbt.

src/—this is where we put our source files. sbt uses the same directory structure as Maven for source files by default. Other directories in src/ will be ignored in build time.

src/main/scala — contains main .scala files

src/main/java — contains main .java files

src/main/resources — contains main files other than .scala/.java files

src/test/scala — contains test .scala files

src/test/java — contains test .java files

src/test/resources — contains test files other than .scala/.java files

target/— contains artifacts from building the project

println(“Hello World”)

Before we build and run our project, let’s add the following classic code to our project:

println("Hello World")

Where should we chuck it?

Normally when you run a Scala sbt project, it will use the main function as the entry point to run the project. So another question — where should we chuck the main function?

Because sbt needs to call the main function from an object, the answer is that we write it in a Scala object like this:

object HelloWorldMain {
def main(args: Array[String]): Unit = {
println("Hello World")
}
}

So, let’s create a package called com.helloworld (alternatively you can use your domain name in reverse order) in src/main/scala.

Then right-click on the package and select New -> Scala Class. We will name the class HelloWorldMain. Before you click OK, remember to change Kind from Class to Object for the reason we mentioned earlier.

You can add the code snippet we discussed earlier in the file.

Build and Run the project

Now you should be able to see two green arrows next to your main function. Clicking either of them will build and run the project.

Then you will see the “Hello World” text printed out in the Run tool window in Intellij.

And the build artifact can be found in target/scala-2.12/classes.

The End

There you go, a demonstration of creating a dummy Scala project from scratch. I hope after reading this post, you will have a better understanding of Scala sbt project directory.

As always, stay tuned for more Scala posts 😉