Scala
is a programming language that implements many interesting concepts and can be compiled into Java bytecode, making it an an ideal match for Java developers who would like to try out new programming language and keep using existing code and third-party libraries.
Unfortunately Scala and Maven 2 have not been working together until now. Scala comes with an standalone compiler, scalac and a collection of Ant tasks. Maven 2 relies on Plexus
for compilers and there are up to now Plexus compiler components for the standard javac, for Jikes
, for the Eclipse Java Compiler
and for C# using Microsofts compiler or Mono
.
It wrote until now, because this patch for Plexus
brings a Scala compiler component for plexus. The easiest way to use it is to create a new Maven project with some Scala source code in src/main/scala and to use an adaption of following pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>scala-maven-example</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>Scala Maven Example</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>scala</groupId>
<artifactId>scala-library</artifactId>
<version>2.1.8</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>Goshaky</id>
<url>http://www.goshaky.com/m2</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>Goshaky Plugins</id>
<url>http://www.goshaky.com/m2</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>false</enabled>
</releases>
</pluginRepository>
</pluginRepositories>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/main/scala</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerId>scalac</compilerId>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-compiler-scalac</artifactId>
<version>1.6-SNAPSHOT</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
This pom uses the Goshaky repository which contains snapshots for Scala and the new compiler component until they are available in the public repository, configures the Maven compiler plugin to use the Scala compiler component and adds the path src/main/scala to the source lookup path, so you don't have to put your *.scala files below src/main/java.
The most interesting part in writing this component was seeing how certain Scala constructs (e.g. functions as arguments) are mapped to Java constructs.