Question

I have an SBT (0.13.2) multiproject, and have a group of subprojects declared in separate build file, like this:

object SubBuilds extends Build {
  lazy val sub_project1 = Project("sub_project1",  file("sub/Project1")).dependsOn(Build.core)
  lazy val sub_project2 = Project("sub_project2", file("sub/Project2")).dependsOn(Build.core)
  ...
}

Each subproject has its own build.sbt file where name := "..." can be specified.

I can access the list of projects in root build simply as SubBuilds.projects and get Seq[Project]. It has supposedly property settings: Seq[Def.Setting[_]] which I hoped to use, but I'm completely stuck how I can get value from any of those properties. I can find a setting with key name but it has nothing like value.

How can I achieve this: get, say, name property using Project instance in scala builds?

Update: I should note that I intend to use it inside of other task, I tried answer by @eugene-yokota but it produces "Illegal dynamic reference" during build compilation.

object CustomTasks {
  /** Task to display Subprojects */
  val subList = taskKey[Unit]("Display subprojects")

  val subListTask = subList := {
    SubBuilds.projects foreach { a =>
      val v = (name in a).value
      println(s"Subproject ${a.id}, $v")
    }
  }
}

[error] .../project/CustomTasks.scala:164: Illegal dynamic reference: a
[error]       val v = (name in a).value
Was it helpful?

Solution

Building on Eugene's answer. The correct solution is indeed Scopes, but you're using it wrong, I guess.

The code should be more or less like this

lazy val nameAndProjectID = Def.task {
    (name.value, projectID.value)
} 

lazy val subListTask = subList := {
  nameAndProjectID.all(ScopeFilter()).value.foreach { case (name, id) =>
   println(s"Subproject ${id}, $name")
  }
}

OTHER TIPS

See Scopes. Project is one of three axes of a key. You can get name from a sub project as:

(name in sub_project1).value
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top