Getting started with Maven and Scala

Even though Scala is commonly used with SBT, it's also possible to use it with Maven, particularly if you already have a Maven based project and you want to use Scala with it.

So, let's use a pom.xml for a simple java application like this:

 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 3  <modelVersion>4.0.0</modelVersion>
 4  <groupId>br.com.rodrigolazoti</groupId>
 5  <artifactId>scalaproject</artifactId>
 6  <version>0.0.1-SNAPSHOT</version>
 7  <packaging>jar</packaging>
 8 
 9  <properties>
10      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
11  </properties>
12 
13  <dependencies>
14      <dependency>
15          <groupId>junit</groupId>
16          <artifactId>junit</artifactId>
17          <version>4.11</version>
18          <scope>test</scope>
19      </dependency>
20  </dependencies>
21 </project>

Right, now let's set up the project’s pom file to adding support for Scala code.

Add scala-tools repository

The Maven needs to know where to find the plugin and the Scala library, so add this repository to pom.xml:

1 <repositories>
2   <repository>
3       <id>scala-tools</id>
4       <url>https://oss.sonatype.org/content/groups/scala-tools/</url>
5   </repository>
6 </repositories>

Add scala library dependency

The scala's version that I'll use for this example is 2.10.0.

Add this dependency to pom.xml:

1 <dependency>
2   <groupId>org.scala-lang</groupId>
3   <artifactId>scala-library</artifactId>
4   <version>2.10.0</version>
5 </dependency>

Add maven-scala-plugin

Add the maven-scala-plugin to pom.xml:

 1 <build>
 2  <sourceDirectory>src/main/scala</sourceDirectory>
 3  <testSourceDirectory>src/test/scala</testSourceDirectory>
 4  <plugins>
 5      <plugin>
 6          <groupId>org.scala-tools</groupId>
 7          <artifactId>maven-scala-plugin</artifactId>
 8          <version>2.15.2</version>
 9          <executions>
10              <execution>
11                  <goals>
12                      <goal>compile</goal>
13                      <goal>testCompile</goal>
14                  </goals>
15              </execution>
16          </executions>
17      </plugin>
18  </plugins>
19 </build>

Add some Scala code

Let's add some scala code to the project.

Create a file called /src/main/scala/App.scala and put the following code into it:

1 object App extends App {
2 
3   List("Hello ", "world", "!") foreach print 
4 
5 }

Conclusion

From now on you can execute mvn package to build your project or mvn test to test your project, and the Scala code will get built automatically. ;)

See below the complete project’s pom file:

 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 3  <modelVersion>4.0.0</modelVersion>
 4  <groupId>br.com.rodrigolazoti</groupId>
 5  <artifactId>scalaproject</artifactId>
 6  <version>0.0.1-SNAPSHOT</version>
 7  <packaging>jar</packaging>
 8 
 9  <properties>
10      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
11  </properties>
12 
13  <repositories>
14      <repository>
15          <id>scala-tools</id>
16          <url>https://oss.sonatype.org/content/groups/scala-tools/</url>
17      </repository>
18  </repositories>
19 
20  <dependencies>
21      <dependency>
22          <groupId>org.scala-lang</groupId>
23          <artifactId>scala-library</artifactId>
24          <version>2.10.0</version>
25      </dependency>
26 
27      <dependency>
28          <groupId>junit</groupId>
29          <artifactId>junit</artifactId>
30          <version>4.11</version>
31          <scope>test</scope>
32      </dependency>
33  </dependencies>
34 
35  <build>
36      <sourceDirectory>src/main/scala</sourceDirectory>
37      <testSourceDirectory>src/test/scala</testSourceDirectory>
38      <plugins>
39          <plugin>
40              <groupId>org.scala-tools</groupId>
41              <artifactId>maven-scala-plugin</artifactId>
42              <version>2.15.2</version>
43              <executions>
44                  <execution>
45                      <goals>
46                          <goal>compile</goal>
47                          <goal>testCompile</goal>
48                      </goals>
49                  </execution>
50              </executions>
51              <configuration>
52                  <args>
53                      <arg>-optimise</arg>
54                      <arg>-unchecked</arg>
55                      <arg>-deprecation</arg>
56                  </args>
57                  <charset>UTF-8</charset>
58              </configuration>
59          </plugin>
60      </plugins>
61  </build>
62 </project>

comments powered by Disqus