Conditional scalacOptions with SBT - scala

I am using a project with cross-build for Scala 2.8, 2.9 and (hopefully) 2.10, using SBT. I would like to add the -feature option when compiling with 2.10 only.
In other words, when I compile with a version smaller than 2.10.0, I would like to set the compiler options as:
scalacOptions ++= Seq( "-deprecation", "-unchecked" )
and when compiling with a version greater or equal than 2.10.0:
scalacOptions ++= Seq( "-deprecation", "-unchecked", "-feature" )
Is there a way to achieve this ?

I found this was quick and concise way of doing it:
scalaVersion := "2.10.0"
crossScalaVersions := "2.9.2" :: "2.10.0" :: Nil
scalacOptions <<= scalaVersion map { v: String =>
val default = "-deprecation" :: "-unchecked" :: Nil
if (v.startsWith("2.9.")) default else default :+ "-feature"
}

When cross-building, scalaVersion reflects the version your project is currently built against. So depending on scalaVersion should do the trick:
val scalaVersionRegex = "(\\d+)\\.(\\d+).*".r
...
scalacOptions <++= scalaVersion { sv =>
sv match {
case scalaVersionRegex(major, minor) if major.toInt > 2 || (major == "2" && minor.toInt >= 10) =>
Seq( "-deprecation", "-unchecked", "-feature" )
case _ => Seq( "-deprecation", "-unchecked" )
}

There is CrossVersion.partialVersion now which can be used for this. I am not sure which SBT it was introduced in, but it seems to work fine even in 0.13.8:
scalacOptions ++= {
if (CrossVersion.partialVersion(scalaVersion.value).exists(_ >= (2, 10))) {
Seq("-deprecation", "-unchecked", "-feature")
} else {
Seq("-deprecation", "-unchecked")
}
}
Note: you need to import scala.math.Ordering.Implicits._ to be able to use >= operator on tuples.

Related

Is there a way to exclude fatal warnings from unit tests?

I would like to turn fatal warnings on for my project but exclude fatal warnings for for my unit tests. I am currently cross compiling to Scala 2.12 and 2.13 (also ignoring deprecation warnings) and these are my current compiler flags within build.sbt
scalacOptions ++= Seq(
"-language:_",
"-target:11",
"-encoding",
"UTF-8",
"-feature",
"-Xfatal-warnings",
"-language:existentials",
"-language:higherKinds",
"-language:implicitConversions",
"-unchecked",
"-Ywarn-dead-code",
"-Ywarn-numeric-widen",
"-Ywarn-value-discard"
) ++ (CrossVersion.binaryScalaVersion(scalaVersion.value) match {
case "2.12" =>
Seq(
"-Xlint",
"-Yno-adapted-args",
"-Xfuture",
"-Ypartial-unification"
)
case "2.13" =>
Seq(
"-Wconf:cat=deprecation:w,any:e", // exclude deprecations in fatal warnings
"-Xlint:-byname-implicit" // needed for Doobie
)
case _ => Nil
})
All you need is:
Test / scalacOptions -= "-Xfatal-warnings"

SBT - Adding custom resolver is not working

I am trying to import redshift JDBC into my project . Based on this link
https://docs.aws.amazon.com/redshift/latest/mgmt/configure-jdbc-connection-with-maven.html
My sbt build doesnt seem to pick this resolver . What am i doing wrong
Below is my sbt settings
lazy val commonSettings = Seq(
scalacOptions ++= compilerOptions,
logLevel := Level.Debug,
resolvers ++= Seq(
"Redshift" at "http://redshift-maven-repository.s3-website-us-east-1.amazonaws.com/release"
)
)
It works for me:
lazy val root = (project in file("."))
scalaVersion in ThisBuild:= "2.12.7"
resolvers += "redshift" at "http://redshift-maven-repository.s3-website-us-east-1.amazonaws.com/release"
libraryDependencies += "com.amazon.redshift" % "redshift-jdbc41" % "1.2.10.1009"

Using .value in shared setting definition in SBT / Sane way to organize settings based on Scala version

I want to provide a multiple settings based on Scala binary version.
Those settings would be shared between several projects.
Something like:
lazy val akaneSettings = Def.settings(
organization := "ws.kotonoha",
moduleName := "akane",
crossScalaVersions := Seq("2.11.12", "2.12.4"),
scalaVersion := "2.11.12",
version := "0.2-SNAPSHOT",
javacOptions ++= Seq("-encoding", "utf8"),
scalacOptions ++= Seq(
"-feature",
"-deprecation"
),
scalaBinaryVersion.value match {
case "2.11" =>
Def.settings(
scalacOptions ++= Seq(
"-Ybackend:GenBCode",
"-Yopt:l:classpath",
"-Yopt-warnings",
"-target:jvm-1.8"
),
libraryDependencies ++= Seq("org.scala-lang.modules" % "scala-java8-compat_2.11" % "0.8.0")
)
case "2.12" =>
Def.settings(
scalacOptions ++= Seq(
"-opt:l:classpath"
)
)
case _ => throw new Exception("Not supported yet")
}
}
)
Unfortunately, the pattern match on .value does not work: it requires that I use it within a macro context.
Of course I can do the branching logic for each individual setting and use := / ++=, but that will leave a mess.
Is there a way to sanely organize groups of settings based on Scala version?
You need to move your conditional checks to inside the settings definitions, and not generate multiple settings from outside.
SBT's syntax might give the impression that you're updating values in a mutable fashion, such as by using the := operator, but you're not. Every single setting transformation is stored to be composed and applied later. At the point where akaneSettings is defined the value of scalaBinaryVersion is not known (and may actually be different depending on the context being evaluated).
Your example should look somewhat like:
// No need to use Def.Setting in recent SBT versions
lazy val akaneSettings = Seq(
organization := "ws.kotonoha",
// ...,
scalacOptions ++= {
scalaBinaryVersion.value match {
case "2.11" =>
Seq("-some-2.11-setting")
case "2.12" =>
Seq("-some-2.12-setting")
case _ =>
sys.error("Only Scala 2.11 and 2.12 are supported")
}
},
libraryDependencies ++= {
scalaBinaryVersion.value match {
case "2.11" =>
Seq("org.scala-lang.modules" % "scala-java8-compat_2.11" % "0.8.0")
case "2.12" =>
Seq.empty
case _ =>
sys.error("Only Scala 2.11 and 2.12 are supported")
}
}
}
You can create functions to generate your settings. For example:
def akaneSettings(scalaBinaryVersion: String) = Def.settings(
...
scalaBinaryVersion match {
...
}
)
... and then using it as akaneSettings(scalaBinaryVersion.value).

