IntelliJ IDEA 中创建 Maven Scala 项目

Scala 项目看家的构建工具当然是 SBT, 假如我们已习惯于 Maven, 想要用 Maven 来构建 Scala 项目该如何做呢?那首先要找到一个 Maven Scala 相应的 Archetype, 然后用命令 mvn archetype:generate 或是在 IntelliJ IDEA 使用 Maven 项目创建向导来选择一个 Maven Scala Archetype。这里主要介绍 IntelliJ IDEA 中 Maven 向导创建 Scala 项目的方式。


首先确保我们已安装了 IntelliJ IDEA 的 Maven 和  Scala 插件。插件中自带了 org.scala-tools.archetypes:scala-archetype-simple:1.2 的 Maven archetype,  这是一个貌视 Scala 官方的 archetype。我们可以尝试基于它来创建一个 Maven Scala 项目。通过 IntelliJ IDEA 的菜单 File/New/Project...,  在弹出的窗口中选择 Maven/Create from archetype, 然后找到 scala-archetype-simple,  自带版本为 1.2, 当前最新版也不过 1.3, 那还是  2010 年建立,别提有多老了。

创建出来项目的 pom.xml 也不出所料
1<properties>
2    <maven.compiler.source>1.5</maven.compiler.source>
3    <maven.compiler.target>1.5</maven.compiler.target>
4    <encoding>UTF-8</encoding>
5    <scala.version>2.8.0</scala.version>
6  </properties>

一切都太老了,实际上在当今的 IntelliJ IDEA(2017.2.6) 中该项目也无法运行。

所以我们需寻求更新的 Maven Scala Archetype,于是找到了 net.alchim31.maven:scala-archetype-simple:1.6,  它的版本号是  1.4, 1.5, 1.6,看来是来给官方的 scala-archetype-simple 续命来的,它的项目在 github 上可以找到 davidB/scala-archetype-simple

我们需要在创建项目时把该 archetype 添加到 IntelliJ IDEA 中来,还是同样的创建 Maven 项目的向导,只是这时候我们要点击 Add Archetype... 按钮,在窗口中输入相应的

GroupId: net.alchim31.maven
ArtifactId: scala-archetype-simple
Version: 1.6

添加完后,我们选择该 Maven Archetype 来创建项目,输入自己项目的 GroupId, ArtifactId 等信息,生成项目后,我们可以查看一个 pom.xml 文件
1<properties>
2    <maven.compiler.source>1.6</maven.compiler.source>
3    <maven.compiler.target>1.6</maven.compiler.target>
4    <encoding>UTF-8</encoding>
5    <scala.version>2.11.5</scala.version>
6    <scala.compat.version>2.11</scala.compat.version>
7  </properties>

好像还不怎么新,可以自己把 Java 的改为 1.8, 或 Scala 的版本改为 2.12.* 的版本,改完了需作测试。

基于原始状态,我试着运行该项目的 App.scala, 报出错误
Error:scalac: bad option: '-make:transitive'
把 pom.xml 中的
<arg>-make:transitive</arg>
删掉,再运行,错误是
Error:(18, 18) not found: type JUnitRunner
@RunWith(classOf[JUnitRunner])

需要引入依赖
1<dependency>
2    <groupId>org.specs2</groupId>
3    <artifactId>specs2-junit_${scala.compat.version}</artifactId>
4    <version>3.7.2</version>
5    <scope>test</scope>
6</dependency>

并且在 specs.scala 测试类中引入 JUnitRunner
import org.specs2.runner.JUnitRunner
下面是我修复后的完整的 pom.xml 内容
 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/maven-v4_0_0.xsd">
 3  <modelVersion>4.0.0</modelVersion>
 4  <groupId>cc.unmi</groupId>
 5  <artifactId>spark-test</artifactId>
 6  <version>1.0-SNAPSHOT</version>
 7  <name>${project.artifactId}</name>
 8  <description>My wonderfull scala app</description>
 9  <inceptionYear>2015</inceptionYear>
