Scala and Maven with maven-scala-plugin

| | Comments (0) | TrackBacks (0)
Ok, so the documentation for maven-scala-plugin isn't quite perfect. To save you some time, here is a fully functional pom.xml file that works as of March 6, 2008.
<?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>mvn.scala.test</groupId>
    <artifactId>mvn.scala.test</artifactId>
    <name>Maven Scala Plugin Test</name>
    <packaging>jar</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <description>
		Test for Maven Scala Plugin.
    </description>
  
  <repositories>
    <repository>
      <id>scala-tools.org</id>
      <name>Scala-tools Maven2 Repository</name>
      <url>http://scala-tools.org/repo-releases</url>
    </repository>
    <repository>
        <id>jline</id>
        <name>JLine Project Repository</name>
        <url>http://jline.sourceforge.net/m2repo</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>

  <dependencies>
    <dependency>
	<groupId>org.scala-lang</groupId>
	<artifactId>scala-library</artifactId>
	<version>2.6.1</version>
    </dependency>
    <dependency>
	<groupId>jline</groupId>
	<artifactId>jline</artifactId>
	<version>0.9.94</version>
    </dependency>
  </dependencies>
  
  <build>
    <sourceDirectory>src/main/scala</sourceDirectory>
    <testSourceDirectory>src/test/scala</testSourceDirectory>

    <plugins>

      <plugin>
        <groupId>org.scala-tools</groupId>
        <artifactId>maven-scala-plugin</artifactId>
		<version>2.4</version>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
              <goal>testCompile</goal>
            </goals>
          </execution>
        </executions>
	    <configuration>
	      <mainClass>org.example.HelloWorld</mainClass>
	    </configuration>
      </plugin>
	
    </plugins>

  </build>
 
</project>

Some points of interest

Ok, so the first thing you might notice is that I have to have two explicit dependencies: jline and scala itself. maven-scala-plugin will compile your code without the scala dependency, but it cannot run the classes unless the jar is provided at runtime via a dependency. Jline I determined was required by simple trial and error.

The scala-maven-plugin site's documentation is incorrect. The group id is not "scala" as they claim, it is in fact: <groupId>org.scala-tools</groupId>

The sourceDirectory and testSourceDirectory do not have to be specified if you decide to use src/main/scala and src/test/scala (at least according to their documentation) but I have chosen to explicitly state them here anyway.

So, you need to put your scala code in src/main/scala and your tests in src/test/scala. The typical HelloWorld example could be saved to src/main/scala/HelloWorld.scala which looks like:

package org.example {
  object HelloWorld extends Application {
    println "hello"
  }
}
Then from the command line run:
mvn clean compile
mvn scala:run
And voila, you should see "hello" print to the console.

Leave a comment


Type the characters you see in the picture above.

0 TrackBacks

Listed below are links to blogs that reference this entry: Scala and Maven with maven-scala-plugin.

TrackBack URL for this entry: http://www.nearinfinity.com/mt/mt-tb.cgi/508