开发环境搭建
- 安装JDK
- 安装Scala, scala下载页面 下载版本2.12.3
- 安装Intellij idea插件, Intellij idea Scala插件下载
环境搭建
1、Scala SDK安装与Idea插件安装,注意Idea插件与版本要匹配,前提是要装好JDK,参考百度上的帖子很容易
在Intellij idea中用Maven+plugin搭建Scala项目
- Idea中新建项目scala-study
- Maven构建,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.shannonAJ</groupId>
<artifactId>scala-study</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.12.3</version>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-compiler</artifactId>
<version>2.12.3</version>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-reflect</artifactId>
<version>2.12.3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<version>2.15.1</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
- main下添加Java package和 scala package
- HelloScala.Scala
package com.shannonAJ.scala
/**
* Created by ShannonAJ on 2017/8/13.
*/
class HelloScala {
def sayHello(x:String): Unit = {
print("Hello " + x);
}
}
5.HelloDemo.java
package com.shannonAJ.task;
import com.shannonAJ.scala.HelloScala;
/**
* Created by ShannonAJ on 2017/8/13.
*/
public class HelloDemo {
public static void main(String[] args) {
System.out.println("use scala class");
HelloScala hello = new HelloScala();
hello.sayHello("scala");
}
}
- 编译
mvn clean scala:compile compile
- 运行结果
use scala class
Hello scala
Process finished with exit code 0