How do you share a custom task in a SBT multi-project - scala

I have a project set up as a SBT multi-build. That looks like this:
- project
Dependencies.scala
- core
build.sbt
- server
build.sbt
build.sbt
I want to use Dependencies.scala as a container for version numbers of libraries that are shared between the sub-projects.
sealed trait Dependencies {
val commonsIo = "2.4"
}
object DependencyVersions extends Dependencies
In the root build.sbt I added a Setting that is given to each sub-project.
lazy val dependencies = settingKey[Dependencies]("versions")
val defaultSettings = Defaults.coreDefaultSettings ++ Seq(
dependencies := DependencyVersions)
def projectFolder(name: String, theSettings: Seq[Def.Setting[_]] = Nil) = Project(name, file(name), settings = theSettings)
lazy val core = projectFolder("core", defaultSettings)
I can't access the dependencies setting in core/build.sbt.
"commons-io" % "commons-io" % dependencies.value.commonsIo, <-- doesn't work
How can I get this to work?

You can define common settings (dependencies) in an object Common extends AutoPlugin (in project/Common.scala), and then use .enablePlugin(Common) on sub-project descriptor (see it in Anorm).

Thanks #cchantep got it working now using the AutoPlugin below
import sbt._
sealed trait Dependencies {
val commonsIo = "2.4"
}
object DependencyVersions extends Dependencies
object DependencyVersionsPlugin extends AutoPlugin {
override def trigger = allRequirements
object autoImport {
lazy val dependencies = settingKey[Dependencies]("Bundles dependency versions")
}
import autoImport._
override def projectSettings = Seq(
dependencies := DependencyVersions
)
}

Related

Need to provide a SettingKey from a plugin I use in my sbt plugin

I am using the s3 resolver plugin and would like to override it in my AutoPlugin.
I have tried added the value to projectSettings and globalSettings.
Error
not found: value s3CredentialsProvider
[error] s3CredentialsProvider := s3CredentialsProviderChain
Code
lazy val s3CredentialsProviderChain = {bucket: String =>
new AWSCredentialsProviderChain(
new EnvironmentVariableCredentialsProvider(),
CustomProvider.create(bucket)
)
}
override lazy val projectSettings = Seq(
publishTo := {
if (Keys.isSnapshot.value) {
Some("my-snapshots" at "s3://rest-of-stuff")
} else {
Some("my-releases" at "s3://rest-of-stuff")
}
},
s3CredentialsProvider := s3CredentialsProviderChain
)
The plugin code I'm working on does not define any custom settings of it's own thus has no autoImport of it's own.
Update
I have been unable to resolve the fm.sbt.S3ResolverPlugin in MyPlugin and the code won't compile.
I have tried adding it to enablePlugins on MyPlugin's build.sbt as well as adding it to the dependencies like this:
libraryDependencies ++= Seq(
"com.amazonaws" % "aws-java-sdk-sts" % amazonSDKVersion,
"com.frugalmechanic" % "fm-sbt-s3-resolver" % "0.17.0"
)
I get an error from sbt which I've asked below:
sbt fails to resolve a plugin as dependency
If you create an AutoPlugin in project directory. You need to add this to plugins.sbt.
addSbtPlugin("com.frugalmechanic" % "fm-sbt-s3-resolver" % "0.16.0")
If you create an independent plugin, add this to build.sbt of the plugin
sbtPlugin := true
addSbtPlugin("com.frugalmechanic" % "fm-sbt-s3-resolver" % "0.16.0")
autoImport does not work in scala files that are compiled for sbt, ie plugins for example. You have specify imports statements as in simple scala program. Something like this
import fm.sbt.S3ResolverPlugin
import sbt._
object TestPlugin extends AutoPlugin {
override def requires = S3ResolverPlugin
override def trigger = allRequirements
override def projectSettings: Seq[Def.Setting[_]] = Seq(
S3ResolverPlugin.autoImport.s3CredentialsProvider := ???
)
}
Note that to enable TestPlugin, you have to call enablePlugins(S3ResolverPlugin) in build.sbt

SBT AutoPlugin dependencies

I've created an SBT plugin placed into project folder. This plugin extends sbt.AutoPlugin and adds a custom task.
Something like this:
object MyCustomTask extends AutoPlugin {
...
lazy val myCustomTask = Def.task {
runner.value.run("my.support.project.classpath.Utility")
}
}
and I have this build.sbt
lazy val support = (project in file("support"))
.settings(libraryDependencies ++= Seq(
"com.h2database" % "h2" % "1.4.197"
))
lazy val root = (project in file("root"))
.settings(...)
.dependsOn(support) // <- how can I remove this?
.enablePlugin(MyCustomTask)
I don't want to make a dependency between root project and support project, because in this way root inherits all the dependecies from support that it doesn't needs (like the h2database dependency), but if I remove the dependsOn(support) the task defined in MyCustomTask can't find my.support.project.classpath.Utility.
Ho can I move that dependency into MyCustomTask plugin definition?
Dependencies can be added to the plugin overriding the projectSettings field, like the following:
object MyCustomTask extends AutoPlugin {
...
lazy val myCustomTask = Def.task {
runner.value.run("my.support.project.classpath.Utility")
}
override val projectSettings: Seq[Def.Setting[_]] = Seq(
libraryDependencies += "com.h2database" % "h2" % "1.4.197"
)
}

Reference to undefined setting in plugin of multi-project sbt

