Scala sbtのプラグインを使う

sbtのプラグインを使ってみる

sbtには、sbtの機能を拡張するためのプラグインのしくみがある。ためしにつかってみる。

sbtのプロジェクト作成とかはこちら
Scala sbtのプロジェクト - kaishitaeiichiの日記

プラグイン

  • sbteclipse
  • Play framework sbt-plugin

sbteclipse

sbteclipseは、sbtとEclipseを連携させるためのplugin。Eclipseのpluginでなくて、sbtのplugin

Home · sbt/sbteclipse Wiki · GitHub

ここでは、

helloworld

がプロジェクトのルートディレクト

まず、以下の設定を追加する
helloworld\project\plugins.sbt

addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.1.0")
cd helloworld
sbt

sbtのコンソールを起動したら

> eclipse
[info] About to create Eclipse project files for your project(s).
[info] Successfully created Eclipse project files for project(s):
[info] helloworld
>

そうすると
helloworld\.project
helloworld\.classpath
というEclipseのプロジェクトの設定ファイルができている。

EclipseScala IDE for Eclipse(これは、Ecilpseのプラグイン)がインストールされていれば、Scalaプロジェクトとして、利用できる。

Play framework sbt-plugin

playは、Scalaでは、けっこう注目されているWebアプリのフレームワーク。2012春ごろPlay framework 2.0がリリースされたときに自分もためした。まだまだ発展中のようだ。今はどうなんだろう。

Home - 2.0.4

New Application - 2.0.4

mkdir sbt-projects 
cd sbt-projects
mkdir myFirstApp

mkdir myFirstApp\project


myFirstApp\project\plugins.sbt

// The Typesafe repository 
resolvers += "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"

// Use the Play sbt plugin for Play projects
// http://repo.typesafe.com/typesafe/releases/play/sbt-link/
// の中にあったバージョンを指定した
addSbtPlugin("play" % "sbt-plugin" % "2.1-10112012")

myFirstApp\project\Build.scala

import sbt._
import Keys._
import PlayProject._
 

object BuildSettings {
  val buildScalaVersion = "2.9.2"
  val buildSbtVersion = "0.12"

  val buildSettings = Defaults.defaultSettings ++ Seq (
    scalaVersion := buildScalaVersion,
    sbtVersion := buildSbtVersion
  )

}

object ApplicationBuild extends Build {
 
  val appName         = "My first application"
  val appVersion      = "1.0"
 
  val appDependencies = Nil
 
  //ここは、単なるsbtだったら、単なるProjectクラスのコンストラクタとなる。
  //Playは、独自のPlayProjectを実装しているようで、PlayProjectクラスのコンストラクタとなっている。
  val main = PlayProject(
    appName, appVersion, appDependencies, mainLang = SCALA, settings = BuildSettings.buildSettings
  ) 
 
}
cd myFirstApp

sbt起動

C:\Users\user\Desktop\sbt-projects\myFirstApp>sbt
[info] Loading project definition from C:\Users\user\Desktop\sbt-projects\myFirstApp\project
[info] Compiling 1 Scala source to C:\Users\user\Desktop\sbt-projects\myFirstApp\project\target\scala-2.9.2\sbt-0.12\classes...


  [info] Resolving org.scala-sbt#precompiled-2_8_2;0.12.1 ...
  [info] Resolving org.scala-sbt#precompiled-2_10_0-m7;0.12.1 ...
[info] Done updating.
[info] Compiling 1 Scala source to C:\Users\user\Desktop\sbt-projects\myFirstApp2\project\target\scala-2.9.2\sbt-0.12\classes...
[error] java.lang.ExceptionInInitializerError
[error] Use 'last' for the full log.
Project loading failed: (r)etry, (q)uit, (l)ast, or (i)gnore?

エラーが出た!なんでじゃあ

ここで、Use 'last'とあるので、
Project loading failed: (r)etry, (q)uit, (l)ast, or (i)gnore?
の選択肢の中から、lastと入力してenter

        at SbtJansiLaunch.main(SbtJansiLaunch.java:4)
Caused by: java.lang.RuntimeException: Invalid project ID: Expected ID character
My first application
  ^

うん?名前が悪いのか?Build.scalaのappName、MyFirstAppに修正

Project loading failed: (r)etry, (q)uit, (l)ast, or (i)gnore?
の選択肢の中から、retryと入力してenter

[info] Loading project definition from F:\Data\Scala\sbt-projects\myFirstApp\project
[info] Compiling 1 Scala source to F:\Data\Scala\sbt-projects\myFirstApp\project\target\scala-2.9.2\sbt-0.12\classes...
[info] Set current project to myFirstApp (in build file:/F:/Data/Scala/sbt-projects/myFirstApp/)
[myFirstApp] $

ようやく、Playのプロジェクトのsbtを起動できた。

[myFirstApp] $run

....省略....ダウンロード長すぎ

[info]  [SUCCESSFUL ] org.easytesting#fest-util;1.1.6!fest-util.jar (1316ms)
[info] Done updating.
--- (Running the application from SBT, auto-reloading is enabled) ---

[info] play - Listening for HTTP on /0:0:0:0:0:0:0:0:9000

(Server started, use Ctrl+D to stop and go back to the console...)

playのWebサーバ起動。Webサーバ停止は、Ctrl+Cでなくて、Ctrl+D

開発できるようになったのかなとおもったら、myFirstApp配下には、playのプロジェクトの標準ディレクトリ構成になってない。ディレクトリとかデフォルトの設定ファイルを生成するタスクはないんだろうか。手作業で作るのはめんどうだ。これなら、sbtにplayをインストールするより、play(sbt込み)をインストールしたほうが良いような。。。。