在Maven项目中配置Scala

要在Maven项目中也可以用Scala,并且可以与Java代码共存,只需要在项目的pom.xml中添加以下代码:

配置Scala的版本

1
2
3
4
<properties>
<scala.version>2.11.7</scala.version>
<scala.compat.version>2.11</scala.compat.version>
</properties>

配置Scala的SDK

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

配置Scala插件(用于编译Scala代码)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.2</version>
<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>

注意:在与java代码共存的项目中,不要配置源文件目录,否则java代码不编译。