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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值