I have the following project definition (simplified):
object B extends Build {
lazy val root = (project in file("."))
.aggregate(commons, processor)
lazy val commons = (project in file("commons"))
lazy val processor = (project in file("processor"))
.enablePlugins(BuildInfoPlugin, BuildTag)
}
and the BuildTag plugin (also simplified to the issue at hand):
object BuildTag extends AutoPlugin {
override def requires = BuildInfoPlugin
override lazy val buildSettings = Seq(
packageOptions in (Compile, packageBin) += {
Package.ManifestAttributes(("buildinfo.package", (buildInfoPackage in Compile).value))
}
)
}
when I load the project, I get an error like:
Reference to undefined setting:
{.}/compile:buildInfoPackage from {.}/compile:packageBin::packageOptions
It looks like sbt is trying to reference the setting outside of the scope where the plugin is using it. Why might that be and how can I fix it?
The problem here was not the multi-module nature, because it is reproducible also in a single-module project.
However instead of
override lazy val buildSettings = ...
you need to use projectSettings to make the buildInfoPackage task usable.

Play Framework and scala.tools.nsc

I have to use scala parser inside Play Framework application.
import scala.tools.nsc._
trait Foo
class Parser {
def parse(code: String) = {
val settings = new Settings
settings.embeddedDefaults[Foo]
val interpreter = new Interpreter(settings)
interpreter.parse(code)
}
}
I have following dependency in Build.scala
"org.scala-lang" % "scala-compiler" % "2.9.1"
This code works when build using SBT. In Play it ends with NullPointerException and:
Failed to initialize compiler: object scala not found.
** Note that as of 2.8 scala does not assume use of the java classpath.
** For the old behavior pass -usejavacp to scala, or if using a Settings
** object programatically, settings.usejavacp.value = true.
Build.scala
import sbt._
import Keys._
import PlayProject._
object ApplicationBuild extends Build {
val appName = "com.qwerty.utils"
val appVersion = "1.0-SNAPSHOT"
val scalaVersion = "2.9.1"
val appDependencies = Seq(
"org.scala-lang" % "scala-compiler" % "2.9.1"
)
val main = PlayProject(appName, appVersion, appDependencies, mainLang = SCALA).settings(
// Add your own project settings here
)
}
For background on embeddedDefaults, see the original proposal.
The container (Play) must define the 'app.class.path' and 'boot.class.path' resources and then embeddedDefaults will use them to configure the interpreter properly for the environment. So, this is an enhancement for Play.
If you can pass the necessary classpaths into your application, you can configure classpaths and classloaders explicitly yourself with something like:
val settings = new Settings
settings.classpath.value = "<classpath>"
settings.bootclasspath.value =
settings.bootclasspath.value + File.pathSeparator +
"<extra-bootclasspath>"
val interpreter = new Interpreter(settings) {
override def parentClassLoader = classOf[Foo].getClassLoader
}
interpreter.parse(code)
The bootclasspath should generally contain scala-library.jar and the classpath should contain the application jars.

Subproject dependencies in SBT

I am having a strange problem with SBT subprojects which I think is dependency related. Here's my setup:
I have an SBT project with two subprojects A and B.
A contains a class and companion object MyA
B depends on A.
B contains an object MyB which has a main method.
When I try to execute MyB from the SBT prompt, I get a NoSuchMethodError on MyA. This is not a ClassNotFoundException, but maybe it's happening because it sees the MyA class on the classpath, but not the MyA object.
As a sanity check, I dropped the B subproject and moved its source into the A source tree. When I run MyB from the SBT prompt, it works as expected.
Has anyone run into this, or am I doing something obviously wrong?
Here is my project configuration:
class MyProject(info: ProjectInfo) extends ParentProject(info) {
lazy val a = project("a", "a", new AProject(_))
lazy val b = project("b", "b", new BProject(_), a)
object Dependencies {
lazy val scalaTest = "org.scalatest" % "scalatest_2.9.0" % "1.4.1" % "test"
}
class AProject(info: ProjectInfo) extends DefaultProject(info) with AutoCompilerPlugins {
val scalaTest = Dependencies.scalaTest
val continuationsPlugin = compilerPlugin("org.scala-lang.plugins" % "continuations" % "2.9.0")
override def compileOptions = super.compileOptions ++ compileOptions("-P:continuations:enable") ++ compileOptions("-unchecked")
}
class BProject(info: ProjectInfo) extends DefaultProject(info)
}
It turns out to have been a problem enabling the continuations plugin on project B. Here's my working configuration:
class MyProject(info: ProjectInfo) extends ParentProject(info) {
lazy val a = project("a", "a", new AProject(_))
lazy val b = project("b", "b", new BProject(_), a)
object Dependencies {
lazy val scalaTest = "org.scalatest" % "scalatest_2.9.0" % "1.4.1" % "test"
}
class AProject(info: ProjectInfo) extends DefaultProject(info) with AutoCompilerPlugins {
val scalaTest = Dependencies.scalaTest
val continuationsPlugin = compilerPlugin("org.scala-lang.plugins" % "continuations" % "2.9.0")
override def compileOptions = super.compileOptions ++ compileOptions("-P:continuations:enable") ++ compileOptions("-unchecked")
}
class BProject(info: ProjectInfo) extends DefaultProject(info) with AutoCompilerPlugins {
override def compileOptions = super.compileOptions ++ compileOptions("-P:continuations:enable") ++ compileOptions("-unchecked")
}
}