2011年11月20日日曜日

Scala(sbt)でjoda-time 2.0を使う設定。

背景

Scala(ビルドツールはxsbt)でjoda-time 1.6を使っていて、
@xuwei_kさんのまさしくこの状況にハマりました。
joda timeのDateTimeのコンストラクタがScalaと相性悪い・・・と思ったら単なる勘違いだった件

jodatimeのコンストラクタをScalaから呼べないとおもったら、単に自分の勘違いだった件

scalatimeというjoda timeをScalaでラップしたようなライブラリがあるのですが、
それはおいておいて、とりあえずjoda time 2.0使おうと思ってちょっとはまったのでメモ的に記します。

設定

~/build.sbtに下記の設定をします。


libraryDependencies ++= Seq(
  "joda-time" % "joda-time" % "2.0",  //*1
  "org.joda" % "joda-convert" % "1.1"  //*2
)

*1は普通にjoda-time 2.0を取得する設定ですが、
*2を加えています。

*1だけですと、
error: error while loading DateTime, Missing dependency 'class
org.joda.convert.FromString', required by
/Users/xxxxx/.m2/
repository/joda-time/joda-time/2.0/joda-time-2.0.jar(org/joda/time/DateTime.class)

のようなエラーが出ます。

この原因は、

https://groups.google.com/d/topic/scala-user/aM4LNwNksXQ/discussion

によると、
「joda-convertはmavenのpom上ではOptionalであり、Javaでは取得できなくても動くけど、
Scalaだとすべてのclassが参照するclassはcompile時にclasspath上に存在しないといけないのでエラーになるよ」
とのこと(意訳)。

ということで、sbtでは*2を明示的に加えてあげる必要があります。

xsbtでScalaのcompileのオプションを設定する方法。

Liftで書いていて、xsbtの

compile
 時に

there were 1 deprecation warnings; re-run with -deprecation for details
と出てきていたものの、どうやって設定するのやら悩んでいましたが、
下記の設定でOKでした。

set scalacOptions += "-deprecation"

2011年10月19日水曜日

xsbt + xsbt-web-plugin + scala 2.9.1のサンプル。

  • sbt 0.11(xsbt)
  • xsbt-web-plugin
  • scala 2.9.1

の組み合わせのサンプルを発見。
バージョン指定の方法で毎度悩むので、2011年10月19日現在の最新(と思われる)この組み合わせを使ってliftをやろうかと。

https://github.com/schmmd/helloworld-scala-servlet

(via https://github.com/siasia/xsbt-web-plugin/wiki/

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

    2011年6月19日日曜日

    Windowsの環境変数をsetxで永続的に設定


    Windowsの環境変数を永続的(マシン再起動後にも設定が有効となるように)、かつ、スクリプトで自動で設定できないかなぁと調べてみました。

    Windows Support Toolsに含まれるsetxコマンドを使えば良いとのこと。詳細は以下リンクをご参照。

    setxで環境変数の値を設定する(基本編) 


    で困ったのが、複数の環境変数をsetxで設定した後に、それらをユーザー変数のPATHにどんどん追加しようとした時。何故か最後のsetxのみ有効になってしまうという。

    ということで下記のような書き方で対応。
    一時的な変数USER_PATHにどんどん追加していって、最後にsetxコマンドで環境変数に設定するというやり方です。
    set USER_PATH=%USER_PATH%;"%SCALA_HOME%\bin"
    set USER_PATH=%USER_PATH%;%SBT_HOME%
    setx PATH %USER_PATH%
    ちなみに"%SCALA_HOME%\bin"のところがダブルクオーテーションで囲われているのは、パスに半角スペースが含まれており、それをエスケープするためです。


    参考リンク