Adding Scalastyle in a Multi-Module SBT Scala Project

Knoldus Inc.
Knoldus - Technical Insights
3 min readSep 15, 2014

Nowadays, building a Multi-Module SBT project is becoming very common. It helps us to keep multiple related projects in a single build. Moreover, each sub-project in the build has its own SBT default directory. Also, it generates its own build and works like any other project.

But, adding Scalastyle in a Multi-Module SBT Scala project is not easy because Scalastyle does not support Multi-Module SBT projects yet. So, in this post we will see how to add Scalastyle in a Multi-Module SBT project in an easy way.

In this following example we have a main project called parent and two sub-projects — child1 and child2. Now, lets get started with building the parent project.

1. To create multiple projects in a single build, we must first declare each project and also that how they relate in Build.scala file. So, here’s an example of a Build.scala file which defines a root project parent, where the parent project aggregates two sub-projects, child1 and child2.

[code language=”scala”]
import sbt._
import Keys._
import org.scalastyle.sbt.ScalastylePlugin

object BuildSettings {

lazy val buildSettings =
Defaults.defaultSettings ++
Seq(
version := “1.0”,
scalaVersion in ThisBuild := “2.11.2”,
parallelExecution in ThisBuild := false,
scalacOptions := Seq(
“-feature”,
“-language:implicitConversions”,
“-language:postfixOps”,
“-deprecation”,
“-encoding”, “utf8”,
“-Ywarn-adapted-args”))

}

object SBTMultiBuild extends Build {

import BuildSettings._

// Common settings for all build modules.
val commonSettings = buildSettings ++ Seq(ScalastylePlugin.Settings: _*)

lazy val parent = Project(
id = “parent”,
base = file(“.”)),
settings = buildSettings) aggregate(child1, child2)

lazy val child1 = Project(
id = “child1”,
base = file(“child1”),
settings = commonSettings)

lazy val child2 = Project(
id = “child2”,
base = file(“child2”),
settings = commonSettings)
}
[/code]

This code resides under parent/project/Build.scala. Having common buildSettings for all projects helps in managing all common settings for all projects under one object. Besides we also have commonSettings which contains Scalastyle settings in it, so that we don’t have to add it separately in all projects.

2. Next we have added commonSettings in all sub-projects

[code language=”scala”]
lazy val child1 = Project(
id = “child1”,
base = file(“child1”),
settings = commonSettings)

lazy val child2 = Project(
id = “child2”,
base = file(“child2”),
settings = commonSettings)
[/code]

By adding commonSettings in all sub-projects we get Scalastyle settings added in all of them. So, we don’t have to add Scalastyle settings in sub-projects separately.

3. Now we have to add following line of code in parent/project/plugins.sbt file

[code language=”scala”]
// SBT plugin for ScalaStyle
addSbtPlugin(“org.scalastyle” %% “scalastyle-sbt-plugin” % “0.5.0”)
[/code]

This code will help SBT to add Scalastyle as a Plugin in the project.

4. At last we need to add scalastyle-config.xml file in the parent folder.

5. Now if we run — sbt scalastyle command on the parent project we can see the warnings on the terminal for all sub-projects in a single go.

[code language=”scala”]
knoldus@knoldus-desktop:~/parent$ sbt scalastyle
[info] Loading project definition from /home/knoldus/parent
[info] Compiling 2 Scala sources to /home/knoldus/parent/target/scala-2.11/sbt-0.13/classes…
[info] Set current project to parent (in build file:/home/knoldus/parent)
[info] Processed 1 file(s)
[info] Found 0 errors
[info] Found 0 warnings
[info] Found 0 infos
[info] Finished in 10 ms
[success] created: sbt.SettingKey$$anon$4@54449f9d
[info] Processed 2 file(s)
[info] Found 0 errors
[info] Found 0 warnings
[info] Found 0 infos
[info] Finished in 1 ms
[success] created: sbt.SettingKey$$anon$4@54449f9d
[warn] /home/knoldus/parent/child1/src/main/scala/sample.scala:16:55: magic.number.message
[info] Processed 1 file(s)
[info] Found 0 errors
[info] Found 1 warnings
[info] Found 0 infos
[info] Finished in 4 ms
[success] created: sbt.SettingKey$$anon$4@54449f9d
[warn]/home/knoldus/parent/child2/src/main/scala/sample.scala:10:8: method.name.message
[info] Processed 3 file(s)
[info] Found 0 errors
[info] Found 1 warnings
[info] Found 0 infos
[info] Finished in 1 ms
[success] Total time: 1 s, completed 14 Sep, 2014 1:02:50 PM
[/code]

So, we saw in this post that how easy it is to add Scalastyle in a Multi-Module SBT Scala Project.

--

--

Knoldus Inc.
Knoldus - Technical Insights

Group of smart Engineers with a Product mindset who partner with your business to drive competitive advantage | www.knoldus.com