2011年7月28日木曜日

sbteclipseを使ってxsbt(sbt 0.10系)からeclipse用の設定ファイルを作成する方法

Scalaの作者であるMartin Oderskyさんがinetrviewにて
I personally switched to the Eclipse plugin after 20 years of emacs. Certainly before then, I tried the plugin; but for the complex projects I do, it didn't live up to what I needed. Now, I would now never go back to emacs.
『eclipseのScalaプラグインの出来が素晴らしくて、20年使ってきたけどemacsにはもう戻れません!』
という発言をされていたのでeclipseのScalaプラグイン『Scala IDE for Eclipse』が気になってきまして。

でもscalaにはsbtという素敵なビルドツールがあるし、
  • eclipseはエディタとして使う
  • sbtはビルド用に使う
ということができないかなーと思って、sbtとeclipseの連携方法を調査したのでメモ。

参考リンク。

→sbtecipseを使って、sbtから.projectファイルや.classpathファイルを生成するとのこと。

http://stackoverflow.com/questions/6566470/how-to-add-sbteclipse-plugin-to-sbt-0-10-x
→バージョンの指定方法を参考にしました。


やり方。

  1. build.sbtファイルをproject/plugins配下に作成(デフォルトで存在?)。
    ※sbtを実行したルートディレクトリに作成しても良い模様。
  2.  下記内容を記載。
  3. resolvers += {
      val typesafeRepoUrl = new java.net.URL("http://repo.typesafe.com/typesafe/releases")
      val pattern = Patterns(false, "[organisation]/[module]/[sbtversion]/[revision]/[type]s/[module](-[classifier])-[revision].[ext]")
      Resolver.url("Typesafe Repository", typesafeRepoUrl)(pattern)
    }
    
    libraryDependencies <<= (libraryDependencies, sbtVersion) { (deps, version) => 
      deps :+ ("com.typesafe.sbteclipse" %% "sbteclipse" % "1.2" extra("sbtversion" -> version))
    }
    
  4. ※2011/11/05更新
    resolvers += Classpaths.typesafeResolver
    
    addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse" % "1.4.0")
    
  5.  sbtを起動。 sbteclipseのダウンロードが行われる。 
  6. eclipse create-srcを実行すると、.projectファイルと.classpathファイルが作成される。
  7. eclipseにて、.projectファイルが存在するパスを指定し、プロジェクト毎import。
という感じです。

    2011年7月25日月曜日

    LiftとMongoDBとRogueを使ってみる。(その2:Rogue設定編)

    以前の続きです。


    Rogueを使ってみます。


    Rogueについてはこちらを参照。

    Rogue: A Type-Safe Scala DSL for querying MongoDB

    Foursquareが公開しているMongoDBクエリ用のライブラリ。型安全なScala-DSLだそうで。

    型安全なクエリを実行できるのは素晴らしいですね。
    正直あまりよくわかっていないのですが、ひとまず開発環境の足がかりを作ります。

    ※赤字は追記・変更点。


    まずは2011年7月段階で最新安定版のLift 2.3をダウンロード。

    ダウンロードしたファイルを解答し、「lift_basic」フォルダをどこかに展開。

    project/build/LiftProject.scalaを下記の通り変更。


    import sbt._
    
    class LiftProject(info: ProjectInfo) extends DefaultWebProject(info) {
    
     val liftVersion = "2.3"
    
     // uncomment the following if you want to use the snapshot repo
     val scalatoolsSnapshot = ScalaToolsSnapshots
    
     // If you're using JRebel for Lift development, uncomment
     // this line
     // override def scanDirectories = Nil
    
     override def libraryDependencies = Set(
       "net.liftweb" %% "lift-webkit" % liftVersion % "compile",
       "net.liftweb" %% "lift-mapper" % liftVersion % "compile",
       "org.mortbay.jetty" % "jetty" % "6.1.22" % "test",
       "junit" % "junit" % "4.5" % "test",
       "ch.qos.logback" % "logback-classic" % "0.9.26",
       "org.scala-tools.testing" %% "specs" % "1.6.6" % "test",
       "com.h2database" % "h2" % "1.2.138",
       "com.foursquare" %% "rogue" % "1.0.14-SNAPSHOT" withSources()
      ) ++ super.libraryDependencies
    
    }
    



    sbtを起動し、updateして必要なライブラリをすべてダウンロードできているようであればOKです。

    "com.foursquare" %% "rogue" % "1.0.14-SNAPSHOT" withSources()
    
    の"1.0.14-SNAPSHOT"については、下記のリポジトリを参考にその時々のバージョンを指定して下さい。
    http://scala-tools.org/repo-snapshots/com/foursquare/


    続いてMongoDBへの接続設定。
    正直なところ下記を参照して適当に設定しています。
    http://www.assembla.com/spaces/liftweb/wiki/Mongo_Configuration


    src/main/scala/code/model/MongoConfig.scalaを下記内容で作成。



    package code.model
    import net.liftweb._
    import mongodb._
    import util.Props
    import com.mongodb.{Mongo, ServerAddress}
    
    object MongoConfig {
      def init: Unit = {
        val srvr = new ServerAddress("127.0.0.1", 27017)
    
        MongoDB.defineDb(DefaultMongoIdentifier, new Mongo(srvr), "foo")
      }
    }
    



    別マシンでmongodを実行している場合や、DB名を指定する場合にどういうことをすれば良いのかは不明ですが、まぁ気にしないことにします。 




    そしてsrc/main/scala/bootstrap/liftweb/Boot.scalaにて、上で定義した設定処理を実行するように下記を追記。


    class Boot {
      def boot {
        if (!DB.jndiJdbcConnAvailable_?) {
         //MongoDb boot
         MongoConfig.init
    (後略)
    





    ひとまず設定はこんなところです。




    ********

    TODO
    1. Lift+RogueでCRUDしてみる。
    ********
    サンプルはgithubにて公開。
    https://github.com/RKTM/Lift-MongoDb-Rogue-Sample