Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use shared configurations across SBT (Play) multi-projects?

I have several SBT 0.13 / Play 2.2 projects (websites). They are all multi-module as they share some common functionality. This makes their project configuration files both complex and almost identical, but not quite.

I would like to be able to share as much as possible of these configuration files across the projects (frequent play updates makes keeping 5+ websites up to date a royal pain, not to mention all the almost-identical-but-evolving dependency lists across the projects).

build.properties and plugins.sbt are identical across projects and can be overwritten by a simple script. Great.

Build.scala is trickier - I would like to introduce a shared base class like so:

abstract class MyBuildBase extends Build { ... }

that in Build.scala do:

object ApplicationBuild extends MyBuildBuild { ... }

In order for this to make any sense at all, MyBuildBase.scala needs to be shared across projects. This can be done with svn:external, which operates on directories. Which means I need to somehow make this shared directory accessible when Build.scala is compiled (otherwise sbt complains loudly).

Reading http://www.scala-sbt.org/0.13.0/docs/Detailed-Topics/Classpaths.html and http://www.scala-sbt.org/0.13.0/docs/Getting-Started/Full-Def.html it seems like this should be possible.

However, it is exceptionally unclear to me what to actually put in the project/project/Build.scala file to actually achieve this - I can't find an example of "an sbt build file that's intended to build an sbt build file and include some extra source files in the build".

Any suggestions?

like image 645
Jxtps Avatar asked Oct 31 '13 22:10

Jxtps


1 Answers

What you probably want to do is create a plugin, or shared library.

You can make an sbt project with a build like follows:

build.sbt

sbtPlugin := true

organization := "you"

name := "common-build"

version := "1.0"

Then create in src/main/scala your abstract class "MyBuildBase". Release this project as an sbt plugin.

Then in your other projects, you can use this as a library/plugin. In project/plugins.sbt add:

addSbtPlugin("you" % "common-build" % "1.0")

And this will resolve your common build library when building your build.

If you need more information, look up more about sbt plugins and ignore the part about making something that extends a Plugin. Plugins are just libraries versioned with sbt's version number and your own. You should be able to put whatever code you want in there to share between builds.

like image 132
jsuereth Avatar answered Oct 21 '22 13:10

jsuereth