Saturday, July 23, 2011

Scala, Eclipse and Maven integration tutorial

I have evaluated Scala as a language for cloud computing and Hadoop. One requirement was a robust development environment, with a real build system, a good IDE with code completion and debugging.

The combination of ScalaEclipse and Maven seemed like a fit for this requirement, but my initial experience was mixed.


Problems with Scala, Eclipse and Maven integration

It was easy to install Scala, Eclipse and Maven, but when I set up a project it had a persistent error in Eclipse:

object Predef does not have a member AnyRef

Other problems:

  • There were problems running the unit test.
  • I had to restart Eclipse a lot.
  • Eclipse had Scala set to version 2.9.0.1 while Maven had 2.8.0. When I tried to change Maven to use 2.9.0.1 the pom.xml file would be marked as having an error.
I searched internet for help but could not find it. After a good deal of experimenting I sorted out the problems and found a good solution.


Software versions

My setup is:
  • Scala 2.9.0.1.
  • Eclipse 3.7 Indigo
  • Scala-ide Eclipse plugin: scala nightly 29 - http://download.scala-ide.org/nightly-update-wip-experiment-2.9.0-1


Scala, Eclipse Maven project setup tutorial

Here are that steps that I took to set up at new Scala, Eclipse and Maven project so it works with unit testing.

Press menu item: File - New - Other...



Select Maven Project




Select the org.scala-tools.archetypes scala-archetype-simple




Add group id and artifact id to project. Click Finish



This will create the project with example program and unit tests, but it will leave Eclipse in an unstable state







In the project's pom.xml file make the changes that I have marked in red:


<properties>
 <maven.compiler.source>1.5</maven.compiler.source>
 <maven.compiler.target>1.5</maven.compiler.target>
 <encoding>UTF-8</encoding>
 <scala.version>2.9.0-1</scala.version>
</properties>


<dependency>
 <groupId>org.scala-tools.testing</groupId>
 <artifactId>specs_${scala.version}</artifactId>
 <version>1.6.8</version>
 <scope>test</scope>
</dependency>

Now both Scala IDE and Maven are both using the same version of Scala. Scala 2.9.0.1


Right click the whole project and select: Configure - Add Scala Nature




Now use the Maven build system to clean, build and run unit tests. Run from either Eclipse or command line.

From Eclipse, right click the whole project and selecting:
Maven clean
Maven install





From command line:

C:\prog\apache-maven-2.2.1\bin\mvn clean
C:\prog\apache-maven-2.2.1\bin\mvn install

Note that you have to use Maven 2.2 and not Maven 3.



Now there should be no more errors.
The unit test: "scalatest.scala" has some problems, delete it.

Run all unit tests from Eclipse. By right clicking the whole project and select Run As JUnit Test



Now you can see the result in the JUnit runner.


Final impression of Scala, Eclipse and Maven integration

Once I had resolved the problems the Scala, Eclipse and Maven combination was a great development environment meeting my requirements.

One thing that is currently missing from the Scala Eclipse plugin is code refactoring. Refactoring works very well in both Eclipse for Java and Visual Studio for C#.



2 comments:

Unknown said...

Great Information.

karthick
java training in chennai

Unknown said...

Thanks for sharing such informative article on Java technology. It is most preferred technology among developers to create stunning mobile application fast and easy. It also created massive career opportunity for aspirinsg professionals. JAVA J2EE Training in Chennai