Hanfei Sun
Hanfei Sun

Reputation: 47071

What are the differences between `./build.sbt` and `./project/build.sbt`?

For example, I want to set a key in the build definition, which one should I put the setting in? The build.sbt in the root directory? Or the build.sbt in project directory?

I think both will work. Is there any reason that I could prefer one to the other?

Upvotes: 6

Views: 1184

Answers (2)

Jacek Laskowski
Jacek Laskowski

Reputation: 74709

There's almost no difference between build.sbt and project/build.sbt -- they're both build.sbt after all, but their location makes a difference in how and what part of the project build they're configuring.

Quoting sbt is recursive:

The project directory is another project inside your project which knows how to build your project. The project inside project can (in theory) do anything any other project can do. Your build definition is an sbt project.

And the turtles go all the way down. If you like, you can tweak the build definition of the build definition project, by creating a project/project/ directory.

There are two direct ways to set up a build for your project using sbt - with .sbt files in the main directory of your project and project/*.scala files. All these files constitute the build.

There's however a way to set up a build for the build you've just configured and the same rules apply - you can use (note project directory before the locations) project/*.sbt and project/project/*.scala files.

And the turtles go all the way down.

To extend your project's build you may want to use sbt plugins - a build configuration bundled together to ease its installation. They're a part of the build of the build of your project (the build was doubled on purpose). By convention, the plugins are installed in project/plugins.sbt, but any .sbt file beneath project directory would be fine.

To make the story short, if you want to set a key in the build definition, you should use .sbt or project/*.scala files at the level that would correspond to the build definition. If that's the top-level build definition of your project, go for build.sbt in the root directory.

Remember that any build definition under project becomes a part of the build definition of your project. [And the turtles go all the way down][3], remember?

Upvotes: 2

Marius Soutier
Marius Soutier

Reputation: 11274

The project folder should not contain build.sbt, only plugins.sbt and .scala files. The Scala files are typically used to create some common object with vals that are used in several builds throughout your project. I also usually create a dev.sbt that contains plugins that I'm using for development, and that file stays out of version control.

Upvotes: 8

Related Questions