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

5、Sbt Assembly : https://github.com/sbt/sbt-assembly 发布应用程序的一个sbt插件。

我的,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的程序,程序很简单,这里就不解释了。

package com.shengli.json
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能运行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 <argument>* 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 <command> 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,在文件头部加入:

import AssemblyKeys._
assemblySettings 至此build.sbt文件内容如下: import AssemblyKeys._
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] Compiling 1 Scala source to /home/victor/workspace/test_sbt/target/scala-2.10/classes...
[warn] there were 1 deprecation warning(s); re-run with -deprecation for details
[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应用进行打包发布。

SBT 构建scala eclipse开发的更多相关文章

  1. 使用SBT构建Scala应用(转自git)

    # 使用SBT构建Scala应用 ## SBT简介 SBT是Simple Build Tool的简称,如果读者使用过Maven,那么可以简单将SBT看做是Scala世界的Maven,虽然二者各有优劣, ...

  2. 【原】SBT构建Scala应用

    [转帖] 原文地址:https://github.com/CSUG/real_world_scala/blob/master/02_sbt.markdown 尊重版权,尊重他人劳动成果,转帖请注明原文 ...

  3. 使用SBT构建Scala应用【转载】

    使用SBT构建Scala应用 SBT简介 SBT是Simple Build Tool的简称,如果读者使用过Maven,那么可以简单将SBT看做是Scala世界的Maven,虽然二者各有优劣,但完成的工 ...

  4. 使用SBT构建Scala项目

    既然决定要在Scala上下功夫,那就要下的彻底.我们入乡随俗,学一下SBT.sbt使用ivy作为库管理工具.ivy默认把library repository建在user home下面. 安装SBT 在 ...

  5. idea中使用sbt构建scala项目及依赖

    1.安装scala插件 http://www.cnblogs.com/yrqiang/p/5310700.html 2. 详细了解sbt: http://www.scala-sbt.org/0.13/ ...

  6. sbt介绍与构建Scala项目

    一.sbt简介 sbt是类似ANT.MAVEN的构建工具,全称为Simple build tool,是Scala事实上的标准构建工具. 主要特性: 原生支持编译Scala代码和与诸多Scala测试框架 ...

  7. Scala 基础(二):sbt介绍与构建Scala项目

    一.sbt简介 sbt是类似ANT.MAVEN的构建工具,全称为Simple build tool,是Scala事实上的标准构建工具. 主要特性: 原生支持编译Scala代码和与诸多Scala测试框架 ...

  8. 使用sbt构建spark 程序

    今日在学习scala和spark相关的知识.之前在eclipse下编写了wordcount程序.但是关于导出jar包这块还是很困惑.于是学习sbt构建scala. 关于sbt的介绍网上有很多的资料,这 ...

  9. Eclipse中构建scala开发环境的步骤

    Eclipse是一款非常使用的开发工具,熟悉它的童鞋应该都知道,它不仅是最常用的android开发工具,还是最常用的Java开发工具.既然eclipse如此重要,本文小编就和大家一起来扒一扒在ecli ...

随机推荐

  1. JQUERY添加、删除元素、eq()方法;

    一.jQuery - 添加元素 1.append() - 在被选元素内部的结尾插入指定内容 2.prepend() - 在被选元素内部的开头插入指定内容 3.after() - 在被选元素之后插入内容 ...

  2. hdu1421 搬寝室(dp)

    此题是动态规划题. 解题思路: 用w[i]存储n个物品的重量,对其进行排序. 那么当取了第i个物品,必然会取第i-1个物品. 令dp[i][j]表示前i个物品,取j对的最小疲劳度. 若取第i个物品 则 ...

  3. ps通道磨皮

    1.Ctrl+J 复制一个新图层2.进入通道面板,复制一个噪点最多的通道3.滤镜--其他--高反差保留 (我一般设数值13)4.图像--计算 (混合模式选择强光) 计算3次,得到Alpha3 5.按住 ...

  4. c语言多线程队列读写

    最近用c语言写了个简单的队列服务,记录一下,文件结构为 main.c queue.c queue.h,代码如下: 主函数 #define NUM_THREADS 200 #include <st ...

  5. Alpha版本——Postmortem会议

    No Bug 031402401鲍亮 031402402曹鑫杰 031402403常松 031402412林淋 031402418汪培侨 031402426许秋鑫 设想和目标 1.我们的软件要解决什么 ...

  6. .Net , 请取服务器上的文件

    public class IdentityScope : IDisposable { /// <summary> /// 登录一个新用户 /// </summary> /// ...

  7. int和NSInteger区别

    NSInteger会自动根据操作系统的位数(32或者64位)返回最大的类型 查到c语言中,int和long的字节数是和操作系统指针所占位数相等. 但c语言中说,long的长度永远大于或等于int ob ...

  8. Electron实战:创建ELectron开发的window应用安装包

    前言:研究electron自动更新的时候,在electron的官方文档auto-updater 中,提到了在几个平台mac,Linux,windows下electron 的自动更新方法,其中winds ...

  9. Android Gradle 技巧之二: 最爱命令行

    命令行 很多做 Android 开发不久的同学,习惯于使用图形界面,对命令行操作很陌生甚至恐惧.遇到 AS 运行错误,束手无策.AS 为了确保易用性,也在 UI 界面上屏蔽了很多命令行运行的细节,导致 ...

  10. springboot + swagger

    swagger用于定义API文档. 好处: 前后端分离开发 API文档非常明确 测试的时候不需要再使用URL输入浏览器的方式来访问Controller 传统的输入URL的测试方式对于post请求的传参 ...