Sunday, March 14, 2010

Working with Scala and Maven

In order to get Maven to properly recognize Scala code, there are are a number of steps you must take. Included below are pom.xml samples, along with explanations.

1. Name your source directories properly:



2. Add the repositories.

These are required in order to download the necessary Scala dependencies (see step #3):
<name>Scala-tools Maven2 Repository</name>

<name>Scala-tools Maven2 Repository</name>

3. Add the appropriate dependencies:

This contains the scala compiler, which Maven will use to compile your code and run your unit tests.

This is Scala's unit testing framework. It's also possible to use JUnit, but when in Rome, right?

If you want to use scalatest, unfortunately you also need to include JUnit as a dependency (see step #5).


4. Add the scala-tools plugin:



5. Add the @RunWith annotation to each unit test.

If you are using scalatest as your unit testing framework, you must trick Maven into thinking that your tests are JUnit tests. Otherwise, Maven will not run your tests:
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import org.scalatest.FunSuite

class MyScalaTest extends FunSuite{

Tip: If you're using Eclipse, you can right click on one of these unit tests and select "Run As > JUnit Test" to manually run the test.

1 comment:

electronic signatures said...

A nice post on Scala and Maven.As these are really cool tips on working with these two.It really helped me as I had not them in my mind.Thanks