How does sbt reference the Project in the Build? - scala

How does sbt pick up the root project in a simple definition such as this?
object HelloBuild extends Build {
lazy val root = Project(id = "hello", base = file("."))
}

My (now a bit less) uninformed guess: reflection. :)
Edit:
Some evidence (from the comments above):
http://www.scala-sbt.org/0.12.3/docs/Getting-Started/Multi-Project.html
https://github.com/sbt/sbt/blob/0.13/main/src/main/scala/sbt/Build.scala#L14

Related

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.

Deep nesting of project folder in a SBT multi-project project

I have an SBT project that aggregates over multiple projects like this:
object ClientCore extends Build {
/**
* This is the root project
*/
lazy val rootProj = Project(id = "clientcore", base = file(".")) aggregate(
utilsProj,
commonUiProj,
spatialMathProj,
sessionManagerProj,
lobbyProj,
)
/**
* This is a utils library
*/
lazy val utilsProj = Project(id = "utils", base = file("Utils"))
/**
* A shared library for UI elements
*/
lazy val commonUiProj = Project(id = "commonui", base = file("CommonUI"))
/**
* This is a spatial math library
*/
lazy val spatialMathProj = Project(id = "spatialmath", base = file("SpatialMath"))
lazy val sessionManagerProj = Project(id = "sessionmanager", base = file("sessionManager"),
settings = buildSettings ++ assemblySettings) settings(
outputPath in assembly := new File(s"$outDir\\SessionManagerClient.jar"),
jarName in assembly := "SessionManagerClient.jar",
test in assembly := {}
) dependsOn(utilsProj)
lazy val lobbyProj = Project(id = "lobby", base = file("Lobby"),
settings = buildSettings ++ assemblySettings) settings(
outputPath in assembly := new File(s"$outDir\\Lobby.jar"),
jarName in assembly := "Lobby.jar",
test in assembly := {}
) dependsOn(utilsProj)
}
For some reason some of the projects end up with a deep nesting of 'project' folders. For example Utils might look like: 'Util/project/project/project/project/...
I'm using Intellij's SBT plugin to sync the presentation but managing the project with SBT. I'm not certain whether this is an SBT issue or an Intellij one.
Thanks for any help you can provide.
Kurt
This is an IntelliJ issue (among many others related to the SBT plugin...)
I think you may have refreshed your config somewhere before defining a module and adding that module to the root project aggregates, which has a tendency to make a mess in IntelliJ.
This can be fixed in IntelliJ:
detach your project from IntelliJ
restart IntelliJ
reimport your project

How to enable sbt plugins of root project for all subprojects?

I have a project/build.scala file that defines a root project and a bunch of sub projects:
lazy val root = Project(
id="root",
base=file(".")).aggregate(subA, subB).enablePlugins(MyPlugin)
lazy val subA = Project(
id="subA",
base=file("a"))
lazy val subB = Project(
id="subB",
base=file("b"))
How do I make MyPlugin available in subA and subB without specifying it on each of them? I just want them to inherit the plugins from the root project.
Someone in IRC suggested overriding projects in my build object in build.scala:
override def projects = super.projects map { _.enablePlugins(...) }

SBT create sub-projects using a collection

I've been searching if this is possible for a while with little success.
Using SBT, can you create a sub-project programmatically, without explicitly assigning each project to it's own val?
My current project structure looks something like this:
root/
common/ <--- This is another sub-project that others dependOn
project/
build.scala
src/main/scala
apps/ <--- sub-projects live here
Sub1/
Sub2/
Sub1 and Sub2 are both their own SBT projects.
My first attempt to link these projects together looked like this:
// root/project/build.scala
import sbt._
import Keys._
object build extends Build {
lazy val common = project /* Pseudo-code */
val names = List("Sub1", "Sub2")
lazy val deps = names map { name =>
Project(id = name, base = file(s"apps/$name")).dependsOn(common)
}
lazy val finalDeps = common :: deps
lazy val root = project.in(file(".")).aggregate(finalDeps.map(sbt.Project.projectToRef) :_*)
.dependsOn(finalDeps.map(ClassPathDependency(_, None)) :_*)
}
However, because SBT uses reflection to build it's projects and sub-projects, this doesn't work.
It only works if each sub-project is stated explicitly:
lazy val Sub1 = project.in(file("apps/Sub1"))
So the question:
Is there a way to programmatically build sub-project dependencies in SBT?
Sbt allows for making a build definition for the build itself:
http://www.scala-sbt.org/release/docs/Getting-Started/Full-Def.html
You can try creating a project/project/build.scala file that contains a source generator, something like this:
// project/project/build.scala
sourceGenerators in Compile <+= sourceManaged in Compile map { out =>
Generator.generate(out / "generated")
}
EDIT: You should implement the Generator object yourself.
This source generator will in turn scan the topmost apps folder and create a source for an object that contains all the subprojects.
// project/subprojects.scala
// This is autogenerated from the source generator
object Subprojects{
lazy val Sub1 = project.in(file("apps/Sub1"))
lazy val Sub2 = project.in(file("apps/Sub2"))
lazy val all = Seq(Sub1,Sub2)
}
Now in your main build.scala just write:
// project/build.scala
lazy val root = project.in(file("."))
.aggregate(Subprojects.all.map(sbt.Project.projectToRef) :_*)
.dependsOn(Subprojects.all.map(ClassPathDependency(_, None)) :_*)
I didn't run all this through a compiler so some errors are possible but the principle should work.
EDIT: I created a repo on Github where I implemented the solution. Go there and see how it is done.
https://github.com/darkocerdic/sbt-auto-subprojects

generating jacoco code coverage report for all sub modules

I'm trying to integrate JaCoCo in my Play Scala project, I want it to run code coverage for all sub modules in one report.
When I add the "jacoco.settings" to the root project, jacoco doesn't recognize the tests for the sub modules. in order to do so, I configured the jacoco setting for each one of the modules, and then run the coverage for each one of them (see comment out code). the result is one report for each module.
I would like to generate one report for the whole project, any Idea what can I do?
object PlutusBuild extends Build {
lazy val root = Project(id = "Plutus", base = file("."))
.aggregate(common, importer, crawler, ref_webapp)
.settings(graphSettings: _*)
.settings(jacoco.settings: _*)
lazy val common = Project(id = "Plutus-Common", base =
file("common"))
/*.settings(jacoco.settings: _*)*/
lazy val importer = Project(id = "Plutus-Importer", base =
file("importer")).dependsOn(common)
/*.settings(jacoco.settings: _*)*/
lazy val crawler = Project(id = "Plutus-Crawler", base =
file("crawler")).dependsOn(common).
configs(Atmos).settings(atmosSettings:_*)
/*.settings(jacoco.settings: _*)*/
...
Thanks...
I take it you're using jacoco4sbt? This was recently fixed in version 2.1.4.
You need to use this setting:
Keys.fork in jacoco.Config := true