caoilte / sbt-import-scala-files   2.0.0

GitHub

A very small sbt plugin for importing Scala files into your build.

Scala versions: 2.12
sbt plugins: 1.x

Import Scala Files Latest version Build Status

sbt-import-scala-files is a very small sbt plugin for importing Scala files into your build. This is most useful when you want to re-use Scala files from your meta-build (files under the project directory) in your proper build.

Rationale

This plugin was written to make it easier to write defaults plugins. A defaults plugin provides a sensible base configuration for other projects. It saves you the work of copy pasting lots and lots of sbt configuration between all of your projects. All your other projects only have to depend on one plugin and it takes care of everything else.

If you are writing a defaults plugin you may also find yourself wanting to make the same decisions that you make for your other projects about your defaults plugin as well. However, sbt provides no out of the box way to refer to meta-build configuration in the proper build. This plugin makes such inter-dependencies possible.

If this sounds like turtles all the way down... it really is.

Installation

Add the following to your project/plugins.sbt file:

addSbtPlugin("org.caoilte" % "sbt-import-scala-files" % "2.0.0")

The plugin is now enabled, but will not do anything until you define a Seq[File] to be imported with the importScalaFilesList setting. It will then copy those files into your build's managed source directory before every compilation phase using the source generation task.

Licence

Apache 2.0

Use Cases

Importing a meta-build file into your proper build

Imagine a library that depends on a particular library within Akka. Projects which depend on that project will pickup that dependency transitively for free, but if they want to depend on the same version of a different Akka library (all Akka libraries are released together) they cannot do so without manual inspection. By including the version of Akka a project depends on within its main Jar that Jar can be depended on within another projects meta-build and the Akka version used as the version there.

Consider an example from this project's sbt tests here.

It has a file called project/MetaBuildConstants.scala that can be referred to from other Scala/sbt files in the meta-build but is un-reachable from the proper-build. We make it usable in the proper build by adding the following to our build.sbt.

filesToImport := Seq(file("project/MetaBuildConstants.scala"))

We are now able to use code from this Scala file in a proper build Scala file.

Adding an sbt plugin to your sbt plugin project and to projects that depend on your project

The previous use case showed how to import a Scala file from the meta-build into the proper build. If you want to import sbt settings from your meta-build (eg plugin installations) into your proper build it takes a little more work.

One way is to create a Scala file in your meta-meta-build (ie the project/project directory) and import that into the meta-build and the proper build. Consider the example in this plugin's sbt tests here. We want to write a defaults plugin that both adds scalafmt as a dependency (so that it is picked up transitively by projects which depend on our defaults plugin) and we want to run scalafmt on our defaults plugin as well.

To do this we create a project/project/Library.scala file which defines the scalafmt ModuleId and then we,

  • copy that and install as a plugin in our meta-build in project/plugins.sbt
  • copy that and add as a dependency in our proper build in build.sbt