scala eclipse sbt 应用程序开发

搭建Eclipse开发Scala应用程序的一般步骤

一、环境准备:

2、Scala IDE for Eclipse :scala-ide.org

4、Sbt Eclipse : https://github.com/typesafehub/sbteclipse   typesafe的一个sbt for eclipse的助手,可以帮助生成eclipse

我的,Scala版本是2.10.3, Sbt版本是0.13

二、sbt生成scala eclipse项目:

我们想要在Eclipse里开发scala应用并符合sbt发布程序的文件结构(类似Maven结构),除了手工建立文件结构,还可以采用sbt eclipse的配置方法。

2.1、添加sbt eclipse插件

有2种配置方式:

一种是在 ~/.sbt/0.13/plugins//build.sbt 里配置addPlugin,这种做法是全局的插件,即对本机所有sbt项目均使用。

另一种是每个项目不一样的plugins,则是在每个项目跟目录下project/plugins.sbt里进行插件配置。

plugins.sbt里面内容配置,添加插件:

addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.4.0")

2.2、生成eclipse项目文件

然后进入到根目录sbt,成功进入sbt,运行eclipse命令生成eclipse的.classpath等eclipse相关文件:

可以看到和maven的目录结构是相似的:

src

├── main

│   ├── java

│   └── scala

└── test

├── java

└── scala

发现没有resouces目录:

在跟目录的build.sbt里添加:

EclipseKeys.createSrc := EclipseCreateSrc.Default + EclipseCreateSrc.Resource

再执行sbt eclipse

src/

├── main

│   ├── java

│   ├── resources

│   └── scala

└── test

├── java

├── resources

└── scala

2.3、导入Eclipse中

我们进入Eclipse,利用导入向导的import existing project into workspace这一项进行导入。

三、开发与部署

下面准备用一个实际例子演示在Scala里开发的一般步骤,最近用到scala里的json,就用json4s这个json lib来开发一个解析json的例子,json4s地址: https://github.com/json4s/json4s

3.1、添加依赖

我们如果想使用第三方的类,就需要添加依赖关系,和GAV坐标,这个再熟悉不过,我们需要编辑根目录下的build.sbt文件,添加依赖:

这里name,version,scalaVersion要注意每个间隔一行,其它的也是,不然会出错。

libraryDependencies是添加依赖的地方:我们添加2个。

resolvers是仓库地址,这里配置了多个。

name := "shengli_test_sbt"version := "1.0"scalaVersion := "2.10.3"EclipseKeys.createSrc := EclipseCreateSrc.Default +EclipseCreateSrc.Resource

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:

3.2、测试程序

一个简单的解析Json的程序,程序很简单,这里就不解释了。

packagecom.shengli.jsonimportorg.json4s._importorg.json4s.JsonDSL._importorg.json4s.jackson.JsonMethods._

