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.






Before we start, you can
Recent comments