本文介绍了在Eclipse Mars 下运行Maven打包Scala 项目。由于实际工作中我们用到Spark,最后会顺带介绍如何打包Spark项目。
Scala官网上推荐sbt+Intellj,但是更加熟悉Eclipse加上Maven,因此使用这两个工具开发打包Spark项目。
在Eclipse上使用Maven打包Scala需要
一、首先,下载Eclipse Mars。下载完成后需要安装Scala IDE for Eclipse plugin。具体网站为:http://scala-ide.org/download/current.html。
Scala Plugin安装步骤为
1. 拷贝更新站点http://download.scala-ide.org/sdk/lithium/e44/scala211/stable/site
2. 打开Eclipse,Help -> Install New Software
3. 在Install界面选择Add按钮,Name为Scala 2.11,Location为步骤1的URL
4.勾选需要的选项并Next到Finish
二、在Eclipse Windows-> Preference 页面上查看是否有Maven,没有需要安装Maven。安装Maven步骤在此不再详述
三、接下来是安装m2eclipse-scala,安装m2eclipse-scala步骤和安装Scala IDE类似,具体URL为http://alchim31.free.fr/m2e-scala/update-site/
安装结束m2eclipse-scala后,在Windows->Preference->Maven->Archetypes里选择Add Remote Catalog。
Catalog File: http://repo.maven.apache.org/maven2/archetype-catalog.xml,Description:Remote Catalog。
自此安装步骤结束。
四、接下来创建Maven项目。File->New->Maven Project。在Catalog中选择之前创建的Remote Catalog,Filter为scala。
找到net.alchim31.maven点Next,在下一个页面输入项目Group Name 和Artifact Name后完成创建Maven Project。
Maven项目结构如下图所示:
右击项目 ->Build Path -> Config Build Path -> Library -> Add Library -> Scala Library,选择要添加的Scala Library。
现在可以创建Scala程序了。如果需要编写Spark项目,还需要在Library中导入Spark需要的jar包,可以从Spark官网上下载。
五、程序编写完成后到最重要的一步了:用Maven打包程序。
打开pom.xml,修改<properties>中用到的Scala version:
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
<encoding>UTF-8</encoding>
<scala.version>2.10.4</scala.version>
<scala.compat.version>2.10</scala.compat.version>
</properties>
在<dependencies>中添加Spark和Scala的dependency:
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.10.4</version>
</dependency>
<dependency> <!-- Spark dependency -->
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.10</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming_2.10</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_2.10</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming-kafka_2.10</artifactId>
<version>1.5.0</version>
</dependency>
在<Build>中添加<plugins>:
<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>-make:transitive</arg>
<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>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
最后一个<plugin>作用是打包Scala程序和所用到的jar包一起到一个jar文件。
然后右键该项目->Run As->Maven build,Goals输入assembly:assembly。其中assembly用到最后一个plugin。
如果仅需要打包自己写的程序,Goals输入compile。
在项目文件夹target目录下会看到生成的jar文件。完成。
参考文档:
http://blog.cloudera.com/blog/2014/04/how-to-run-a-simple-apache-spark-app-in-cdh-5/
http://scala-ide.org/docs/tutorials/m2eclipse/
https://maven.apache.org/guides/getting-started/index.html
bbs.ttxit.com/forum.php?mod=viewthread&tid=88