How to have SBT subproject with multiple Scala versions?

后端 未结 3 1495
感情败类
感情败类 2021-02-04 08:28

I have a project using Scala 2.10 and one using Scala 2.11. They depend on a common project, which can compile with both.

lazy val foo = (project in file(\"foo\"         


        
3条回答
  •  北恋
    北恋 (楼主)
    2021-02-04 09:05

    I created an SBT plugin for this.

    project/plugins.sbt

    resolvers += Resolver.sonatypeRepo("releases")
    
    addSbtPlugin("com.lucidchart" % "sbt-cross" % "1.0")
    

    build.sbt

    lazy val foo = (project in file("foo")).dependsOn(baz_2_10).settings(
      scalaVersion := "2.10.4"
    )
    
    lazy val bar = (project in file("bar")).dependsOn(baz_2_11).settings(
      scalaVersion := "2.11.5"
    )
    
    lazy val baz = (project in file("baz")).cross
    
    lazy val baz_2_10 = baz("2.10.4")
    
    lazy val baz_2_11 = baz("2.11.5")
    

    It takes a couple more lines, but now everything compiles as expected: sbt foo/compile works, and sbt bar/compile works.

    You don't have to remember unique commands, you don't have bugs from crossPath := false, and unlike ++, this is parallelizable: sbt compile will compile foo, bar, and baz with the correct Scala versions concurrently.

提交回复
热议问题