How to set different scalacOptions per Scala version when cross-compiling using Build.scala?

When building with Scala 2.10 and SBT 0.13.2, I want to have -language:_, but this isn't recognized by Scala 2.9. There's a question about settings different scalacOptions for cross-compilation (Conditional scalacOptions with SBT), but it is about build.sbt. I'm using Build.scala because I'm doing a multi-project build.
I have tried this:
def scalacOptionsVersion(v: String) = {
Seq(
"-unchecked",
"-deprecation",
"-Xlint",
"-Xfatal-warnings",
"-Ywarn-dead-code",
"-target:jvm-1.7",
"-encoding", "UTF-8") ++ (
if (v.startsWith("2.9")) Seq() else Seq("-language:_"))
}
override val settings = super.settings ++ Seq(
...,
scalaVersion := "2.10.4",
scalacOptions <++= scalaVersion(scalacOptionsVersion),
crossScalaVersions := Seq("2.9.2", "2.10.4", "2.11.4"),
...
)
but I get an error:
[error] /Users/benwing/devel/lemkit/scala/project/build.scala:29: type mismatch;
[error] found : sbt.Def.Initialize[Equals]
[error] required: sbt.Def.Initialize[sbt.Task[?]]
[error] Note: Equals >: sbt.Task[?], but trait Initialize is invariant in type T.
[error] You may wish to define T as -T instead. (SLS 4.5)
[error] scalacOptions <++= scalaVersion(scalacOptionsVersion),
[error] ^
[error] one error found
Help?
In SBT 0.13+ this will work:
def scalacOptionsVersion(scalaVersion: String) = {
Seq(
"-unchecked",
"-deprecation",
"-Xlint",
"-Xfatal-warnings",
"-Ywarn-dead-code",
"-target:jvm-1.7",
"-encoding", "UTF-8"
) ++ CrossVersion.partialVersion(scalaVersion) match {
case Some((2, scalaMajor)) if scalaMajor == 9 => Nil
case _ => Seq("-language:_")
}
}
val appSettings = Seq(
scalacOptions := scalacOptionsVersion(scalaVersion.value)
// other settings...
)

Play sub-projects: how to convert to build.sbt

