Search code examples
scalasbtsbteclipse

How to have sbt multi-project builds configure setting for subprojects?


I have an (0.13.1) project with a bunch of subprojects. I am generating project configurations using . My projects only have source files, so I want to remove the generated src/java folders.

I can achieve that by (redundantly) adding the following to the build.sbt of each subproject:

unmanagedSourceDirectories in Compile := (scalaSource in Compile).value :: Nil

unmanagedSourceDirectories in Test := (scalaSource in Test).value :: Nil

I tried just adding the above configuration to the root build.sbt but the eclipse command still generated the java source folders.

Is there any way to specify a configuration like this once (in the root build.sbt) and have it flow down to each subproject?


Solution

  • You could define the settings unscoped and then reuse them

    val onlyScalaSources = Seq(
      unmanagedSourceDirectories in Compile := Seq((scalaSource in Compile).value),
      unmanagedSourceDirectories in Test := Seq((scalaSource in Test).value)
    )
    
    val project1 = project.in( file( "project1" )
      .settings(onlyScalaSources: _*)
    
    val project2 = project.in( file( "project2" )
      .settings(onlyScalaSources: _*)
    

    You could also create a simple plugin (untested code)

    object OnlyScalaSources extends AutoPlugin {
      override def trigger = allRequirements
      override lazy val projectSettings = Seq(
        unmanagedSourceDirectories in Compile := Seq((scalaSource in Compile).value),
        unmanagedSourceDirectories in Test := Seq((scalaSource in Test).value)
      )
    }
    

    More details about creating plugins in the plugins documentation