10  <licenses>
11    <license>
12      <name>My License</name>
13      <url>http://....</url>
14      <distribution>repo</distribution>
15    </license>
16  </licenses><br/><br/>
17  <properties>
18    <maven.compiler.source>1.8</maven.compiler.source>
19    <maven.compiler.target>1.8</maven.compiler.target>
20    <encoding>UTF-8</encoding>
21    <scala.version>2.11.7</scala.version>
22    <scala.compat.version>2.11</scala.compat.version>
23  </properties><br/><br/>
24  <dependencies>
25    <dependency>
26      <groupId>org.scala-lang</groupId>
27      <artifactId>scala-library</artifactId>
28      <version>${scala.version}</version>
29    </dependency><br/><br/>
30    <!-- Test -->
31    <dependency>
32      <groupId>junit</groupId>
33      <artifactId>junit</artifactId>
34      <version>4.12</version>
35      <scope>test</scope>
36    </dependency>
37    <dependency>
38      <groupId>org.specs2</groupId>
39      <artifactId>specs2-core_${scala.compat.version}</artifactId>
40      <version>3.7.2</version>
41      <scope>test</scope>
42    </dependency>
43    <dependency>
44        <groupId>org.specs2</groupId>
45        <artifactId>specs2-junit_${scala.compat.version}</artifactId>
46        <version>3.7.2</version>
47        <scope>test</scope>
48    </dependency>
49    <dependency>
50      <groupId>org.scalatest</groupId>
51      <artifactId>scalatest_${scala.compat.version}</artifactId>
52      <version>3.0.4</version>
53      <scope>test</scope>
54    </dependency>
55  </dependencies><br/><br/>
56  <build>
57    <sourceDirectory>src/main/scala</sourceDirectory>
58    <testSourceDirectory>src/test/scala</testSourceDirectory>
59    <plugins>
60      <plugin>
61        <!-- see http://davidb.github.com/scala-maven-plugin -->
62        <groupId>net.alchim31.maven</groupId>
63        <artifactId>scala-maven-plugin</artifactId>
64        <version>3.3.1</version>
65        <executions>
66          <execution>
67            <goals>
68              <goal>compile</goal>
69              <goal>testCompile</goal>
70            </goals>
71            <configuration>
72              <args>
73                <arg>-dependencyfile</arg>
74                <arg>${project.build.directory}/.scala_dependencies</arg>
75              </args>
76            </configuration>
77          </execution>
78        </executions>
79      </plugin>
80      <plugin>
81        <groupId>org.apache.maven.plugins</groupId>
82        <artifactId>maven-surefire-plugin</artifactId>
83        <version>2.20.1</version>
84        <configuration>
85          <useFile>false</useFile>
86          <disableXmlReport>true</disableXmlReport>
87          <!-- If you have classpath issue like NoDefClassError,... -->
88          <!-- useManifestOnlyJar>false</useManifestOnlyJar -->
89          <includes>
90            <include>**/*Test.*</include>
91            <include>**/*Suite.*</include>
92          </includes>
93        </configuration>
94      </plugin>
95    </plugins>
96  </build>
97</project>

下一步,将会把这个修复好的项目作为一个 g8 的项目模板。

Dec 8, 2017: 做成了一个 giter8(g8) 的 Maven Scala 2.11 项目模板,之所以特定于 Scala 2.11.x, 是因为换成 Scala 2.12.x 后其他的依赖也改动很大,目前主流上像 Spark(当前版本 2.2) 还是只支持 Scala 2.11.

如果安装了 giter8 的话,只需要输入一行命令
1$ g8 yabqiu/maven-scala2.11-archetype
2
3Maven Scala 2.11 project template
4
5name [Maven Scala Project]: My Scala Test
6package [cc.unmi]: 
7version [1.0-SNAPSHOT]:
8
9Template applied in /Users/yanbin/Workspaces/./my-scala-test

再回答三个关于项目名,包名,版本的问题,一个基本的 Maven Scala 2.11 的项目便立马呈现在了你的面前。

关于  giter8(g8) 的使用请参考我一年多前的一篇日志:Giter8 -- 把项目布局模板放到 GitHub 上

链接:

  1. Creating a Scala Project with Maven Dependency Management for Gatling Testing in IntelliJ IDEA
  2. spark-in-action/scala-archetype-sparkinaction
  3. SCALA WITH MAVEN

  永久链接 https://yanbin.blog/intellij-idea-create-maven-scala-project/, 来自 隔叶黄莺 Yanbin's Blog
[版权声明] 本文采用 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 进行许可。