Skip to main content

Getting the base directory of an sbt project

This is a command you can run in a shell script to print the base directory of an sbt project:

$ sbt --batch -error "project $PROJECT" "print baseDirectory" | tr -d "\n"

For example:

$ sbt --batch -error "project ingests_api" "print baseDirectory" | tr -d "\n"
/Users/alexwlchan/repos/storage-service/ingests/ingests_api⏎

We use this at work in some of our larger repositories, where we have lots of Scala apps in a single repo. The sbt base directory for each app/project includes some non-Scala files alongside the code, like a README and a Dockerfile. We have scripts that use this command to ask sbt where to find these non-Scala files, so they can be used by other build steps.

How it works

Normally I wouldn’t write a blog post for something this short, but the sbt documentation made this really hard to work out. Lots of stuff is either missing or confusingly explained, and I only got this working after I read several long GitHub issue threads.

Here’s how it works: