Search code examples
scalasbtmulti-project

Using the version SettingKey as a dependency version in sbt


Current situation

My project setup looks somewhat like this:

Project: root (build.sbt, version.sbt)
Sub-project: projectA (build.sbt) Sub-project: projectB (build.sbt)

In my version.sbt I have a val that reads the version to use from an external json file like this:

version in ThisBuild := findVersion

lazy val findVersion: String =
  parse(Source.fromFile("../buildProperties.json").mkString).map(_ \\ "version").map(_ map (_.asString)) match {
    case Right(Some(x) :: _) => x
    case _     => throw new IllegalArgumentException("Unable to read buildProperties.json")
  }

The problem

I now need to use this version to define dependencies inside the subprojects (projectA & projectB). So I either:

  1. need to access the val findVersion of the root project from within the build.sbt of projectA & projectB OR
  2. need to use the version directly like this in the build.sbt of projectA & projectB: libraryDependencies += "ext-lib" % "com.fbaierl" % version.value % Provided. But this results in a error: 'value' can only be used within a task or setting macro, such as :=, +=, ++=, Def.task, or Def.setting. exception.

Is there any way I can achieve what I need?


Solution

  • The recommended way of depending between projects would be in your root / build.sbt have something like this

    val projectA = project in file("projectA")
    
    val projectB = (project in file("projectB")).dependsOn(projectA)
    

    If that is not what you have / need, then you can use the version task to set versions in setting dependecies like so:

    libraryDependencies := { libraryDependencies.value :+ ("ext-lib" %% "com.fbaierl" % version.value) }
    

    Hope that helps