SBT 構建scala eclipse開發

來源:互聯網
上載者:User

標籤:

scala eclipse sbt 應用程式開發

搭建Eclipse開發Scala應用程式的一般步驟

一、環境準備:

1、Scala : http://www.scala-lang.org/

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

3、Sbt: http://www.scala-sbt.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比較成熟。

Sbt本身的命令:參考 http://www.scala-sbt.org/0.13/tutorial/Running.html 和 http://www.scala-sbt.org/0.13/docs/Command-Line-Reference.html

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開發

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.