I've a working multi-module Play 2.2 application which is organized like this...
myApp
+ app
+ conf
+ project
+ build.properties
+ Build.scala
+ plugin.sbt
... where Build.scala contains the following statements:
import sbt._
import Keys._
import play.Project._
object ApplicationBuild extends Build {
val appName = "myApp"
val appVersion = "1.0-SNAPSHOT"
val authDependencies = Seq(
"se.radley" %% "play-plugins-salat" % "1.3.0"
)
val mainDependencies = Seq(
"se.radley" %% "play-plugins-salat" % "1.3.0"
)
lazy val auth = play.Project(
appName + "-auth",
appVersion,
authDependencies,
path = file("modules/auth")).settings(
lessEntryPoints <<= baseDirectory(customLessEntryPoints),
routesImport += "se.radley.plugin.salat.Binders._",
templatesImport += "org.bson.types.ObjectId",
testOptions in Test := Nil,
resolvers ++= Seq(Resolvers.sonatype, Resolvers.scalaSbt)
)
lazy val main = play.Project(
appName,
appVersion,
mainDependencies).settings(
scalacOptions += "-language:reflectiveCalls",
routesImport += "se.radley.plugin.salat.Binders._",
templatesImport += "org.bson.types.ObjectId",
testOptions in Test := Nil,
lessEntryPoints <<= baseDirectory(customLessEntryPoints),
resolvers ++= Seq(Resolvers.sonatype, Resolvers.scalaSbt)
).dependsOn(auth).aggregate(auth)
def customLessEntryPoints(base: File): PathFinder = {
(base / "app" / "assets" / "stylesheets" / "bootstrap" * "bootstrap.less") +++
(base / "app" / "assets" / "stylesheets" * "*.less")
}
}
object Resolvers {
val scalaSbt = Resolver.url("Scala Sbt", url("http://repo.scala-sbt.org/scalasbt/sbt-plugin-snapshots"))(Resolver.ivyStylePatterns)
val sonatype = Resolver.sonatypeRepo("snapshots")
}
Now reading the Play 2.2 documentation it looks like I should convert my project to build.sbt:
The following example uses a build.scala file to declare a play.Project. This approach was the way Play applications were defined prior to version 2.2. The approach is retained in order to support backward compatibility. We recommend that you convert to the build.sbt based approach or, if using a build.scala, you use sbt’s Project type and project macro.
Is there any working example that describes how to replace project/build.scala with build.sbt? I read some short articles here and there... but I was unable to get a working Play project.
There is no urgent need to convert your build to build.sbt. build.sbt is simpler, but basically just gets compiled into Build.scala.
The other answer to this question will work, but is perhaps a little verbose. Start with the SBT documentation:
http://www.scala-sbt.org/0.13.0/docs/Getting-Started/Multi-Project.html
Now, create specify your main project and sub projects, and put your main project settings into your main build.sbt file:
lazy val auth = project.in(file("modules/auth"))
lazy val main = project.in(file(".")).dependsOn(auth).aggregate(auth)
playScalaSettings
name := "myApp"
version := "1.0-SNAPSHOT"
libraryDependencies += "se.radley" %% "play-plugins-salat" % "1.3.0"
scalacOptions += "-language:reflectiveCalls"
routesImport += "se.radley.plugin.salat.Binders._"
templatesImport += "org.bson.types.ObjectId"
testOptions in Test := Nil
lessEntryPoints <<= baseDirectory(customLessEntryPoints)
resolvers ++= Seq(Resolvers.sonatype, Resolvers.scalaSbt)
object Resolvers {
val scalaSbt = Resolver.url("Scala Sbt", url("http://repo.scala-sbt.org/scalasbt/sbt-plugin-snapshots"))(Resolver.ivyStylePatterns)
val sonatype = Resolver.sonatypeRepo("snapshots")
}
And now, in modules/auth/build.sbt, put your settings for the auth module:
name := "myApp-auth"
lessEntryPoints <<= baseDirectory(customLessEntryPoints)
routesImport += "se.radley.plugin.salat.Binders._"
templatesImport += "org.bson.types.ObjectId"
testOptions in Test := Nil
resolvers ++= Seq(Resolvers.sonatype, Resolvers.scalaSbt)
Anyway, it might need a bit of tweaking, but hopefully you get the point.
if using a build.scala, you use sbt’s Project type and project macro
Replace play.Project with Project and fix the arguments according to the ScalaDoc, it should be something like
lazy val auth = Project(
appName + "-auth",
file("modules/auth")).settings(
version := appVersion,
libraryDependencies ++= authDependencies,
lessEntryPoints <<= baseDirectory(customLessEntryPoints),
routesImport += "se.radley.plugin.salat.Binders._",
templatesImport += "org.bson.types.ObjectId",
testOptions in Test := Nil,
resolvers ++= Seq(Resolvers.sonatype, Resolvers.scalaSbt)
)
lazy val main = Project(
appName,
file("app")).settings(
version := appVersion,
libraryDependencies ++= mainDependencies,
scalacOptions += "-language:reflectiveCalls",
routesImport += "se.radley.plugin.salat.Binders._",
templatesImport += "org.bson.types.ObjectId",
testOptions in Test := Nil,
lessEntryPoints <<= baseDirectory(customLessEntryPoints),
resolvers ++= Seq(Resolvers.sonatype, Resolvers.scalaSbt)
).dependsOn(auth).aggregate(auth)
Same definitions can be used in build.sbt instead. I would also extract common settings:
val commonSettings = Seq(
version := appVersion,
routesImport += "se.radley.plugin.salat.Binders._",
templatesImport += "org.bson.types.ObjectId",
testOptions in Test := Nil,
lessEntryPoints <<= baseDirectory(customLessEntryPoints),
resolvers ++= Seq(Resolvers.sonatype, Resolvers.scalaSbt)
)
lazy val auth = Project(
appName + "-auth",
file("modules/auth")).settings(commonSettings: _*).settings(
libraryDependencies ++= authDependencies
)
lazy val main = Project(
appName,
file("app")).settings(commonSettings: _*).settings(
libraryDependencies ++= mainDependencies,
scalacOptions += "-language:reflectiveCalls"
).dependsOn(auth).aggregate(auth)