温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

maven中怎么创建一个scala 项目

发布时间:2021-07-28 17:19:29 来源:亿速云 阅读:263 作者:Leah 栏目:开发技术

这期内容当中小编将会给大家带来有关maven中怎么创建一个scala 项目,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

使用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

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

  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源码
其它修改可以参考上面的 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)

上述就是小编为大家分享的maven中怎么创建一个scala 项目了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI