Conditional SBT dependencies

Hello,

Is it possible to add conditionnal dependecies on SBT depending of the used Java version ?

I have this following libraryDependencies who working on Java 8.

libraryDependencies ++= Seq(
    guice,
    ws,
    "commons-io" % "commons-io" % "2.6",
    "com.atlassian.commonmark" % "commonmark" % "0.11.0",
    "com.atlassian.commonmark" % "commonmark-ext-yaml-front-matter" % "0.11.0",
}

But to compile Play project with Java 9, I have to add "com.sun.activation" % "javax.activation" % "1.2.0", but this library don’t compile on Java 8.

I have try with this code, but It not working, do you have any idea ?

val java9LibraryDependencies = Seq[sbt.ModuleID](
    "com.sun.activation" % "javax.activation" % "1.2.0"
)

libraryDependencies ++= Seq(
    guice,
    ws,
    "commons-io" % "commons-io" % "2.6",
    "com.atlassian.commonmark" % "commonmark" % "0.11.0",
    "com.atlassian.commonmark" % "commonmark-ext-yaml-front-matter" % "0.11.0",
    if (System.getProperty("java.version").startsWith("9.")) {
        java9LibraryDependencies
    }
}
error: No implicit for Append.Values[Seq[sbt.librarymanagement.ModuleID], Seq[Any]] found,
  so Seq[Any] cannot be appended to Seq[sbt.librarymanagement.ModuleID]
libraryDependencies ++= Seq(

You’re adding the java9LibraryDependencies sequence as an element in the other Seq.

Perhaps try something like:

val java9LibraryDependencies: Seq[sbt.ModuleID] =
  if (System.getProperty("java.version").startsWith("9."))
    Seq("com.sun.activation" % "javax.activation" % "1.2.0")
  else 
    Seq.empty

libraryDependencies ++= Seq(
    guice,
    ws,
    "commons-io" % "commons-io" % "2.6",
    "com.atlassian.commonmark" % "commonmark" % "0.11.0",
    "com.atlassian.commonmark" % "commonmark-ext-yaml-front-matter" % "0.11.0",
} ++ java9LibraryDependencies
2 Likes

Hello @raboof,

Great it working like a charm.

Thanks

Great! (I moved this topic to the ‘Tooling (sbt and others)’ category, it seems to fit better there)