Skip to content

Instantly share code, notes, and snippets.

@vaslabs
Last active January 31, 2019 14:53
Show Gist options
  • Save vaslabs/afd88c996613901923143aeafbc7b966 to your computer and use it in GitHub Desktop.
Save vaslabs/afd88c996613901923143aeafbc7b966 to your computer and use it in GitHub Desktop.
Useful sbt recipes
/*
There are some services that are free for open source which can measure the quality of your code and also scan your
dependencies for any known vulnerabilities, those being codacy and snyk.
You'll need these plugins
*/
addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.5.1")
addSbtPlugin("com.codacy" % "sbt-codacy-coverage" % "1.3.15")
addSbtPlugin("net.virtual-void" % "sbt-dependency-graph" % "0.9.2")
/*
You'll need these dependencies
npm install -g snyk
*/
addCommandAlias("reportTestCov", ";project talos; coverageReport; coverageAggregate; codacyCoverage")
/*
Then on your CI you can do stuff like
sbt coverage test
sbt reportTestCov
snyk test
snyk monitor --project-name=<your snyk project>
*/
/*
There is also a way
*/
/*
These settings help you get the most out of scala and keep your code clean from unused declarations. Especially useful for
the implicit scope as with intelliJ it's not always reliable when implicits are not used.
*/
lazy val compilerSettings = {
scalacOptions ++= Seq(
"-deprecation",
"-feature",
"-language:postfixOps", //Allow postfix operator notation, such as `1 to 10 toList'
"-language:implicitConversions",
"-language:higherKinds",
"-Ypartial-unification",
"-Ywarn-dead-code", // Warn when dead code is identified.
"-Ywarn-extra-implicit", // Warn when more than one implicit parameter section is defined.
"-Ywarn-inaccessible", // Warn about inaccessible types in method signatures.
"-Ywarn-infer-any", // Warn when a type argument is inferred to be `Any`.
"-Ywarn-nullary-override", // Warn when non-nullary `def f()' overrides nullary `def f'.
"-Ywarn-nullary-unit", // Warn when nullary methods return Unit.
"-Ywarn-numeric-widen", // Warn when numerics are widened.
"-Ywarn-unused:implicits", // Warn if an implicit parameter is unused.
"-Ywarn-unused:imports", // Warn if an import selector is not referenced.
"-Ywarn-unused:locals", // Warn if a local definition is unused.
"-Ywarn-unused:params", // Warn if a value parameter is unused.
"-Ywarn-unused:patvars", // Warn if a variable bound in a pattern is unused.
"-Ywarn-unused:privates", // Warn if a private member is unused.
"-Ywarn-value-discard", // Warn when non-Unit expression results are unused.
"-Ywarn-unused:imports",
"-Xfatal-warnings"
)
}
/*
If you are using Kamon you'll notice that you need sigar to get host metrics. The below is using the sbt native
packager plugin. The plugins below and the docker are a good template to get most of Kamon.
Images based on alpine are very small.
*/
addSbtPlugin("io.kamon" % "sbt-aspectj-runner" % "1.1.1")
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.3.11")
lazy val dockerCommonSettings = Seq(
version in Docker := version.value,
maintainer in Docker := <maintainer name>,
dockerBaseImage := "openjdk:8u191-alpine3.8", //better to have a specific jvm version than just openjdk:8-alpine
dockerExposedPorts := Seq(8080),
maintainer := "your email",
packageName in Docker := "your-service-name",
dockerCommands ++= Seq(
Cmd("USER", "root"),
ExecCmd("RUN", "apk", "add", "--no-cache", "sigar")
),
dockerRepository := Some("<host>:<port>"),
version in Docker := version.value,
dockerUsername := Some("your/username"),
dockerUpdateLatest := true //updates latest tag automatically
)
lazy val dockerPlugins = Seq(DockerPlugin, AshScriptPlugin, JavaAppPackaging, UniversalPlugin)
/*
If you wish not to publish artifacts from one of your submodules these are the settings
*/
lazy val noPublishSettings = Seq(
publish := {},
skip in publish := true,
publishLocal := {},
publishArtifact in Test := false
)
/*
This is what I'm using to publish libraries in sonatype.
The part of the recipe I want to highlight is version management.
To configure the version I'm doing something like this.
*/
version in ThisBuild := sys.env.getOrElse("PUBLISH_VERSION", "SNAPSHOT")
/*
That way you can have flexibility to generate this publish version on your CI environment as well.
For instance if you are building on the latest tag you can assign it to your env variable other wise if it's
a git commit hash you can add a snapshot in the end.
Then you'll have some publish settings like this.
*/
addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "2.3")
val publishSettings = Seq(
publishTo := {
val nexus = "https://oss.sonatype.org/"
if (version.value.trim.endsWith("SNAPSHOT"))
Some("snapshots" at nexus + "content/repositories/snapshots")
else
Some("releases" at nexus + "service/local/staging/deploy/maven2")
},
organization := <some.domain.project>, //e.g. for one of my projects it's org.vaslabs.talos
organizationName := <organisation name>,
scmInfo := Some(ScmInfo(url(<your project url>), "scm:git@github.com:<username>/<project>.git")),
developers := List(
Developer(
id = <id>,
name = <firstName lastName>,
email = <your email>,
url = url(<your personal page>)
)
),
publishMavenStyle := true,
licenses := List("MIT" -> new URL("https://opensource.org/licenses/MIT")),
homepage := Some(url(<home page of your project>)),
startYear := Some(2019)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment