Sbt Dslentry

2020-08-21

When you have a multi-module SBT project, it’s rather easy to assign multiple settings at once to a project. Suppose you have a build file with a project in it that has the following definition:

lazy val myProject = (project in file("."))
  .enablePlugins(somePlugin)
  .settings(
    name := "Hello",
    scalaVersion := "2.13.3"
  )

If you have a bunch of settings to apply, you can just do:

@@ -1,6 +1,8 @@
+val mySettings = Def.settings(???)
 lazy val myProject = (project in file("."))
   .enablePlugins(somePlugin)
   .settings(
     name := "Hello",
     scalaVersion := "2.13.3"
   )
+  .settings(mySettings)

Is there a similar method if you’re working with a single module SBT project where all settings are assignments at the top level. Take the following example:

enablePlugins(somePlugin)
name := "Hello"
scalaVersion := "2.13.3"

This is a perfectly valid project, but you don’t have access to settings method anymore. I found out that you can use DslEntry.fromSettingsDef method to do the equivalent:

+val mySettings = Def.settings(???)
+
 enablePlugins(somePlugin)
 name := "Hello"
 scalaVersion := "2.13.3"
+
+DslEntry.fromSettingsDef(mySettings)

In this particular example, it does not make sense because if you can define mySettings right off the bat, why don’t you just assign the settings right away, and you’d be right. But I was in a position where I was trying to get a hold of a given settings from an upstream plugin. I did not want to apply all the settings but just wanted to grab one. I found myself with a Seq[SettingsDef[_]] or an Option[SettingsDef[_]] and I was not able to apply it to my project.

This does the trick.