4

I am trying dynamically change reference to the jars dependency I am using in my project, depending on the platform (Windows or Linux)

So, its a very trivial scenario,

How can I implement this simple check in the build.sbt ?

Pavel
  • 1,519
  • 21
  • 29
  • 2
    Does something like this help: https://stackoverflow.com/questions/228477/how-do-i-programmatically-determine-operating-system-in-java – James Whiteley Apr 09 '19 at 10:54
  • Looks like this is a code I need, just have to execute it in the build time, let me try, thank you !!! – Pavel Apr 09 '19 at 11:01

1 Answers1

5

Potential approach is to pattern match on System.getProperty("os.name") within a custom defined setting like so

val configureDependencyByPlatform = settingKey[ModuleID]("Dynamically change reference to the jars dependency depending on the platform")
configureDependencyByPlatform := {
  System.getProperty("os.name").toLowerCase match {
    case mac if mac.contains("mac")  => "org.example" %% "somelib-mac" % "1.0.0"
    case win if win.contains("win") => "org.example" %% "somelib-win" % "1.0.0"
    case linux if linux.contains("linux") => "org.example" %% "somelib-linux" % "1.0.0"
    case osName => throw new RuntimeException(s"Unknown operating system $osName")
  }
}

and then add the evaluated setting to libraryDependencies as follows

libraryDependencies ++= Seq(
  configureDependencyByPlatform.value,
  "org.scalatest" %% "scalatest" % "3.0.5",
  ...
)
Mario Galic
  • 47,285
  • 6
  • 56
  • 98
  • 1. Is there something similar I can use in the scope of git submodules?? as I need to have two different versions of how projects would resolved locally (Windows) and on the build sever (Linux), I have sbt multiproject etc. 2. This build.sbt approach is looking good, I hope I will use in the project, but what I've used so far, is actually build in -> unmanagedJars (https://www.scala-sbt.org/1.x/docs/Library-Dependencies.html), which is nicely resolving locations I need. – Pavel Apr 09 '19 at 17:41