SBT:动态检测建筑平台

Pav*_*vel 4 scala sbt

我正在尝试根据平台(Windows 或 Linux)动态更改对我在项目中使用的 jars 依赖项的引用

所以,这是一个非常微不足道的场景,

如何在 build.sbt 中实现这个简单的检查?

Mar*_*lic 5

潜在的方法是System.getProperty("os.name")自定义定义的设置中进行模式匹配,如下所示

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")
  }
}
Run Code Online (Sandbox Code Playgroud)

然后将评估设置添加到libraryDependencies如下

libraryDependencies ++= Seq(
  configureDependencyByPlatform.value,
  "org.scalatest" %% "scalatest" % "3.0.5",
  ...
)
Run Code Online (Sandbox Code Playgroud)