how to tell sbt that which package needs to be compiled based on provided scala version

156 Views Asked by At

I am using sbt cross build my project supports two scala versions scala-2.12.8 and scala-2.13.1 and accrodingly i have source code directoroes as

src/main/scala_2.12
src/main/test_2.12

src/main/scala_2.13
src/main/test_2.13

i looked at the sbt documentation its not clear from it then i found this answer here is my code

unmanagedSourceDirectories in Compile <+= (scalaVersion, sourceDirectory in Compile) {
  case (v, dir) if v startsWith "2.12" => dir / "scala_2.12"
  case (v, dir) if v startsWith "2.13" => dir / "scala"
}

and am getting the following error

info] Loading project definition from /home/git/testproj/project
/home/git/testproj/build.sbt:54: error: (sbt.SettingKey[String], sbt.SettingKey[java.io.File]) does not take parameters
unmanagedSourceDirectories in Compile <+= (scalaVersion, sourceDirectory in Compile) {
                                                                                     ^
[error] Type error in expression
Project loading failed: (r)etry, (q)uit, (l)ast, or (i)gnore? 
1

There are 1 best solutions below

0
On

It's build-in in sbt since 0.13.something for cross compilation:

src/main/scala      - shared compiled sources
src/main/scala-2.11 - version-specific compiled sources
src/main/scala-2.12
src/main/scala-2.13

src/test/scala      - shared test sources
src/test/scala-2.11 - version-specific test sources
src/test/scala-2.12
src/test/scala-2.13

And for any configuration you created (e.g. it, functional-test, or whatever you name it) it will be:

src/$config/scala               - common code
src/$config/scala-$scalaVersion - version specific code

This was described in documentation in Scala-version specific source directory section.