In this tutorial you learn how to integrate Scala in your existing Maven Java projects. From configuring the project’s pom file to adding your first Scala code. Tip: Try to hide this inside a large code commit, and your co-workers will not notice you have added Scala support.. surprise!
Step 1: Configure the Maven Scala Plugin in your pom.xml
To building the Scala source code into Java bytecode, you can use the Maven Scala Plugin.
Add the <plugins> section somewhere in your pom.xml (in the <build> section):
<project> <build> <plugins> <plugin> <groupId>org.scala-tools</groupId> <artifactId>maven-scala-plugin</artifactId> <executions> <execution> <goals> <goal>compile</goal> <goal>testCompile</goal> </goals> </execution> </executions> </plugin> </plugins> </build> <project>
Alternative Step 1: Circular dependencies
Update: Do you need to build circular dependencies between Java and Scala? Put this in your pom.xml instead.
Add the <plugins> section somewhere in your pom.xml (in the <build> section):
<project> <build> <plugins> <plugin> <groupId>org.scala-tools</groupId> <artifactId>maven-scala-plugin</artifactId> <executions> <execution> <id>compile</id> <goals> <goal>compile</goal> </goals> <phase>compile</phase> </execution> <execution> <id>test-compile</id> <goals> <goal>testCompile</goal> </goals> <phase>test-compile</phase> </execution> <execution> <phase>process-resources</phase> <goals> <goal>compile</goal> </goals> </execution> </executions> </plugin> </plugins> </build> <project>
Step 2: Add Maven Scala Plugin repository
Before the Maven Scala Plugin can be loaded, your Maven installation needs to know where to find the plugin and the other Scala dependencies. These items can be found in the Scala Tools repository. Add the following to your project-specific pom.xml. (You can also choose to add this repository to your ~/.m2/settings.xml)
Add this to your pom.xml in the <project> section:
<pluginRepositories> <pluginRepository> <id>scala</id> <name>Scala Tools</name> <url>http://scala-tools.org/repo-releases/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories>
<repositories> <repository> <id>scala</id> <name>Scala Tools</name> <url>http://scala-tools.org/repo-releases/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories>
Step 3: Add Scala dependency to your pom.xml
Now that all Scala repositories and the Maven Scala plugin is configured, you can add the Scala dependency to your project’s pom.xml. Add the following code to the >dependencies> section of your pom.xml:
<dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-library</artifactId> <version>2.7.2</version> </dependency>
Step 4: Add some Scala code
The Maven Scala plugin expects your Scala code in
Create the scala directory.
Put the following code in a file called
object HelloWorld { def main(args: Array[String]) { println("Hello, world!") } }
Ready!
You can now execute mvn package to build your project, mvn test to test your project, and the Scala class will get built. Let me know if you got it working, I’d love to hear your comments.
Very helpful article.
There’s already a Maven 2 archetype available; from the command-line, use
mvn archetype:generate
then choose
internal -> scala-archetype-simple (A simple scala project)
I ran this today; the current version of the archetype still points to Scala 2.7.0 (2.7.2 is the latest release), but otherwise it creates a simple sample project and POM to get started with.
I’d recommend thinking about additional useful archetypes which you could submit to the Scala team, for example, a Scala GUI project.
Regards
Patrick
Thanks! This archetype is really useful for people who have no existing Java Maven projects.
Great article Daan! Just thought I’d mention that if you wanted a project that supported inter-dependencies between Java + Scala, you need to bind the compile and testCompile goals to some phase *before* the java compile and testCompile goals are executed.
Hi Josh,
Thanks for the notice! I have updated the post with an alternative Step 1.
- Daan
Thanks for your article.
You can create Scala project with Eclipse Maven PlugIn, too.
Plz, check my article, thanks again.
http://kennyground.blogspot.com/2009/04/create-scala-project-using-eclipse.html
Good write-up, Daan.
If you were looking to add Spring and Hibernate to what you’ve already got here with Scala and Maven, I’ve written a blog about how to do that (along with some of the problems you might encounter) here:
http://grahamhackingscala.blogspot.com/2010/01/scala-spring-hibernate-maven-webapp-how.html
Cheers,
Graham.