Including project in build depending on setting's value, e.g. scalaVersion?

StackOverflow https://stackoverflow.com/questions/23452098

  •  15-07-2023
  •  | 
  •  

Domanda

I have a Scala project that is divided into several subprojects:

lazy val core: Seq[ProjectReference] = Seq(common, json_scalaz7, json_scalaz)

I'd like to make the core lazy val conditional on the Scala version I'm currently using, so I tried this:

lazy val core2: Seq[ProjectReference] = scalaVersion {
    case "2.11.0" => Seq(common, json_scalaz7)
    case _        => Seq(common, json_scalaz7, json_scalaz)
}

Simply speaking, I'd like to exclude json_scalaz for Scala 2.11.0 (when the value of the scalaVersion setting is "2.11.0").

This however gives me the following compilation error:

[error] /home/diego/work/lift/framework/project/Build.scala:39: type mismatch;
[error]  found   : sbt.Project.Initialize[Seq[sbt.Project]]
[error]  required: Seq[sbt.ProjectReference]
[error]   lazy val core2: Seq[ProjectReference] = scalaVersion {
[error]                                                        ^
[error] one error found

Any idea how to solve this?

Update

I'm using sbt version 0.12.4 This project is the Lift project, which compiles against "2.10.0", "2.9.2", "2.9.1-1", "2.9.1" and now we are working on getting it to compile with 2.11.0. So creating a compile all task would not be practical, as it would take a really long time.

Update 2

I'm hoping there is something like this:

lazy val scala_xml    = "org.scala-lang.modules"     %% "scala-xml"         % "1.0.1"
lazy val scala_parser = "org.scala-lang.modules"     %% "scala-parser-combinators" % "1.0.1"

...

lazy val common =
  coreProject("common")
    .settings(description := "Common Libraties and Utilities",
            libraryDependencies ++= Seq(slf4j_api, logback, slf4j_log4j12),
            libraryDependencies <++= scalaVersion {
              case "2.11.0" => Seq(scala_xml, scala_parser)
              case _ => Seq()
            }
  )

but for the projects list

Note how depending on the scala version, I add the scala_xml and scala_parser_combinator libraries

You can see the complete build file here

È stato utile?

Soluzione

Cross building a project

Simply speaking, I'd like to exclude json_scalaz for Scala 2.11.0

The built-in support in sbt for this is called cross building, which is described in Cross-Building a Project. Here's from the section with a bit of correction:

Define the versions of Scala to build against in the crossScalaVersions setting. For example, in a .sbt build definition:

crossScalaVersions := Seq("2.10.4", "2.11.0")

To build against all versions listed crossScalaVersions, prefix the action to run with +. For example:

> +compile

Multiple-project builds

sbt also has built-in support to aggregate tasks across multiple projects, which is described Aggregation. If what you need eventually is normal built-in tasks like compile and test, you could set up a dummy aggregate without json_scalaz.

lazy val withoutJsonScalaz = (project in file("without-json-scalaz")).
  .aggregate(liftProjects filterNot {_ == json_scalaz}: _*)

From the shell, you should be able to use this as:

> ++2.11.0
> project withoutJsonScalaz
> test

Getting values from multiple scopes

Another feature you might be interested in is ScopeFilter. This has the ability to traverse multiple projects beyond usual aggregation and cross building. You would need to create a setting whose type is ScopeFilter and set it based on scalaBinaryVersion.value. With scope filters, you can do:

val coreProjects = settingKey[ScopeFilter]("my core projects")

val compileAll = taskKey[Seq[sbt.inc.Analysis]]("compile all")

coreProjects := {
  (scalaBinaryVersion.value) match {
    case "2.10" => ScopeFilter(inProjects(common, json_scalaz7, json_scalaz))
  }
}

compileAll := compileAllTask.value

lazy val compileAllTask = Def.taskDyn {
  val f = coreProjects.value
  (compile in Compile) all f
}

In this case compileAll would have the same effect as +compile, but you could aggregate the result and do something interesting like sbt-unidoc.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top