Mevan_scala 的小bug 的meansure

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

我的环境是:

maven 3.3.9eclipse 4.6java 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
 
 

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


 
 
  1.    scala-demo  tree.├──  pom .xml
  2. ├──  src
  3. │   ├──  main
  4. │   │   └──  scala
  5. │   │       └──  com
  6. │   │           └──  hainiubl
  7. │   │               └──  scala
  8. │   │                   └──  demo
  9. │   │                       └──  App .scala
  10. │   └──  test
  11. │       └──  scala
  12. │           └──  samples
  13. │               ├──  junit .scala
  14. │               ├──  scalatest .scala
  15. │               └──  specs .scala
  16. └──  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:18errornot found: type JUnitRunner[ERROR] @RunWith(classOf[JUnitRunner])[ERROR]                  ^[ERROR] one error found
 
 

在pom.xml中增加


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

修改后完整的pom.xml


 
 
  1. <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">
  2.    <modelVersion>4.0.0 </modelVersion>
  3.    <groupId>com.hainiubl.scala </groupId>
  4.    <artifactId>scala-demo </artifactId>
  5.    <version>1.0 </version>
  6.    <name>${project.artifactId} </name>
  7.    <description>My wonderfull scala app </description>
  8.    <inceptionYear>2015 </inceptionYear>
  9.    <licenses>
  10.      <license>
  11.        <name>My License </name>
  12.        <url>http://.... </url>
  13.        <distribution>repo </distribution>
  14.      </license>
  15.    </licenses>
  16.    <properties>
  17.      <maven.compiler.source>1.8 </maven.compiler.source>
  18.      <maven.compiler.target>1.8 </maven.compiler.target>
  19.      <encoding>UTF-8 </encoding>
  20.      <scala.version>2.11.8 </scala.version>
  21.      <scala.compat.version>2.11 </scala.compat.version>
  22.    </properties>
  23.    <dependencies>
  24.      <dependency>
  25.        <groupId>org.scala-lang </groupId>
  26.        <artifactId>scala-library </artifactId>
  27.        <version>${scala.version} </version>
  28.      </dependency>
  29.      <!-- Test -->
  30.      <dependency>
  31.        <groupId>junit </groupId>
  32.        <artifactId>junit </artifactId>
  33.        <version>4.12 </version>
  34.        <scope>test </scope>
  35.      </dependency>
  36.      <dependency>
  37.        <groupId>org.specs2 </groupId>
  38.        <artifactId>specs2-core_${scala.compat.version} </artifactId>
  39.        <version>2.4.16 </version>
  40.        <scope>test </scope>
  41.      </dependency>
  42.      <dependency>
  43.          <groupId>org.specs2 </groupId>
  44.          <artifactId>specs2-junit_${scala.compat.version} </artifactId>
  45.          <version>2.4.16 </version>
  46.          <scope>test </scope>
  47.      </dependency>
  48.      <dependency>
  49.        <groupId>org.scalatest </groupId>
  50.        <artifactId>scalatest_${scala.compat.version} </artifactId>
  51.        <version>2.2.4 </version>
  52.        <scope>test </scope>
  53.      </dependency>
  54.    </dependencies>
  55.    <build>
  56.      <sourceDirectory>src/main/scala </sourceDirectory>
  57.      <testSourceDirectory>src/test/scala </testSourceDirectory>
  58.      <plugins>
  59.        <plugin>
  60.          <!-- see http://davidb.github.com/scala-maven-plugin -->
  61.          <groupId>net.alchim31.maven </groupId>
  62.          <artifactId>scala-maven-plugin </artifactId>
  63.          <version>3.2.0 </version>
  64.          <executions>
  65.            <execution>
  66.              <goals>
  67.                <goal>compile </goal>
  68.                <goal>testCompile </goal>
  69.              </goals>
  70.              <configuration>
  71.                <args>
  72.                  <arg>-dependencyfile </arg>
  73.                  <arg>${project.build.directory}/.scala_dependencies </arg>
  74.                </args>
  75.              </configuration>
  76.            </execution>
  77.          </executions>
  78.        </plugin>
  79.        <plugin>
  80.          <groupId>org.apache.maven.plugins </groupId>
  81.          <artifactId>maven-surefire-plugin </artifactId>
  82.          <version>2.18.1 </version>
  83.          <configuration>
  84.            <useFile>false </useFile>
  85.            <disableXmlReport>true </disableXmlReport>
  86.            <!-- If you have classpath issue like NoDefClassError,... -->
  87.            <!-- useManifestOnlyJar>false</useManifestOnlyJar -->
  88.            <includes>
  89.              <include>**/*Test.* </include>
  90.              <include>**/*Suite.* </include>
  91.            </includes>
  92.          </configuration>
  93.        </plugin>
  94.      </plugins>
  95.    </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 还有可能会报错:


 
 
  1. Multiple  annotations  found  at  this  line:
  2.      -  Plugin  execution  not  covered  by  lifecycle  configurationnet .alchim31 .maven :scala-maven-plugin :3.2.0 :testCompile ( execution
  3.       defaultphasetest-compile)
  4.      -  Plugin  execution  not  covered  by  lifecycle  configurationnet .alchim31 .maven :scala-maven-plugin :3.2.0 :compile ( execution
  5.       defaultphasecompile)

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

转载:水牛 http://hainiubl.com/topics/39

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值