使用eclispe编写scala项目时需搭建scala环境、eclispe安装scala插件
1.win10下搭建scala环境
安装包尽量与spark版本中使用的scala保持一致
解压缩至指定目录下(D:\dev)
配置环境变量(右击此电脑 -- 属性 -- 高级系统设置 -- 环境变量 -- 系统环境变量 -- 新建SCALA_HOME)
设置系统环境变量:SCALA_HOME
Path中添加 %SCALA_HOME%\bin
验证是否配置成功(win+R -- cmd)
查看版本:scala -version
进入scala shell界面
scala
退出命令行 :quit
查看帮助 :help
2.eclipse安装scala插件(注意eclispe版本号与插件对应)
scala-eclispe插件下载地址: http://scala-ide.org/download/prev-stable.html
下载 压缩包 解压
打开已有的eclipse
点击help——install new software
点击Add
选择name后面的Local按钮
选择刚才的解压目录
全选 一路next 接收协议 finsh 重启ecipse即可
3.配置eclipse中的scala library
打开eclispe -- window -- preferences -- scala -- Installations -- add 添加所需scala版本
点击 ok即可(上面的情况属于已安装)
4.创建maven项目
创建普通maven项目 -- 右击点击Configure 添加 scala nature 选择相应版本
配置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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mace</groupId>
<artifactId>scala.common</artifactId>
<version>0.0.1-SNAPSHOT</version>
<description>scala-2.11.8入门学习</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<maven.scala.plugin.version>2.15.2</maven.scala.plugin.version>
<scala.version>2.11.8</scala.version>
<spark.version>2.3.0</spark.version>
</properties>
<dependencies>
<!-- 如果本地有scala环境可不需要下面这三个依赖。直接依赖本地scala环境即可 -->
<!-- scala-library Scala编程语言的标准库 -->
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}</version>
</dependency>
<!-- scala-compiler Scala编程语言的编译器 -->
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-compiler</artifactId>
<version>${scala.version}</version>
</dependency>
<!-- scala-reflect -->
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-reflect</artifactId>
<version>${scala.version}</version>
</dependency>
<!-- spark-core -->
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.11</artifactId>
<version>${spark.version}</version>
<scope>provided</scope>
</dependency>
<!-- redis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<!-- maven-scala-plugin 编译scala源代码 mvn clean scala:compile compile package -->
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<version>${maven.scala.plugin.version}</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- 打入依赖 -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>assembly</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<!--This plugin's configuration is used to store Eclipse m2e settings
only. It has no influence on the Maven build itself. -->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.scala-tools</groupId>
<artifactId>
maven-scala-plugin
</artifactId>
<versionRange>
[2.15.2,)
</versionRange>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
编写测试环境App
在src/test/java目录下创建scala.test包
新建EnvTest.scala 类
package scala.test
import org.junit.Assert
import org.junit.Test
class EnvTest extends Assert {
@Test
def helloworld:Unit = {
println("hello world!")
}
}
右击 Run As -- Scala JUnit Test 运行