dirceusemighini
dirceusemighini

Reputation: 1354

SBT sub project access root project data (dependency version)

I have a multiple project, and I'm creating a class with it's git version in one of my subprojects.

def iUberdataCoreVersion(version: Option[String] = Some("Not a Git Repository"), dir: File) = {
  val file = dir / "UberdataCoreVersion.scala"
  IO.write(
    file,
    s"""package eleflow.uberdata.core\n  object UberdataCoreVersion{\n          val version = "${version.get}"\n
       val sparkVersion = ${allDependencies.value.filter(_.extraString.startsWith("spark")).head.crossVersion}
             |}\n""".stripMargin)
  Seq(file)
}

The dependencies are defined in the root project build.sbt file. I want to add spark version (one of my dependencies) into this file. Is there a way to pass an argument to a sub project build, or to iterate through the dependencies to discover which version of spark am I using?

Update as required in comments

The problem here is that in spark there is two versions, and at version 2 I can create it's context (to access the spark cluster) in a different way than it's in spark 1.

root
 build.sbt

lazy val subProject = project settings (libraryDependencies ++= Seq(
    "org.apache.spark" %% "spark-repl" % sparkVersion)

I have 3 sub projects, and the spark version is shared between them, that's why I've configured it this way, otherwise I could have inserted the dependencies into the subproject build file.

Consider that sparkVersion is provided as a build argument.

root
  sub
    build.sbt

Here in the subproject I want to create a file that contains the spark version. This can be done in a property file, or in a src code file, as I already do with git code. Can I receive it's value as a val when creating the sub project build?

I can access the spark version through it's context, it has a property for it, but in this specific case I want to do that before creating the context, and that's my problem.

Upvotes: 0

Views: 637

Answers (1)

radumanolescu
radumanolescu

Reputation: 4161

We define properties in build.properties and use them in all subprojects. In this case, it's the Scala version - but it could be any string.

// ---------- ---------- multiproj/build.properties ---------- ---------- //
scala.version = 2.11.8
// ---------- ---------- multiproj/subproj/build.sbt ---------- ---------- //
val props = SettingKey[Map[String, String]]("props")

props := {
  import scala.collection.JavaConversions._
  val pf = new java.util.Properties()
  IO.load(pf, baseDirectory.value / ".." / "build.properties")
  pf.stringPropertyNames.map(p => p -> Option(System.getProperty(p)).getOrElse(pf.getProperty(p))).toMap
}

libraryDependencies ++= Seq(
  "org.scala-lang" % "scala-library"  % props.value("scala.version") % "compile->*"
, "org.scala-lang" % "scala-compiler" % props.value("scala.version") % "compile->*"
)

Upvotes: 1

Related Questions