セカイノカタチ

世界のカタチを探求するブログ。関数型言語に興味があり、HaskellやScalaを勉強中。最近はカメラの話題も多め

3分でsbtとEclipseに対応したScalaプロジェクトを作る

この記事は、Play or Scala Advent Calendar 2012 11日目の記事です。
「急にScalaのprojectツリーを作らなきゃいけない!」ってこと、よくありますよね?
僕は、そんな時このやりかたで、ささっとprojectを作っています。
この間、ジャック・バウアーに拳銃で脅されて、「国家の安全がかかってる!3分以内にScalaのプロジェクトを立ち上げろ!!」と言われた時も、何とか命拾いしました。

前提条件

必要なものは、事前に入れておく(このへんは3分クッキング的)。

java 必要
Eclipse ちなみに現時点での最新はこれ
Scala IDE Eclipse上でScalaプロジェクトを開発出来るようになる
sbt ライブラリ管理とかビルドとか実行とか
conscript g8が使う。git上のスクリプトを実効してくれる
giter8 テンプレート展開してくれる

Scala本体は不要です(たぶん)。sbtがいい感じにやってくれる。javaは必要かも。

やってみる

ようは、giter8で、テンプレートを展開して、sbtのeclipse pluginを入れれば簡単よ。って話しです。

giter8のwikiにテンプレートが、一覧になっているので、ここから良さそうなのをチョイスします。

↓よさそうなの

  • gseitz/android-sbt-project sbt project for Android
  • softprops/unfiltered sbt project for Unfiltered with Jetty
  • chrislewis/basic-project bootstrapped sbt project with simple configuration
  • scalatra/scalatra-sbt Basic Scalatra template using SBT 0.10.x.
  • mads379/lift-blank.g8 Liftweb minimalistic project
  • typesafehub/scala-sbt Plain scala project template
  • typesafehub/play-scala Play project template for Scala users

ここでは、chrislewis/basic-projectを使います。

$> g8 chrislewis/basic-project.g8
version [0.1.0-SNAPSHOT]: 
organization [com.example]: com.qtamaki
name [Basic Project]: Sample Project

Applied chrislewis/basic-project.g8 in sample-project

sample-projectディレクトリが出来ました。
ちなみにScalaのプロジェクトを作るだけなら、これで終わりです。1分でしたね。:p

$> cd sample-project/

sbtのeclipse pluginを入れる設定をします。

$> cat >> project/plugins.sbt
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.1.0")
^D

ここで、gitにプロジェクトを登録しておきます。

$> git init
Initialized empty Git repository in /home/tamaki-t/dev/scala/leftist-heaps/.git/

$> cat > .gitignore
target
project/target
project/project/target
.classpath
.project
.cache
^D

$>  git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	.gitignore
#	build.sbt
#	project/
#	src/
nothing added to commit but untracked files present (use "git add" to track)

$> git add -A
$> git commit -m "init projects"

sbtで、eclipseのプロジェクトファイルを生成します(gitにはcommitしない)。

$> sbt
sbt> eclipse
^D

こっから、Eclipse
[File]->[Import]を選択。ダイアログを出す。

[Existing Projects into Workspace]を選択。

先ほど、プロジェクトを生成したディレクトリを選択する。と、[Projects]ペインにプロジェクトが表示される。

Import完了。ディレクトリ構造もいい感じで、sbtに追加したjarも設定されている(ポイント)。

テストを走らせるときは、sbt上でtestがいいかも。

> test
[info] Compiling 1 Scala source to /home/tamaki-t/dev/scala/sample-project/target/scala-2.9.2/classes...
[info] Compiling 1 Scala source to /home/tamaki-t/dev/scala/sample-project/target/scala-2.9.2/test-classes...
[info] AppSpec
[info] 
[info] The 'Hello world' string should
[info] + contain 11 characters
[info] + start with 'Hello'
[info] + end with 'world'
[info]  
[info]  
[info] Total for specification AppSpec
[info] Finished in 409 ms
[info] 3 examples, 0 failure, 0 error
[info] 
[info] Passed: : Total 3, Failed 0, Errors 0, Passed 3, Skipped 0
[success] Total time: 15 s, completed 2012/11/30 12:31:57

sbt上で実行も出来る。

> run
[info] Running com.qtamaki.sampleproject.App 
Hello com.qtamaki.Sample Project![success] Total time: 0 s, completed 2012/11/30 12:32:13

ここまで3分足らず(多分)。いかかでしたでしょうか?

おまけ

GitHubにpushする。

git remote add origin https://github.com/qtamaki/advent_project.git
git push -u origin master

普通にpushするだけ。簡単。