问题描述
我正在使用 SBT 为 Scala 2.8、2.9 和(希望)2.10 交叉构建的项目.我想在仅使用 2.10 编译时添加 -feature
选项.
I am using a project with cross-build for Scala 2.8, 2.9 and (hopefully) 2.10, using SBT. I would like to add the -feature
option when compiling with 2.10 only.
换句话说,当我使用小于 2.10.0 的版本进行编译时,我想将编译器选项设置为:
In other words, when I compile with a version smaller than 2.10.0, I would like to set the compiler options as:
scalacOptions ++= Seq( "-deprecation", "-unchecked" )
并且在使用大于或等于 2.10.0 的版本进行编译时:
and when compiling with a version greater or equal than 2.10.0:
scalacOptions ++= Seq( "-deprecation", "-unchecked", "-feature" )
有没有办法做到这一点?
Is there a way to achieve this ?
推荐答案
交叉构建时,scalaVersion 反映您的项目当前构建所针对的版本.所以取决于 scalaVersion 应该可以解决问题:
When cross-building, scalaVersion reflects the version your project is currently built against. So depending on scalaVersion should do the trick:
val scalaVersionRegex = "(\\d+)\\.(\\d+).*".r
...
scalacOptions <++= scalaVersion { sv =>
sv match {
case scalaVersionRegex(major, minor) if major.toInt > 2 || (major == "2" && minor.toInt >= 10) =>
Seq( "-deprecation", "-unchecked", "-feature" )
case _ => Seq( "-deprecation", "-unchecked" )
}
这篇关于带 SBT 的条件 scalacOptions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!