Add Scala To An Existing Maven Project

By: Stephen Patrick | 16 Aug 2016 | Category: Scala Java Integration

Scala: Add To Existing Java Maven Project

Scala integrates well with Java. Scala can be added to an existing Java Maven project by adding Scala as a dependency. This involves adding a couple of Scala dependencies and the Scala Maven Plugin.

<dependency>
   <groupId>org.scala-lang</groupId>
   <artifactId>scala-library</artifactId>
   <version>2.10.0</version>
</dependency>

Above we add the Scala dependency. For testing we will add a couple more dependencies. We add dependencies for jUnit and scalatest which is a library that supports JUnit etc.

<dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>4.11</version>
   <scope>test</scope>
</dependency>



<dependency>
   <groupId>org.scalatest</groupId>
   <artifactId>scalatest_2.10</artifactId>
   <version>2.0/version>
   <scope>test</scope>
</dependency>

We also must add the scala-maven-plugin.

<plugin>
   <groupId>net.alchim31.maven</groupId>
   <artifactId>scala-maven-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>