Maven and Scala


Using SBT to compile a scala project is great but sometimes maven stays the best choice (because of CI tooling, because of company choices….).

Here how to setup a scala project (using scaltest!) with maven.

The first step is to define the scala target version in a property (you’ll see it will be used multiple times):

<properties>
  <scala.version>2.9.1</scala.version>
</properties>

Then configure your build to use src/main/scala and src/test/scala instead of java (you can keep java too if your project uses both languages):

...
    <sourceDirectory>src/main/scala</sourceDirectory>
    <testSourceDirectory>src/test/scala</testSourceDirectory>
...

Now basic configuration is done let configure the compilation itself. You simply need to configure the scala-maven-plugin:

<plugin>
  <groupId>net.alchim31.maven</groupId>
  <artifactId>scala-maven-plugin</artifactId>
  <version>3.1.0</version>
  <executions>
    <execution>
      <goals>
        <goal>compile</goal>
        <goal>testCompile</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <scalaVersion>${scala.version}</scalaVersion>
  </configuration>
</plugin>

And to let it compile nicely simply add scala-library to dependencies:

<dependency>
  <groupId>org.scala-lang</groupId>
  <artifactId>scala-library</artifactId>
  <version>${scala.version}</version>
</dependency>

Normally you are now able to build scala sources.

To go further and to benefit from the scalatest framework which is really nice you can add another maven plugin: scalatest-maven-plugin. Simply activate it and remove the surefire execution (another nice way to do it is to use different test file name convention ;)):

<!-- disable surefire because we use scalatest -->
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.12</version>
  <configuration>
    <skipTests>true</skipTests>
  </configuration>
</plugin>
<!-- enable scalatest as a replacement for surefire -->
<plugin>
  <groupId>org.scalatest</groupId>
  <artifactId>scalatest-maven-plugin</artifactId>
  <version>1.0-M2</version>
  <configuration>
    <reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
    <junitxml>.</junitxml>
    <filereports>TestSuite.txt</filereports>
  </configuration>
  <executions>
    <execution>
      <id>test</id>
      <goals>
        <goal>test</goal>
      </goals>
    </execution>
  </executions>
</plugin>

Same as for scala-maven-plugin, this plugin needs to add scalatest dependency (in fact your test classes needs it here):

<dependency>
  <groupId>org.scalatest</groupId>
  <artifactId>scalatest_${scala.version}</artifactId>
  <version>2.0.M4</version>
  <scope>test</scope>
</dependency>

Ok now you can write source code in scala and test it with scala test :).

If you want to see it in action you can get the scala-basic sample of openejb with uses it to test a simple singleton: scala-basic

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s