1

Is there a simple way to compare versions in sbt build files without handcrafting. E.g.

scalacOptions <++= scalaVersion { sv =>
  if (sv >= "2.10.0") "-feature" :: Nil else Nil
}

(The above compiles, but for some reason "2.9.2" >= "2.10.0" ... ?!)

0__
  • 66,707
  • 21
  • 171
  • 266
  • I mean, don't tell me [this is the recommended way to compare versions](http://stackoverflow.com/questions/12626197/conditional-scalacoptions-with-sbt#12628568) – 0__ Jan 26 '13 at 19:23
  • Those are strings, you know... "2.9.2" _is_ greater than "2.10.0"! – Randall Schulz Jan 26 '13 at 19:29
  • Yes I was surprised that by default an implicit `Ordering` with lexicographic rules seems to be in place. But surely sbt has some comparison function somewhere that breaks down the strings properly? – 0__ Jan 26 '13 at 19:30
  • 1
    Take a look at the way scalaz uses scalaVersion. It looks like they use pattern matching and not string comparisions: https://github.com/scalaz/scalaz/blob/master/project/ScalazBuild.scala – Noah Jan 27 '13 at 01:35
  • `case "2.9.1" | "2.9.2" | "2.9.3-RC1" | "2.10.0" => ...` doesn't convince me – 0__ Jan 31 '13 at 19:11
  • Does this answer your question? [Conditional scalacOptions with SBT](https://stackoverflow.com/questions/12626197/conditional-scalacoptions-with-sbt) – Suma Apr 22 '22 at 16:35

2 Answers2

3

semverfi is a Scala library that you can add to your project/plugins.sbt and it will be available for use in build definitions:

libraryDependencies += "me.lessis" %% "semverfi" % "0.1.2"
Mark Harrah
  • 6,999
  • 1
  • 26
  • 31
2

Since sbt 1.2.0 there's a built-in API: https://www.scala-sbt.org/1.0/docs/sbt-1.2-Release-Notes.html#Semantic+Version+selector+API

For example you can do

VersionNumber("2.12.5").matchesSemVer(SemanticSelector(">=2.12"))
nafg
  • 2,424
  • 27
  • 25