Understand and install sbt, use sbt (console, IDEA) and the jar package of spark packaged in IDEA

The test environment of this article: 
● Win10 
● Scala-2.12.3 
● Spark-2.2.0 
● Sbt-1.0.0 
● JDK-1.8 
● IDEA-2017.1.4 
● Git (it may be used when IDEA downloads the SBT plugin) 
● 2017/ 8/26 (I forgot to post it on the blog after I finished writing the article)

  • I took a lot of detours in the process of configuring IDEA+scala&sbt, so I recorded the configuration process, hoping to help newbies.
  • There are currently two main maintenance versions: 0.13.X and 1.X. It should be noted that the SBT plug-in on IDEA is temporarily updated with the 0.13.X series, and my local is 1.0.0. I wonder if there will be inexplicable problems in writing in the future.
  • It is recommended to read more before installation: official documentation ; pdf503 pages
  • Geek Academy's 0.13.15: Chinese translation version ; pdf is only 62 pages 
    (Note: I am new to sbt, please correct me if there is any mistake in the text, let's learn together)

content:

1. Introduction to sbt & why use sbt

1. Introduction to sbt & why to use sbt: 
● Introduction: SBT is a modern build tool. It is written in Scala, and also provides a lot of convenient and fast functions for Scala. It is also a general-purpose build tool. 
● Why: built on scala, can use scala code fully and flexibly 
For simple projects, need little or no configuration 
of continuous compilation (continuous execution of commands), packaging and deployment of jar 
generated files and scaladoc 
to Scala/java hybrid Project Support 
Supports testing with ScalaCheck, specs, and ScalaTest 
Running the scala REPL (scala compiler) using the project's class files and dependencies is 
powerful Dependency management function 
Modular support of sub-projects Support of 
external projects (need to configure a Git repository as a dependency!) 
Parallel execution of tasks, including parallel test execution 
support library (library) management: internal declarations, external Ivy or Maven configuration files, Or manually manage 
(Note: It is recommended to read the official getting started guide) (there are Chinese on the page, but please note that only a part of the Chinese is translated) Address: http://www.scala-sbt.org/1.x/docs/ index.html

Second, sbt installation

二、sbt安装(Windows) 
1. 在sbt官网http://www.scala-sbt.org/download.html,目前最新版为1.0.0(2017/8/26)。我下载的压缩包,添加SBT_HOME。将解压目录的bin目录加入path环境 
2.先别在命令行执行sbt console启动sbt,相关jar包默认会下载到C:\Users\Administrator.sbt(重装系统就没了)。 
关于conf下的 sbtconfig 和 sbtopts,我找到网上的3种可能正确的说法(或者都没什么错),看了sbt.bat 的shell脚本也没有拿定主意。希望知道的朋友可以解答以下: 
1. 若是windows系统,则只会使用conf下的sbtconfig.txt,不会用到sbtopts。 
2. conf/sbtconfig.txt: sbt的配置文件:在这里我们可以配置代理。 
conf/sbtopts:也是sbt的配置文件,主要包含配置设置ivy和boot的地址 
3. stackoverflow上的:

我认为:sbtconfig.txt只有Windows用户可用,而Windows用户可以将代理信息以及ivy和boot的地址,JVM参数等配置都写在sbtconfig.txt。以下是我的配置:(注:最开始我设置了-Dfile.encoding=UTF-8,反而中文乱码,建议大家不写;MaxPermSize在JDK1.8之后已经不再使用。) 
.# Set the java args to high

-Xms128m

-Xmx1024M

.# -XX:PermSize=128m

.# -XX:MaxPermSize=256m

-XX:ReservedCodeCacheSize=128m

.# Set the extra SBT options

-Dsbt.log.format=true

.# mydiv

-Dsbt.repository.config=C:\Users\Administrator.sbt\repo.repositories

-Dsbt.boot.directory=D:\sbt\mysbt\boot

-Dsbt.ivy.home=D:\sbt\mysbt\ivy

-Dsbt.repository.secure=false

注: 
● 前几项是Java虚拟机的相关配置; 
● -Dsbt.repository.secure=false 是否使用https协议获取依赖信息,感觉国内还是不开吧,体验更好。。。 
● -Dsbt.ivy.home:配置vy的文件目录,ivy是sbt的默认管理项目依赖工具(下载的依赖都会保存在这里配置的目录下,和Maven的本地仓库一个道理) 
● -Dsbt.repository.config : C:\Users\Administrator.sbt下创建repositories文件:(注:因为使用的markdown编辑的文章,暂不知如何将‘#’转义,故在每个‘#’注释符前加了‘.’) 
[repositories] 
[repositories] 
local 
nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public 
.#typesafe-ivy-releases: http://repo.typesafe.com/typesafe/ivy-releases/, [organization]/[module]/[revision]/[type]s/artifact.[ext] 
.#typesafe2: http://repo.typesafe.com/typesafe/releases/ 
.#sbt-plugin: http://repo.scala-sbt.org/scalasbt/sbt-plugin-releases/ 
.#sonatype: http://oss.sonatype.org/content/repositories/snapshots 
.#uk_maven: http://uk.maven.org/maven2/ 
.#repo2: http://repo2.maven.org/maven2/

成功后的各个目录如下: 
● .sbt目录 
这里写图片描述 
● sbt执行成功 
这里写图片描述 
● boot文件夹 
这里写图片描述 
● ivy2文件夹 
这里写图片描述 
● sbt console

三、console中使用sbt

1.常用的的 sbt 命令

这里写图片描述
sbtVersion:查看当前sbt版本 
actions : 显示对当前工程可用的命令 
publish-local : 把构建出来的jar包安装到本地的ivy缓存 
publish : 把jar包发布到远程仓库(如果在git中配置了的话) 
show name :查看当前项目的名字。 
show libraryDependencies :查看当前项目依赖的库。

2.最好在预想的项目根目录下面执行的sbt命令!

最简单的测试代码: 
● 随便建立一个文件夹,作为你项目的根目录,再建立一个scala文件:我的scala文件内容如下: 
● cmd进入项目根目录,sbt—》run(注:会警告说没有设置sbt.version 因为我们还没有在工程目录hello下创建project\build.properties并设置sbt.version。) 
这里写图片描述

3.完整的目录结构: 
(在sbt0.7.x, 我们只要创建项目目录,然后在项目目录下敲入sbt,sbt会自动为我们生成需要的目录和文件, 而sbt 0.10之后,如果不适用IDE,我们需要自己创建生相应的目录和文件) 
这里写图片描述 
(注:手动创建目录后,再次编译运行scala文件,在project文件夹下还会生成一个target文件夹,与工程目录hello下的target文件夹几乎一样。感觉这与sbt的迭代式构建是有关系的) 
官网截图: 
这里写图片描述 
解释:sbt 将会自动找到以下内容: 
● 项目根目录下的源文件 
● src/main/scala 或 src/main/java 中的源文件 
● src/test/scala 或 src/test/java 中的测试文件 
● src/main/resources 或 src/test/resources 中的数据文件 
● lib 中的 jar 文件 
4. SBT支持两种使用方式: 
● 交互模式 
执行 sbt 不跟任何命令行参数将会进入交互模式。交互模式有一个命令行(含有 tab 自动补全功能和历史记录)。 
例如,在 sbt 命令行里输入 compile: 
compile

再次 compile,只需要按向上的方向键,然后回车。 输入 run 来启动程序。 输入 exit (建议)或者 Ctrl+D (Unix-like)或者 Ctrl+Z (Windows)可以退出交互模式。

(注:管理员权限sbt console进入scala命令行,可以使用“;”进行多行输入;也可以使用“:paste”命令进入粘贴模式,键入”Ctrl—D“解释执行代码): 
这里写图片描述 
● 批处理模式 
你也可以用批处理模式来运行 sbt,可以以空格为分隔符指定参数。对于接受参数的 sbt 命令,将命令和参数用引号引起来一起传给 sbt。

四、IDEA中使用sbt,并打包jar

详见我的另一篇文章:见我的这篇文章

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324645908&siteId=291194637