使用 maven 创建 scala 项目问题总结

教程 青牛 ⋅ 于 2017-01-04 15:16:56 ⋅ 9018 阅读

使用maven创建scala项目,scala-archetype-simple有bug,会遇到一些问题,这里整理记录一下。

我的环境是:

maven 3.3.9
eclipse 4.6
java 1.8

通过命令行的形式创建 scala项目:

mvn archetype:generate -B  \
  -DarchetypeGroupId=net.alchim31.maven -DarchetypeArtifactId=scala-archetype-simple -DarchetypeVersion=1.6 \
  -DgroupId=com.hainiubl.scala -DartifactId=scala-demo -Dversion=1.0 -Dpackage=com.hainiubl.scala.demo

命令执行完后的目录结构:

➜  scala-demo tree
.
├── pom.xml
├── src
│   ├── main
│   │   └── scala
│   │       └── com
│   │           └── hainiubl
│   │               └── scala
│   │                   └── demo
│   │                       └── App.scala
│   └── test
│       └── scala
│           └── samples
│               ├── junit.scala
│               ├── scalatest.scala
│               └── specs.scala
└── target

使用scala 2.11 编译工程会有问题:

  • scala 2.11不支持这个make参数了,从pom.xml中把这个参数去掉
[ERROR] scalac error: bad option: '-make:transitive'
  • 生成的pom.xml缺少一个依赖
[ERROR] /Users/sandy/workspace/scala-demo/src/test/scala/samples/specs.scala:18: error: not found: type JUnitRunner
[ERROR] @RunWith(classOf[JUnitRunner])
[ERROR]                  ^
[ERROR] one error found

在pom.xml中增加

    <dependency>
        <groupId>org.specs2</groupId>
        <artifactId>specs2-junit_${scala.compat.version}</artifactId>
        <version>2.4.16</version>
        <scope>test</scope>
    </dependency>
  • java 和 scala的版本可以修改成你想要的版本
    我这里改成了1.8

修改后完整的pom.xml

<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.hainiubl.scala</groupId>
  <artifactId>scala-demo</artifactId>
  <version>1.0</version>
  <name>${project.artifactId}</name>
  <description>My wonderfull scala app</description>
  <inceptionYear>2015</inceptionYear>
  <licenses>
    <license>
      <name>My License</name>
      <url>http://....</url>
      <distribution>repo</distribution>
    </license>
  </licenses>

  <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <encoding>UTF-8</encoding>
    <scala.version>2.11.8</scala.version>
    <scala.compat.version>2.11</scala.compat.version>
  </properties>

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

    <!-- Test -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.specs2</groupId>
      <artifactId>specs2-core_${scala.compat.version}</artifactId>
      <version>2.4.16</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.specs2</groupId>
        <artifactId>specs2-junit_${scala.compat.version}</artifactId>
        <version>2.4.16</version>
        <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.scalatest</groupId>
      <artifactId>scalatest_${scala.compat.version}</artifactId>
      <version>2.2.4</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <sourceDirectory>src/main/scala</sourceDirectory>
    <testSourceDirectory>src/test/scala</testSourceDirectory>
    <plugins>
      <plugin>
        <!-- see http://davidb.github.com/scala-maven-plugin -->
        <groupId>net.alchim31.maven</groupId>
        <artifactId>scala-maven-plugin</artifactId>
        <version>3.2.0</version>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
              <goal>testCompile</goal>
            </goals>
            <configuration>
              <args>
                <arg>-dependencyfile</arg>
                <arg>${project.build.directory}/.scala_dependencies</arg>
              </args>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.18.1</version>
        <configuration>
          <useFile>false</useFile>
          <disableXmlReport>true</disableXmlReport>
          <!-- If you have classpath issue like NoDefClassError,... -->
          <!-- useManifestOnlyJar>false</useManifestOnlyJar -->
          <includes>
            <include>**/*Test.*</include>
            <include>**/*Suite.*</include>
          </includes>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

打包编译一下:

mvn package

通过eclipse创建 scala项目:

默认没有scala的archetype,创建maven项目时自己指定一下:

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

1.5或1.6都可以,创建好项目后自己可以修改相应版本
scala-archetype-simple源码
file

其它修改可以参考上面的 pom.xml,道理是一样的。

pom.xml 还有可能会报错:

Multiple annotations found at this line:
    - Plugin execution not covered by lifecycle configuration: net.alchim31.maven:scala-maven-plugin:3.2.0:testCompile (execution: 
     default, phase: test-compile)
    - Plugin execution not covered by lifecycle configuration: net.alchim31.maven:scala-maven-plugin:3.2.0:compile (execution: 
     default, phase: compile)

解决办法
eclipse 菜单 Window->Preference->Maven->Errors/Warnings->Plugin execution not covered by lifecycle configuration 改成 ignore

版权声明:原创作品,允许转载,转载时务必以超链接的形式表明出处和作者信息。否则将追究法律责任。来自海汼部落-青牛,http://hainiubl.com/topics/39
点赞
成为第一个点赞的人吧 :bowtie:
回复数量: 0
    暂无评论~~
    • 请注意单词拼写,以及中英文排版,参考此页
    • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`, 更多语法请见这里 Markdown 语法
    • 支持表情,可用Emoji的自动补全, 在输入的时候只需要 ":" 就可以自动提示了 :metal: :point_right: 表情列表 :star: :sparkles:
    • 上传图片, 支持拖拽和剪切板黏贴上传, 格式限制 - jpg, png, gif,教程
    • 发布框支持本地存储功能,会在内容变更时保存,「提交」按钮点击时清空
    Ctrl+Enter