« 前一篇:无为
后一篇:Buzzwords from my student thesis »

用SBT创建Scala project @ 11/25/2011

Research
在网络上有非常多的观点关于建立Scala项目时用Maven还是sbt,如果你使用java很长时间一定很喜欢Maven而不是用Ant,但是如果你想创建Scala项目,那sbt一定是个不错的选择,因为他因为他就是为此而生,从而有很多功能和特点去支持这个编程语言.

现在先介绍一下如何安装sbt:

下载 sbt-launch.jar
然后拷贝这个文件到你系统的bin文件夹下,比如我:
zhichao@desktop:~$ sudo mv sbt-launch-0.7.7.jar /usr/local/bin/sbt-launcher.jar
创建脚本文件叫sbt,内容是这样的
echo “java -Xmx512M -jar /usr/local/bin/sbt-launcher.jar \”\$@\”" | sudo tee /usr/local/bin/sbt
执行赋予文件
sudo chmod +x /usr/local/bin/sbt
如果你正确按着上面说的去做能得到下面的显示:

zhichao@desktop:~$ sbt
Project does not exist, create new project? (y/N/s)


为了创建一个新项目,一个文件夹应该被创建向下面所示:

zhichao@desktop:~/Documents/HelloWorld mkdir HelloWorld
zhichao@desktop:~/Documents/HelloWorld$ cd HelloWorld/
zhichao@desktop:~/Documents/HelloWorld$ sbt
Project does not exist, create new project? (y/N/s)


按着提示输入项目的基本属性

zhichao@desktop:~/Documents/HelloWorld$ sbt
Project does not exist, create new project? (y/N/s) y
Name: HelloWorld
Organization: de.mobiStudio
Version [1.0]:
Scala version [2.9.0]:
sbt version [0.7.7]:
Getting net.java.dev.jna jna 3.2.3 ...
:: retrieving :: org.scala-tools.sbt#boot-app
    confs: [default]
    1 artifacts copied, 0 already retrieved (838kB/15ms)
Getting Scala 2.7.7 ...
:: retrieving :: org.scala-tools.sbt#boot-scala
    confs: [default]
    2 artifacts copied, 0 already retrieved (9911kB/25ms)
Getting org.scala-tools.sbt sbt_2.7.7 0.7.7 ...
:: retrieving :: org.scala-tools.sbt#boot-app
    confs: [default]
    17 artifacts copied, 0 already retrieved (4379kB/41ms)
[success] Successfully initialized directory structure.
Getting Scala 2.9.0 ...
:: retrieving :: org.scala-tools.sbt#boot-scala
    confs: [default]
    4 artifacts copied, 0 already retrieved (20442kB/45ms)
[info] Building project HelloWorld 1.0 against Scala 2.9.0
[info]    using sbt.DefaultProject with sbt 0.7.7 and Scala 2.7.7
>


至此项目已经被创建好了,你可以退出(ctrl+c)创建过程,因为我们需要至少一个类来完成编译.

> run
[info]
[info] == copy-resources ==
[info] == copy-resources ==
[info]
[info] == compile ==
[info]  Source analysis: 0 new/modified, 0 indirectly invalidated, 0 removed.
[info] Compiling main sources...
[info] Nothing to compile.
[info]  Post-analysis: 0 classes.
[info] == compile ==
[info]
[info] == run ==
[info] == run ==
[error] Error running run: No main class specified.
[info]
[info] Total time: 0 s, completed 19 May, 2011 3:39:02 PM
>


zhichao@desktop:~/Documents/HelloWorld$ cd src/main/scala/
zhichao@desktop:~/Documents/HelloWorld/src/main/scala$ gedit HelloWorld.scala


object HelloWorld {
  def main(args: Array[String]) {
    println("Hello, World!")
  }
}


接下来我们应该看到如下提示,

zhichao@desktop:~/Documents/HelloWorld$ sbt run
[info] Building project HelloWorld 1.0 against Scala 2.9.0
[info]    using sbt.DefaultProject with sbt 0.7.7 and Scala 2.7.7
[info]
[info] == compile ==
[info]  Source analysis: 1 new/modified, 0 indirectly invalidated, 0 removed.
[info] Compiling main sources...
[info] Compilation successful.
[info]  Post-analysis: 2 classes.
[info] == compile ==
[info]
[info] == copy-resources ==
[info] == copy-resources ==
[info]
[info] == run ==
[info] Running HelloWorld
<strong>Hello, World!</strong>!
[info] == run ==
[success] Successful.
[info]
[info] Total time: 6 s, completed 25 Nov, 2011 13:45:06 PM
[info]
[info] Total session time: 7 s, completed 25 Nov, 2011 13:45:06 PM
[success] Build completed successfully.
zhichao@desktop:~/Documents/HelloWorld$


至此对sbt的安装和创建已经完成,我们有两种选择,一个是继续用Editor编程或者选择IDE开发,我的选择是将项目引入到Eclipse当中, next….
发布于 11/25/2011 21:20:43 | 评论:1
邪风 @ 7/16/2012 10:01:09
相当不错的入门资料。

看帖要回帖...

生亦我所欲也,义亦我所欲也;二者不可得兼,舍生而取义者也。生亦我所欲,所欲有甚于生者,故不为苟得也;死亦我所恶,所恶有甚于死者,故患有所不辟也。如使人所欲莫甚于生,则几可以得生者,何不用也?使人之所恶莫甚于死者,则凡可以辟患者,何不为也?由是则生而有不用也,由是则可以辟患而有不为也。是故所欲有甚于生者,所恶有甚于死 者。非独贤者有是心也,人皆有之,贤者能勿丧耳

categories
archives
links
statistics
  • 网志数:49
  • 评论数:108