Maven Plugins for Scala Code Quality
SBT is an excellent tool for Scala and there are many plugins available for SBT to improve and measure the code quality of our Scala code. Sometime you may require to use only the Maven as a build tool and most of the popular plugins available in SBT for Scala are also available in maven.
In this blog I will try to list down the plugins related to Scala that you can easily integrate with Maven and the sample configuration.
- Scala Compiler Plugin — scala-maven-plugin
The Scala Compiler Plugin provides a lot of options and below sample configuration can be used for project that uses a mix of Java and Scala Code
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<jvmArgs>
<jvmArg>-Xms64m</jvmArg>
<jvmArg>-Xmx1024m</jvmArg>
</jvmArgs>
<args>
<arg>-target:jvm-1.7</arg>
<arg>-deprecation</arg>
<arg>-feature</arg>
<arg>-language:existentials</arg>
<arg>-language:implicitConversions</arg>
<arg>-language:postfixOps</arg>
</args>
<sourceDir>src/main/scala</sourceDir>
<testSourceDir>src/test/scala</testSourceDir>
</configuration>
<executions>
<execution>
<id>compile</id>
<goals>
<goal>compile</goal>
</goals>
<phase>compile</phase>
</execution>
<execution>
<id>test-compile</id>
<goals>
<goal>testCompile</goal>
</goals>
<phase>test-compile</phase>
</execution>
<execution>
<phase>process-resources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
- Running ScalaTest Unit Tests — scalatest-maven-plugin
The ScalaTest Maven plugin allows you to run ScalaTest tests through Maven without requiring @RunWith(classOf[JUnitRunner])
annotations and access all functionality of the ScalaTest Runner
, including parallel execution and multiple reporters.
For writing the ScalaTests in your code you can add the following dependency in your pom.xml and then configure the Plugin given below to execute your ScalaTest test cases.
<dependency>
<groupId>org.scalatest</groupId>
<artifactId>scalatest_2.10</artifactId>
<version>2.2.6</version>
<scope>test</scope>
</dependency><plugin>
<groupId>org.scalatest</groupId>
<artifactId>scalatest-maven-plugin</artifactId>
<version>1.0</version>
<configuration>
<reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
<junitxml>.</junitxml>
<filereports>WDFTestSuite.txt</filereports>
</configuration>
<executions>
<execution>
<id>test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<parallel>false</parallel>
<tagsToExclude>Integration-Test</tagsToExclude>
</configuration>
</execution>
<execution>
<id>integration-test</id>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<parallel>false</parallel>
<tagsToInclude>Integration-Test</tagsToInclude>
</configuration>
</execution>
</executions>
</plugin>
- Running Specs 2 Unit Tests — maven-specs2-plugin
This plug-in executes specs2 specifications that have been previously compiled to the test classes directory. It assumes that all specification classes have names ending with specified suffix (Spec by default) and that all classes with such name are specifications.
You can add the following dependency in you pom.xml to write specs2 test cases and configure the plugin to run them.
<dependency>
<groupId>com.mmakowski</groupId>
<artifactId>maven-specs2-plugin</artifactId>
<version>0.4.2</version>
<scope>test</scope>
</dependency><plugin>
<groupId>com.mmakowski</groupId>
<artifactId>maven-specs2-plugin</artifactId>
<version>0.4.2</version>
<executions>
<execution>
<id>verify</id>
<phase>verify</phase>
<goals>
<goal>run-specs</goal>
</goals>
</execution>
<execution>
<id>integration</id>
<phase>integration-test</phase>
<goals>
<goal>run-specs</goal>
</goals>
</execution>
</executions>
</plugin>
- Scala Style for static code check — scalastyle-maven-plugin
Scalastyle examines your Scala code and indicates potential problems with it. If you have come across Checkstyle for Java, then you’ll have a good idea what scalastyle is. Except that it’s for Scala obviously.
<plugin>
<groupId>org.scalastyle</groupId>
<artifactId>scalastyle-maven-plugin</artifactId>
<version>0.8.0</version>
<configuration>
<failOnViolation>true</failOnViolation>
<failOnWarning>false</failOnWarning>
<verbose>false</verbose>
<includeTestSourceDirectory>true</includeTestSourceDirectory>
<sourceDirectory>${basedir}/src/main/scala</sourceDirectory>
<testSourceDirectory>${basedir}/src/test/scala</testSourceDirectory>
<configLocation>${basedir}/src/main/resources/scalastyle_config.xml</configLocation>
<outputFile>${project.basedir}/target/scalastyle-output.xml</outputFile>
<outputEncoding>UTF-8</outputEncoding>
</configuration>
<executions>
<execution>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
- Scoverage for code coverage in scala — scala-scoverage-plugin
The Scala Coverage Plugin can be configured to get the code coverage reports for Scala code. It covers both the unit and integration tests.
<plugin>
<groupId>org.scoverage</groupId>
<artifactId>scoverage-maven-plugin</artifactId>
<version>1.1.1</version>
<configuration>
<scalaVersion>2.10.6</scalaVersion>
<highlighting>true</highlighting>
<minimumCoverage>0</minimumCoverage>
<failOnMinimumCoverage>true</failOnMinimumCoverage>
<aggregate>true</aggregate>
</configuration>
<executions>
<execution>
<id>scoverage_unit</id>
<goals>
<goal>check</goal> <!-- or integration-check -->
</goals>
<phase>prepare-package</phase> <!-- or any other phase -->
</execution>
<execution>
<id>scoverage_integration</id>
<goals>
<goal>check</goal>
</goals>
<phase>integration-test</phase>
</execution></executions>
</plugin>
If you have a multi-module project you can also get the aggregated report by adding a reporting section in your pom.xml<reporting>
<plugins>
<plugin>
<groupId>org.scoverage</groupId>
<artifactId>scoverage-maven-plugin</artifactId>
<version>1.1.1</version>
<configuration>
<aggregate>true</aggregate>
</configuration>
<reportSets>
<reportSet>
<reports>
<report>report</report>
</reports>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>Hence, by integrating all the plugins given above in Maven you can get your Scala code compiled and tested. The above plugins also give you reports regarding the test execution as well as the coverage.