Skip to main content
 首页 » 编程设计

scala之SBT:动态检测建筑平台

2025年12月25日16oomusou

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

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

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

请您参考如下方法:

潜在的方法是在 System.getProperty("os.name") 上进行模式匹配内custom defined setting像这样

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") 
  } 
} 

然后将评估设置添加到 libraryDependencies如下
libraryDependencies ++= Seq( 
  configureDependencyByPlatform.value, 
  "org.scalatest" %% "scalatest" % "3.0.5", 
  ... 
)