object JsonSbtTestextendsApplication{case classWinner(id: Long, numbers: List[Int])case classLotto(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能运行Run as Scala Application,但是如何加依赖打包发布呢?

3.3、Assembly

还记得Spark里面的assembly吗?那个就是发布用的,sbt本身支持的clean compile package之类的命令,但是带依赖的one jar打包方式还是assembly比较成熟。

clean

Deletes all generated files (in the target directory).

compile

Compiles the main sources (in src/main/scala and src/main/java directories).

test

Compiles and runs all tests.

console

Starts the Scala interpreter with a classpath including the compiled sources and all dependencies. To return to sbt, type :quit , Ctrl+D (Unix), or Ctrl+Z (Windows).

run *

Runs the main class for the project in the same virtual machine as sbt.

package

Creates a jar file containing the files in src/main/resources and the classes compiled from src/main/scala and src/main/java .

help

Displays detailed help for the specified command. If no command is provided, displays brief descriptions of all commands.

reload

Reloads the build definition ( build.sbt , project/*.scala , project/*.sbt files). Needed if you change the build definition.

Assembly :

Assembly是作为一种插件的,所以要在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" % "2.4.0")

addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.11.2")

除了插件的配置之外,还需要配置跟目录下build.sbt,支持assembly,在文件头部加入:

importAssemblyKeys._

assemblySettings

至此build.sbt文件内容如下:importAssemblyKeys._

assemblySettings

name := "shengli_test_sbt"version := "1.0"scalaVersion := "2.10.3"EclipseKeys.createSrc := EclipseCreateSrc.Default +EclipseCreateSrc.Resource

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 assembly命令进行发布:

>assembly

[info] Updating {file:/home/victor/workspace/test_sbt/}test_sbt...

[info] Resolving org.fusesource.jansi#jansi;1.4...

[info] Done updating.

[info] Compiling1 Scala source to /home/victor/workspace/test_sbt/target/scala-2.10/classes...

[warn] there were1 deprecation warning(s); re-run with -deprecation fordetails

[warn] one warning found

[info] Including: scala-compiler-2.10.0.jar

[info] Including: scala-library-2.10.3.jar

[info] Including: json4s-native_2.10-3.2.10.jar

[info] Including: json4s-core_2.10-3.2.10.jar

[info] Including: json4s-ast_2.10-3.2.10.jar

[info] Including: paranamer-2.6.jar

[info] Including: scalap-2.10.0.jar

[info] Including: jackson-databind-2.3.1.jar

[info] Including: scala-reflect-2.10.0.jar

[info] Including: jackson-annotations-2.3.0.jar

[info] Including: json4s-jackson_2.10-3.2.10.jar

[info] Including: jackson-core-2.3.1.jar

[info] Checking every*.class/*.jar file's SHA-1.

[info] Merging files...

[warn] Merging 'META-INF/NOTICE' with strategy 'rename'

[warn] Merging 'META-INF/LICENSE' with strategy 'rename'

[warn] Merging 'META-INF/MANIFEST.MF' with strategy 'discard'

[warn] Merging 'rootdoc.txt' with strategy 'concat'

[warn] Strategy 'concat' was applied to a file

[warn] Strategy 'discard' was applied to a file

[warn] Strategy 'rename' was applied to 2 files

[info] SHA-1: d4e76d7b55548fb2a6819f2b94e37daea9421684

[info] Packaging /home/victor/workspace/test_sbt/target/scala-2.10/shengli_test_sbt-assembly-1.0.jar ...

[info] Done packaging.

[success] Total time: 39 s, completed Aug 4, 2014 1:26:11 AM

四、总结

本文介绍了在Eclipse里利用Sbt构建开发Scala程序的一般步骤,并用实例讲解了整个流程。

用sbt eclipse插件生成sbt文件目录结构,sbt eclipse命令来生成更新jar包依赖。

用assebly插件对scala应用进行打包发布。

java eclipse sbt_SBT 构建scala eclipse开发相关推荐

  1. Apache Spark学习:利用Eclipse构建Spark集成开发环境

    介绍了如何使用Maven编译生成可直接运行在Hadoop 2.2.0上的Spark jar包,而本文则在此基础上, 介绍如何利用Eclipse构建Spark集成开发环境 . 不建议大家使用eclips ...

  2. 在Eclipse里搭建Scala开发环境

    在Eclipse里搭建Scala开发环境 Scala:一种类似Java的编程语言,综合了面向对象编程(Object-Oriented Programming)和函数式编程(Functional  Pr ...

  3. Java SE 9:使用Eclipse和IntelliJ IDEA IDE开发和测试HelloWorld模块(第4部分)

    I have already discuss about "Java Module System" Basics in my previous posts. I'm going t ...

  4. Eclipse SDK构建J2EE开发环境

    嫌弃官方Java EE Developers 笨重的兄弟能够自己搞个J2EE开发环境! 1.首先去Eclipse官网下载Eclipse IDE 我使用的是:Eclipse IDE for Java D ...

  5. java 按钮怎么透视_【手把手教你Eclipse插件开发】之Eclipse透视图开发

    Eclipse有很多的透视图,比如Debug,或者java.下面分别是Debug,和java的透视图,可以发现,他们的结构不一样,展示给使用者的画面也不相同. 下面步入正题,来开发我们自己的透视图. ...

  6. java lib 不在构建路径里面问题_svn - Eclipse“这个编译单元不在java项目的构建路径上”...

    svn - Eclipse"这个编译单元不在java项目的构建路径上" 我无法在Eclipse上使用自动完成功能. 我正在研究svn上的项目. 我通过进入Eclipse在Eclip ...

  7. 分布式计算Hadoop系列之如何Eclipse中构建Hadoop项目

    前言 之前根据Hadoop官方文档对HDFS.MapReduce的架构.配置管理等进行了学习,但某些地方官方文档讲解的比较模糊.做过开发的人都能够体会,官方文档有些类似业务规则或者要求,而真正的细节还 ...

  8. [_CN] Eclipse精要与高级开发技术 note

    [_CN] Eclipse精要与高级开发技术 note 一 eclipse是基于java的 ide ,但根据其体系结构,开发插件,也可拓展到其他语言-------- 尽管如此,但还是很少听说用ecli ...

  9. 问题合集 ------- 用 Eclipse 平台进行 C/C++ 开发

    简介: 我们将概述如何在 C/C++ 开发项目中使用 Eclipse 平台.尽管 Eclipse 主要是一个 Java 开发环境,但其体系结构确保了对其它编程语言的支持.在本文中,您将学习如何使用 C ...

最新文章

  1. 强化学习AI:它菜了,我慌了
  2. 'ScriptModel' object has no attribute 'save'
  3. Facebook开源算法代码库,轻松复现前沿视频理解模型
  4. 生成树的计数 Matrix-Tree(矩阵树)定理
  5. FFmpeg代码导读——基础篇
  6. 《2017 云计算评测报告》:带你了解 AWS、阿里云、腾讯云等八家云计算服务提供商的综合用户体验情况...
  7. 纯js分页代码(简洁实用)
  8. 前端面试有这几篇就够了--HTML篇
  9. 把.Net开发环境迁移到Linux上去
  10. JavaCV 第一个JavaCV程序
  11. html古诗竖行排列,古诗词竖版图片
  12. linux WPA_supplicant
  13. gatk过滤_GATK使用方法详解(变异检测)
  14. c语言中个各标点符号作用,C语言运算符和标点符号.xls
  15. 我和《独角兽项目》背后的故事
  16. 如何在html页面上画一条渐变线
  17. oracle查询三个月前的时间
  18. 抽象类 枚举 反射 接口
  19. 当双绞线遇上光纤布线-NETLINK多模光纤收发器HTB-1100
  20. revit学习-视图

热门文章

  1. 人在旅途——》张家界5天出行准备清单
  2. AES-256-CBC 加密解密
  3. 【 OJ 】 HDOJ1035 迷宫类问题模拟走向 [ 32 ]
  4. 介绍一款VideoPad 6.01汉化版免费的电影制作视频编辑器
  5. 14、保存文件格式为.txt
  6. Wayland utilizing Android GPU drivers on glibc based systems, Part 1
  7. 《高效能人士的七个习惯》丨一本被书名耽误的必读好书
  8. Java——计算用户输入的日期离1900年1月1日相距多少天
  9. 19001月1日是星期一c语言,已知道1900年1月1日是星期一,请输入一具体的日期,如何判断此日期是星期几?请利用C语言来进行编程...
  10. 【已解决】ERROR: ENOENT: no such file or directory,open