We have small scala program:
// HelloWorld.scala
object HelloWorld {
def main(args: Array[String]) = {
Console.println("Hello, world!")
}
}
and want to compile/run it. In order to do it, we have to perform some additional steps.
Compile/run with Ant
a). Scala is preinstalled
You need to have "scala-library" and "scala compiler" libraries in order to compile Scala programs.
You can download them from Scala web site (http://scala-lang.org).
Your ant script will start with the following lines:
<!-- 1. Define common properties. -->
<property name="src.dir" value="src/main/scala"/>
<property name="build.dir" value="target/classes"/>
<property name="repository.home" value="c:/maven-repository"/>
<property name="scala-compiler.jar" value="${repository.home}/scala/scala-compiler/2.4.0/scala-compiler-2.4.0.jar"/>
<property name="scala-library.jar" value="${repository.home}/scala/scala-library/2.4.0/scala-library-2.4.0.jar"/>
<!-- 2. Define Scala CLASSPATH. -->
<path id="scala.classpath">
<pathelement location="${scala-compiler.jar}"/>
<pathelement location="${scala-library.jar}"/>
</path>
<!-- 3. Define project CLASSPATH. -->
<path id="project.classpath">
<path refid="scala.classpath"/>
<pathelement location="${build.dir}"/>
</path>
<!-- 4. Define scala compiler command. -->
<taskdef resource="scala/tools/ant/antlib.xml">
<classpath refid="scala.classpath"/>
</taskdef>
Now you can compile sources by using "scalac" command:
<!-- 5. Compiles sources by using "scalac" command. -->
<target name="compile">
<mkdir dir="${build.dir}"/>
<scalac srcdir="${src.dir}" destdir="${build.dir}" classpathref="project.classpath" force="changed">
<include name="**/*.scala"/>
</scalac>
</target>
Once compiled, you can run scala program:
<!-- 6. Runs scala executable. -->
<target name="run" depends="compile">
<java classname="scala.tools.nsc.MainGenericRunner" fork="true">
<classpath>
<path refid="project.classpath"/>
</classpath>
<arg line="HelloWorld"/>
</java>
</target>
Run this command:
>ant run
It will compile and then run scala example.
b). scala is being installed by maven 2
If you don't want to install scala libraries manually, you can use maven tasks for ant
(see http://maven.apache.org/ant-tasks.html for further details):
<project name="scala-compile-test2" default="compile" basedir="."
xmlns:artifact="antlib:org.apache.maven.artifact.ant">
<!-- 1. Define common properties. -->
<property name="src.dir" value="src/main/scala"/>
<property name="build.dir" value="target/classes"/>
<!-- 2. Define Scala CLASSPATH with the help of Maven 2. -->
<!--property name="repository.home" value="c:/maven-repository"/-->
<artifact:localRepository id="local.repository" location="${repository.home}" layout="default"/>
<artifact:pom file="scala-compile-test2.maven" id="maven.project" />
<artifact:dependencies pathId="scala.classpath" filesetId="compile.fileset" useScope="compile">
<pom refid="maven.project"/>
</artifact:dependencies>
Steps 3-6 are the same as in previous example.
Compile/run with Maven2
There are 2 maven plugings to work with scala sources. First plugin is implemented as
standard extension to the plexus compiler
(http://svn.codehaus.org/plexus/plexus-components/trunk/plexus-compiler/plexus-compilers).
The extension is located in the following repository:
http://scriptlandia-repository.googlecode.com/svn/trunk/languages
Another plugin for scala is implemented as regular maven plugin
(see details here: http://millstone.iodp.tamu.edu/~blambi/maven-scala-plugin).
a). plexus-compiler-scalac
First, you have to specify the location of scala sources. We'll do it with the help of
"build-helper-maven-plugin":
<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>
Then you configure "maven-compiler-plugin" plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerId>scalac</compilerId>
<includes>
<include>**</include>
</includes>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-compiler-scalac</artifactId>
<version>1.5.3</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</plugin>
And don't forget about right repository:
<repositories>
<repository>
<id>scriptlandia-repo</id>
<name>Scriptlandia Maven2 repository</name>
<url>http://scriptlandia-repository.googlecode.com/svn/trunk/languages</url>
</repository>
</repositories>
Now, compilation is easy:
>mvn compile
In order to run compiled code we'll use "exec" maven plugin:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>HelloWorld</mainClass>
</configuration>
</plugin>
And run it:
>mvn exec:java
b). maven-scala-plugin
This plugin contains 2 parts: compiler and runner. It requires small configuration:
<plugin>
<groupId>iodp.usio</groupId>
<artifactId>maven-scala-plugin</artifactId>
<configuration>
<mainClass>HelloWorld</mainClass>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
You also have to specify the location of this plugin:
<pluginRepositories>
<pluginRepository>
<id>maven2.iodp.usio</id>
<name>IODP Maven2 Repository</name>
<url>http://millstone.iodp.tamu.edu/maven2</url>
</pluginRepository>
</pluginRepositories>
Now you will be able to compile and run scala code now:
>mvn scala:compile
>mvn scala:run -DmainClass=HelloWorld