Snippet: Mixing Scala, Java in a Maven Project
March 23rd, 2012 by Micha KopsHaving just returned from the Atlassian Camp 2012 I just toyed around with Java and Scala and wanted to share the following snippet that demonstrates how to mix code from both languages in a Maven project using the maven-scala-plugin.
Contents
Setting up the Maven Project
First create a new Maven project in your IDE or by running mvn archetype:generate.
In the next step, add the dependency for scala-library and the scala maven repositories to your pom.xml and hook the maven-scala-plugin to Maven’s lifecycle. My pom.xml finally looks like this one:
<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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.hascode.tutorial</groupId> <artifactId>scala-maven-tutorial</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-library</artifactId> <version>2.9.1</version> </dependency> </dependencies> <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> <build> <pluginManagement> <plugins> <plugin> <groupId>org.scala-tools</groupId> <artifactId>maven-scala-plugin</artifactId> <version>2.9.1</version> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.0.2</version> </plugin> </plugins> </pluginManagement> <plugins> <plugin> <groupId>org.scala-tools</groupId> <artifactId>maven-scala-plugin</artifactId> <executions> <execution> <id>scala-compile-first</id> <phase>process-resources</phase> <goals> <goal>add-source</goal> <goal>compile</goal> </goals> </execution> <execution> <id>scala-test-compile</id> <phase>process-test-resources</phase> <goals> <goal>testCompile</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <executions> <execution> <phase>compile</phase> <goals> <goal>compile</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
Finally add these both source folders for your Scala code to your project directory:
- src/main/scala
- src/test/scala
Writing some code
Now we need some code for both languages .. first our Java class providing a service that returns a simple string
package com.hascode.tutorial.service; public class SomeJavaService { public String getInfo() { return "this is from java.."; } }
Afterwards we’re creating our scala class in src/main/scala
package com.hascode.tutorial import com.hascode.tutorial.service._ object Main { def main(args: Array[String]) { println("We're running scala..") val service = new SomeJavaService println(service.getInfo()) } }
Running the Application
Finally build and run the application using Maven..
mvn clean package exec:java -Dexec.mainClass=com.hascode.tutorial.Main
This should yield the following output ..
We're running scala..
this is from java..
Tutorial Sources
I have put the source from this tutorial on my Bitbucket repository – download it there or check it out using Mercurial:
hg clone https://bitbucket.org/hascode/scala-maven-tutorial
Resources
import com.hascode.tutorial.service._
object Main {
def main(args: Array[String]) {
println(“We’re running scala..”)
val service = new SomeJavaService
println(service.getInfo())
}
}