https://github.com/typesafehub/sbteclipse
https://github.com/sbt/sbt-assembly
一、sbt安装
1.首先装好jdk、scala环境。
2.到sbt官网下载安装包
http://www.scala-sbt.org/download.html
3.安装 sbt-1.0.2 .msi
我是装在D盘,按提示操作即可。
4.配置环境变量
配环境变量 SBT_HOME,值为sbt的安装目录,在环境变量path末尾添加“%SBT_HOME%\bin”。
5.配sbtconfig.txt参数
因为sbt会使用ivy作为库管理工具。ivy默认把library repository建在user home下面。Unix/Linux/Mac OS都还好说,如果就一个分区(或一个逻辑分区),无所谓发在哪个位置啦。如果操作系统是Windows,有分了C: D: E: 等若干分区,还是不要放在默认的%USERPROFILE%下面,C盘会随着开发的项目越来越多,大量的空间被开发库所占用。
windows系统,则只会使用“[SBT_HOME]\conf”下的sbtconfig.txt,不会用到sbtopts。以下以我的配置为例:
-Dfile.encoding=UTF8
-Dsbt.ivy.home=D:/sbt/ivy/
-Dsbt.boot.directory=D:/sbt/boot/
-Dsbt.global.base=D:/sbt/boot/
6.设置远程仓库
1)在sbtconfig.txt增加repositories文件路径
有的介绍是在用户目录下创建.sbt目录,放一个repositories文件,但是重做系统后C盘里的文件会被清理,建议修改到conf/下。
-Dsbt.repository.config=D:/sbt/conf/repositories
2)配置repositories文件
在conf/下创建repositories文件,添加如下内容
[repositories]
local
aliyun nexus:http://maven.aliyun.com/nexus/content/groups/public
到目前为止就已经配好sbt环境了,接下来就开始创建用sbt创建eclipse项目。
二、用sbt创建eclipse项目
1.创建工程项目目录
我这里创建了一个test目录作为本次测试工程。
为了方便管理项目,因此创建了一个根目录sbtProject,在此目录下再创建工程目录。
2.build.sbt
在test下手工创建build.sbt文件,增加以下内容
name := "test"
scalaVersion := "2.10.6"
version := "1.0"
scalacOptions += "-deprecation"
说明:name 创建的工程名称,scalaVersion本工程使用的scala版本,version 创建工程的版本。
3.plugins.sbt
在test下手工创建project目录,在project/下创建plugins.sbt文件,增加以下内容,配置sbteclipse
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "5.2.2")
说明:本次使用的sbt为1.0.2版本,所以配置sbteclipse的版本号为5.2.2,假如版本号对应错误,会导致安装sbteclipse失败,无法创建eclipse项目。
参考https://github.com/typesafehub/sbteclipse说明。
For sbt 0.13 and up
Add sbteclipse to your plugin definition file (or create one if doesn't exist). You can use either:
the global file (for version 0.13 and up) at ~/.sbt/0.13/plugins/plugins.sbt
the project-specific file at PROJECT_DIR/project/plugins.sbt
For the latest version:
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "5.2.2")
In sbt use the command eclipse to create Eclipse project files
> eclipse
In Eclipse use the Import Wizard to import Existing Projects into Workspace
4.开始创建eclipse项目
打开dos,进入到工程test下
1)执行sbt命令,开始下载依赖包,会有些慢。
[info] Loading settings from build.sbt ...
[info] Set current project to test (in build file:/E:/sbtProject/test/)
[info] sbt server started at 127.0.0.1:4141
2)基础依赖包下载完后,直接在sbt命令下执行eclipse(或者exit,执行sbt eclipse)下载eclipse依赖包,创建eclipse工程
[info] Set current project to test (in build file:/E:/sbtProject/test/)
[info] About to create Eclipse project files for your project(s).
[info] Successfully created Eclipse project files for project(s):
[info] test
E:\sbtProject\test>
构建好的eclipse项目目录如下
2017/10/17 18:09 358 .project
2017/10/17 17:16 <DIR> .settings
2017/10/17 18:07 <DIR> bin
2017/10/17 18:21 1,196 build.sbt
2017/10/17 17:13 <DIR> project
2017/10/17 17:45 <DIR> src
2017/10/17 17:16 <DIR> target
3)假如没有创建src目录,则手动创建以下目录,创建完后执行 sbt eclipse
src/main/scala
src/main/java
src/main/resources
src/test/scala
src/test/scala
src/test/resources
4)
若发现src目录下没有resources可以尝试手动创建,也可以在build.sbt中添加如下配置。
EclipseKeys.createSrc := EclipseCreateSrc.Default + EclipseCreateSrc.Resource
官网上查的是 https://github.com/typesafehub/sbteclipse/wiki/Using-sbteclipse
EclipseKeys.createSrc := EclipseCreateSrc.Default + EclipseCreateSrc.ManagedClasses
这两个参数均尝试,无效,可能是系统等其他原因有关。
5.eclipseIDE导入工程
Import–>Existing Projects into Workspace
6.添加依赖
我们如果想使用第三方的类,就需要添加依赖关系,和GAV坐标,这个再熟悉不过,我们需要编辑根目录下的build.sbt文件,添加依赖:
这里name,version,scalaVersion要注意每个间隔一行,其它的也是,不然会出错。
libraryDependencies是添加依赖的地方:我们添加2个。
libraryDependencies ++= Seq(
"org.json4s" %% "json4s-native" % "3.2.10",
"org.json4s" %% "json4s-jackson" % "3.2.10"
)
resolvers是仓库地址,这里配置了多个。
目前为止build.sbt文件内容如下:
name := "test"
scalaVersion := "2.10.6"
version := "1.0"
scalacOptions += "-deprecation"
EclipseKeys.createSrc := EclipseCreateSrc.Default + EclipseCreateSrc.Resource
EclipseKeys.createSrc := EclipseCreateSrc.Default + EclipseCreateSrc.ManagedClasses
EclipseKeys.withSource := true
libraryDependencies ++= Seq(
"org.json4s" %% "json4s-native" % "3.2.10",
"org.json4s" %% "json4s-jackson" % "3.2.10"
)
resolvers ++= Seq(
// HTTPS is unavailable for Maven Central
"Maven Repository" at "http://repo.maven.apache.org/maven2",
"Apache Repository" at "https://repository.apache.org/content/repositories/releases",
"JBoss Repository" at "https://repository.jboss.org/nexus/content/repositories/releases/",
"MQTT Repository" at "https://repo.eclipse.org/content/repositories/paho-releases/",
"Cloudera Repository" at "http://repository.cloudera.com/artifactory/cloudera-repos/",
// For Sonatype publishing
// "sonatype-snapshots" at "https://oss.sonatype.org/content/repositories/snapshots",
// "sonatype-staging" at "https://oss.sonatype.org/service/local/staging/deploy/maven2/",
// also check the local Maven repository ~/.m2
Resolver.mavenLocal
)
再次运行sbt eclipse,则依赖的jar包会自动加载到classpath
7.测试程序
package cn.com.wuy
import org.json4s._
import org.json4s.JsonDSL._
import org.json4s.jackson.JsonMethods._
object JsonSbtTest extends Application{
case class Winner(id: Long, numbers: List[Int])
case class Lotto(id: Long, winningNumbers: List[Int], winners: List[Winner], drawDate: Option[java.util.Date])
val winners = List(Winner(23, List(2, 45, 34, 23, 3, 5)), Winner(54, List(52, 3, 12, 11, 18, 22)))
val lotto = Lotto(5, List(2, 45, 34, 23, 7, 5, 3), winners, None)
val json =
("lotto" ->
("lotto-id" -> lotto.id) ~
("winning-numbers" -> lotto.winningNumbers) ~
("draw-date" -> lotto.drawDate.map(_.toString)) ~
("winners" ->
lotto.winners.map { w =>
(("winner-id" -> w.id) ~
("numbers" -> w.numbers))}))
println(compact(render(json)))
println(pretty(render(json)))
}
eclipse 运行结果:
{"lotto":{"lotto-id":5,"winning-numbers":[2,45,34,23,7,5,3],"winners":[{"winner-id":23,"numbers":[2,45,34,23,3,5]},{"winner-id":54,"numbers":[52,3,12,11,18,22]}]}}
{
"lotto" : {
"lotto-id" : 5,
"winning-numbers" : [ 2, 45, 34, 23, 7, 5, 3 ],
"winners" : [ {
"winner-id" : 23,
"numbers" : [ 2, 45, 34, 23, 3, 5 ]
}, {
"winner-id" : 54,
"numbers" : [ 52, 3, 12, 11, 18, 22 ]
} ]
}
}
三、Assembly打包
1)sbtassembly 安装
在project下面的plugins.sbt里面配置,至此plugins.sbt文件里内容如下:
resolvers += Resolver.url("artifactory", url("http://scalasbt.artifactoryonline.com/scalasbt/sbt-plugin-releases"))(Resolver.ivyStylePatterns)
resolvers += "Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/"
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "5.2.2")
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.5")
sbtassembly各版本参考:https://github.com/sbt/sbt-assembly
For sbt 0.13.6+ and sbt 1.0.0-M6, add sbt-assembly as a dependency in project/assembly.sbt:
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.5")
For older sbt 0.13.x, see sbt-assembly 0.11.2.
For sbt 0.12, see sbt-assembly 0.9.2.
(You may need to check this project's tags to see what the most recent release is.)
2)发布程序
在test/下运行sbt assembly命令进行发布
[info] Packaging E:\sbtProject\test\target\scala-2.10\test-assembly-1.0.jar ...
[info] Done packaging.
[success] Total time: 173 s, completed 2017-10-17 18:24:41
结束
参考链接:
http://blog.youkuaiyun.com/zhifeiyu2008/article/details/49781007
http://blog.youkuaiyun.com/qq_19648191/article/details/53725875
http://www.cnblogs.com/shijiaqi1066/p/5103735.html
http://www.scala-sbt.org/0.13/docs/zh-cn/Getting-Started.html
http://www.cnblogs.com/rxingyue/p/4398591.html
Sbt本身的命令:参考 http://www.scala-sbt.org/0.13/tutorial/Running.html 和 http://www.scala-sbt.org/0.13/docs/Command-Line-Reference.html