使用 Maven 创建最基本的 Scala 项目?

Creating the most basic Scala project with Maven?

我使用 Maven 3 创建一个新的 Scala 项目。据我了解,使用 Maven 创建新项目的方法是:

mvn archetype:generate

也许我遗漏了什么,但我什至找不到一个提供最简单的 Scala 项目的选项(例如 lein new app ... 收到的用于 Clojure 的项目)。有什么帮助吗?

您应该可以使用 mvn archetype:generate。您可以选择,例如 org.scala-tools.archetypes:scala-archetype-simple。您需要在 mvn archetype:generate 命令 的输出中的原型名称 旁边输入编号,因为编号会随着时间而改变。还有其他选项,如 eu.stratosphere:quickstart-scala,如 this article.

中所述

不过,它们可能有些过时了。我个人更喜欢手动编写 pom.xml 文件。作为参考,这是一个用于 Scala 2.11.6 和 Scalatest 2.2.5 的最小 pom 文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>my-artifact</artifactId>
  <version>1.0-SNAPSHOT</version>

  <properties>
    <encoding>UTF-8</encoding>
    <scala.version>2.11.6</scala.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.scala-lang</groupId>
      <artifactId>scala-library</artifactId>
      <version>${scala.version}</version>
    </dependency>

    <dependency>
      <groupId>org.scalatest</groupId>
      <artifactId>scalatest_2.11</artifactId>
      <version>2.2.5</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.scala-tools</groupId>
        <artifactId>maven-scala-plugin</artifactId>
        <version>2.15.2</version>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
              <goal>testCompile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

      <plugin>
        <groupId>org.scalatest</groupId>
        <artifactId>scalatest-maven-plugin</artifactId>
        <version>1.0</version>
        <configuration>
        </configuration>
        <executions>
          <execution>
            <id>test</id>
            <goals>
              <goal>test</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

    </plugins>

  </build>
</project>

主要的 Scala 原型在最知名的地址下看起来已停产。 这个原型看起来好多了:

https://github.com/davidB/scala-archetype-simple

现在使用它:

mvn archetype:generate -DarchetypeGroupId=net.alchim31.maven -DarchetypeArtifactId=scala-archetype-simple