0

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?

Florian Baierl
  • 2,378
  • 3
  • 25
  • 50
  • Put `version.sbt` in `project` directory? – pme Jun 24 '19 at 09:26
  • But then I need to copy it for every single sub-project, right? – Florian Baierl Jun 24 '19 at 09:27
  • you are maybe right. I usually handle multi-projects as recommendet in the Play docs. - check my answer here: https://stackoverflow.com/a/56286492/2750966 – pme Jun 24 '19 at 09:34
  • That would be a possibility, but I would prefer not to define all dependencies of the sub-projects in the root project as that way I feel like I'd lose a lot of the modularity I get with multi-project solutions. – Florian Baierl Jun 24 '19 at 09:56

1 Answers1

1

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

emilianogc
  • 880
  • 4
  • 19