Monday, April 25, 2011

Working In Scala with both IntelliJ and Maven

As an IntelliJ advocate and Maven lover (yes it's possible, just
compare it to ANT), I faced the problem of practicing small Scala katas
in a sober environment deprived of a Maven. This was till I discovered
how to include a nice Maven plugin for Scala located here:

http://scala-tools.org/mvnsites/maven-scala-plugin/

So it took me a few second to setup a new file pattern for a fresh pom
file targetting work with Scala. Following the how-to on the above web
site, one just have to include the following repositories:

<repositories>
<repository>
<id>scala-tools.org</id>
<name>Scala-Tools Maven2 Repository</name>
<url>http://scala-tools.org/repo-releases</url>
</repository>
</repositories>

<pluginRepositories>
<pluginRepository>
<id>scala-tools.org</id>
<name>Scala-Tools Maven2 Repository</name>
<url>http://scala-tools.org/repo-releases</url>
</pluginRepository>
</pluginRepositories>

Then update your dependency management as following:

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.8.1</version>
</dependency>
...
</dependencies>
</dependencyManagement>


And naturally follows the dependency declaration, where you specify
your interest versus the Scala library:

<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
</dependency>
...
</dependencies>


You can then tune your environment to work with scala and resources
directories as your source/tests directories:

<build>
<sourceDirectory>src/main/scala</sourceDirectory>
<testSourceDirectory>src/test/scala</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<scalaVersion>2.8.1</scalaVersion>
</configuration>
</plugin>
</plugins>
</build>


Integration with IntelliJ works nicely when you have activated the
Scala plugin, just importing your freshly created maven project.
IntelliJ automatically flags the scala and resource directories as
source/test directories and proposes your the creation of both Scala
and Java classes when you hit the magic combination ALT + Insert

0 comments:

Post a Comment