一:Scala简介
Scala是一种运行在JVM上的一种编程语言(运行在JVM上的编程语言还有Groovy和Kotlin),即最终都会编译成.class文件。
Scala编程语言的特点:
- 面向对象
- 函数式编程
- 开发效率高,表达能力强:scala一行代码抵java多行代码
哪些框架是使用Scala开发的:
- Spark是使用Scala编程语言开发的。开发Spark程序可以使用Java、Python和Scala,但是Scala是最优雅的方式
- Kafka也是使用Scala编写的
二:环境安装
- 去官网下载安装包安装
- 配置环境变量SCALA_HOME
- 在终端中输入scala进入shell表示安装成功
- 在IDEA中安装Scala插件, 然后重启即可
Mac安装Scala参考:javascript:void(0) IDEA安装Scala插件参考:javascript:void(0)
三:创建Scala Maven工程
- 创建一个普通的maven工程File->Project->Maven(注意:maven工程是Java工程)
- 引入scala-library依赖,注意版本要和自己安装的scala版本保持一致
- 将src/main/java重命名成src/main/scala, src/test/java重命名成src/test/scala
- 配置scala对应的maven插件scala-maven-plugin
- 选中src/main/scala右键可以New->Package或者Scala Class,就可以开始学习Scala了语法了
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.example</groupId>
<artifactId>platform-scala</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.12.8</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/scala</sourceDirectory>
<testSourceDirectory>src/test/scala</testSourceDirectory>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>4.0.1</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-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass></mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>