sbt學習

sbt simple build tool

sbt是一個現代構建工具。


安裝方法:

ubuntu and debian-based distributions

echo "deb https://dl.bintray.com/sbt/debian /" | sudo tee -a /etc/apt/sources.list.d/sbt.list
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 642AC823
sudo apt-get update
sudo apt-get install sbt


red hat and other rpm-based distributions

curl https://bintray.com/sbt/rpm/rpm | sudo tee /etc/yum.repos.d/bintray-sbt-rpm.repo
sudo yum install sbt

How to use sbt to build a jar package of Scala project?

sbt package
will produces the main artifact as a jar into target/scala-2.x.y/project_name_2.x.y-zz.jar.

Standalone jar with all dependencies

What it will do is create one jar file containing the project class files along with all of its dependencies. If you write an application, what you get is a double-clickable jar that you can execute from anywhere.

If you want to build a standalone executable jar with dependencies, you may use the sbt-assembly plugin. And then build it by

sbt assembly

The standalone will be in target/project_name-assembly-x.y.jar.
You can run it by
java -jar project_name-assembly-x.y.jar [class.with.main.function]

Making a project usable as a dependency or another project

For example, If you want to use your project A as a dependency for another project B. You could just package the class files of A, using sbt package. Then you could drop the resulting .jar file in the lib directory of project B. However, if A also requires libraries, you must make sure that they also end up in project B's libraries.

A better approach is to publish your project by command sbt pulish-local, which is detail in [3].

常用命令

  • actions – 顯示對當前工程可用的命令
  • update – 下載依賴
  • compile – 編譯代碼
  • test – 運行測試代碼
  • package – 創建一個可發佈的jar包
  • publish-local – 把構建出來的jar包安裝到本地的ivy緩存
  • publish – 把jar包發佈到遠程倉庫(如果配置了的話)

更多命令

  • test-failed – 運行失敗的spec
  • test-quick – 運行所有失敗的以及/或者是由依賴更新的spec
  • clean-cache – 清除所有的sbt緩存。類似於sbt的clean命令
  • clean-lib – 刪除lib_managed下的所有內容

筆者目前仍然處於對sbt的毫無頭緒的狀態,目前學習是爲了打包Scala程序跑跑Spark程序,先暫時解決需求。



references:

[1]http://ask.systutorials.com/861/how-to-package-a-scala-project-to-a-jar-with-sbt

[2]http://www.importnew.com/4311.html

[3]http://stackoverflow.com/questions/20938177/create-standalone-jar-using-sbt

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章