如何为每个Scala版本制作不同的子类?

时间:2019-04-28 06:35:02

标签: scala maven

这是我的项目结构。我想在运行时加载基于scala版本的不同子类。

root
   -- scala-parent (ParentClass)
   -- scala-2.10 (SubClassScala210)
   -- scala-2.11 (SubClassScala211)
   -- scala-2.12 (SubClassScala212)

基类在scala-parent中,而其他模块具有不同的子类。我可以针对不同版本的Scala库构建子类模块(scala-2.10 / scala-2.11 / scala-2.12)。问题是我只能再次构建scala-parent的一个特定版本的scala。

我想知道这种问题的最佳实践是什么,如何为每个scala版本构建子类?

请提供针对Maven的解决方案,而不是sbt。

1 个答案:

答案 0 :(得分:6)

sbt-cross-build-dependson-example显示了如何cross-build的项目由multiple sub-projects组成,其中子项目 不同的Scala版本取决于父项目。

给出scala-parent项目以及dependsOn scala-2.11的{​​{1}}和scala-2.12子项目,然后 在scala-parent上设置crossScalaVersions,而在像这样的子项目上设置scalaVersion

scala-parent

还在lazy val scalaParent = (project in file("scala-parent")).settings(crossScalaVersions := Seq("2.11.0", "2.12.0")) lazy val scala211 = (project in file("scala-2.11")).dependsOn(scalaParent).settings(scalaVersion := "2.11.0") lazy val scala212 = (project in file("scala-2.12")).dependsOn(scalaParent).settings(scalaVersion := "2.12.0") 项目和其他crossScalaVersions := Seq()项目上设置aggregates

root

现在执行lazy val root = (project in file(".")) .aggregate(scalaParent, scala211, scala212) .settings( name := "sbt-cross-build-dependson-example", libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.5" % Test, crossScalaVersions := Seq() // https://github.com/sbt/sbt/issues/4262#issuecomment-405607763 ) (注意sbt +compile)应该交叉构建项目:

+

请注意,sbt:sbt-cross-build-dependson-example> +compile [info] Setting Scala version to 2.12.0 on 2 projects. ... [info] Updating scala212... [info] Compiling 1 Scala source to /Users/mario/IdeaProjects/sbt-cross-build-dependson-example/scala-parent/target/scala-2.12/classes ... [info] Compiling 1 Scala source to /Users/mario/IdeaProjects/sbt-cross-build-dependson-example/scala-2.12/target/scala-2.12/classes ... ... [info] Setting Scala version to 2.11.0 on 2 projects. ... [info] Updating scala211... [info] Compiling 1 Scala source to /Users/mario/IdeaProjects/sbt-cross-build-dependson-example/scala-parent/target/scala-2.11/classes ... [info] Compiling 1 Scala source to /Users/mario/IdeaProjects/sbt-cross-build-dependson-example/scala-2.11/target/scala-2.11/classes ... 是如何针对2.11和2.12交叉编译的

scala-parent

仅根据各自的版本构建子项目

> tree -d -L 1 scala-parent/target/
scala-parent/target/
├── scala-2.11
├── scala-2.12