leftovers...

about grails groovy

Grailsプラグイン + Mavenをローカルリポジトリでの注意点とかメモ

結論が出てるのか出てないのか、自分でもわからなくなってきたぞ!
って事で自分なりの結論を先に書く。
あとはグダグダなメモなのでツッコミは無しで・・・。

結論+わかった事

  1. ローカルリポジトリにあるプラグインと依存関係があるプラグインは、設定しなくてもコンパイルが通ってしまうが、mavenLocal()は必要。
  2. ローカルリポジトリに置いておく依存参照されるプラグインgroupIdは'org.grails.plugins'に設定しないとダメみたい!
  3. このあたりの検証は疲れる。


もう一回いっておく。ダグダなメモなのでツッコミは無しで・・・。
験用アプリ、プラグインを作成
mainapp - プラグインを使用するGrailsアプリ
myplugin-one - 適当なプラグイン
myplugin-two - 適当なプラグイン

myplugin-depends - プラグイン1,2が参照するプラグイン

つまり以下の関係になる。

mainapp
    |-myplugin-one --dependsOn-- myplugin-depends
    |-myplugin-two --dependsOn-- myplugin-depends

myplugin-dependsを作成。maven-publisherプラグインをインストール。
grails install-plugin maven-publisher
グループ定義を追加。

class MypluginDependsGrailsPlugin {
    def group = 'jp.xmldo.grails'
..省略..
}

myplugin-dependsをmaven-installしてローカルリポジトリに追加。

grails maven-install

適当なドメインクラスMessageをmyplugin-dependsに生成。

grails create-domain-class Message

そのドメインクラスを継承するドメインクラスを、myplugin-one、myplugin-twoに作成して、それぞれmyplugin-one、myplugin-twoプラグインにdependsOn、loadAfterを定義する。

    def dependsOn = ['myplugin-depends':'0.1']
    def loadAfter = ['myplugin-depends']

myplugin-oneをgrails compileする。もちろん、この時点では、ドメインクラスMessageが無いのでコンパイルエラー
BuildConfig.groovyに依存定義をする。

    plugins {
        runtime 'jp.xmldo.grails:myplugin-depends:0.1'
    }

この時点では、mavenLocal()は参照させていない。
もう一度コンパイル
依存プラグインがインストールされて、コンパイルOK。
しかし、grails cleanして、もう一度コンパイル。するとエラー。
よく見ると、myplugin-dependsにドメインクラスを追加した後にバージョンを上げていなかったから、内容が更新されていない。
つまり、当たり前の動作で、

  • maven-installを行う。
  • ドメインクラス追加。
  • バージョンを上げずにmaven-installを行う。
  • 他から見ると最新になっていない。

なので、バージョンを調整して再度実行。

grails clean
grails compile

問題無くコンパイルOK
この時点では、mavenLocal()は参照させていない。

コンパイルもできたので、myplugin-oneをmaven-installする。
そして、mainappで読みこんでみる。

    plugins {
        runtime 'jp.xmldo.grails:myplugin-one:0.1'
    }
  • mavenLocal()設定無し。=> もちろん失敗。
  • mavenLocal()を設定する。 => myplugin-dependsが見つからないよ!と言われる。
          ::::::::::::::::::::::::::::::::::::::::::::::

          ::          UNRESOLVED DEPENDENCIES         ::

          ::::::::::::::::::::::::::::::::::::::::::::::

          :: org.grails.plugins#myplugin-depends;0.2: not found

          ::::::::::::::::::::::::::::::::::::::::::::::

ここで言いたいのは、さっきmyplugin-oneをmaven-installしたときは問題無くコンパイルとインストールが行われたじゃないか!で、何で見つからないとか言うのだよ!
気を取り直して、myplugin-oneに戻る。
myplugin-oneのBuildConfig.groovyにmavenLocal()を設定する。そしてmyplugin-oneバージョンも更新。

まだ、UNRESOLVED DEPENDENCIES、org.grails.plugins#myplugin-depends;0.2: not found
そもそも、groupIdはorg.grails.pluginsでなくて、jp.xmldo.grailsだよ!
なので最終的に。myplugin-dependsのgroupIdをorg.grails.pluginsに変更してやる!

  • myplugin-dependsのgroupIdをorg.grails.pluginsに変更
  • myplugin-dependsをmaven-installする。=> これで 'org.grails.plugins:myplugin-depends:0.2'という別物が完成。
  • myplugin-oneの参照している箇所も変更。バージョンも更新。'jp.xmldo.grails:myplugin-one:0.3'に更新
  • mainappのBuildConfig.groovy内も変更。

いざ!コンパイル実行!

  • 1回のコンパイル => エラー => 原因'jp.xmldo.grails:myplugin-one:0.3'を最初に入れたときの依存に問題。この時のBuildConfigは読んでいない?但しdependsOnを認識している。
  • 2回目クリーンしてコンパイル。=> この時の状況。myplugin-oneはインストールされている。

コンパイル成功!

Grailsインストール+簡単な認証付きアプリをサックリ作成して動作確認。

いまさらだけど書いてみる

ダウンロードページからBinary ZIPあたりをダウンロード
ダウンロードしたzipファイルを解凍

解凍先が/opt/grails-1.0.3の場合

$ export GRAILS_HOME=/opt/grails-1.0.3
  • PATHを通す
$ export PATH=$PATH:$GRAILS_HOME/bin
  • 動作確認(1)
$ grails

Welcome to Grails 1.0.3 - http://grails.org/
Licensed under Apache Standard License 2.0
Grails home is set to: /opt/grails-1.0.3
  • 動作確認(2)
$ grails create-app simple ←アプリ作成
..省略..
     [copy] Copying 1 file to /works/tmp/junk/simple
     [copy] Copying 1 file to /works/tmp/junk/simple
     [copy] Copying 1 file to /works/tmp/junk/simple
     [copy] Copying 1 file to /works/tmp/junk/simple
[propertyfile] Updating property file: /works/tmp/junk/simple/application.properties
Created Grails Application at /works/tmp/junk/simple

$ cd simple ←ディレクトリに移動
$ grails create-domain-class message ← ドメインクラス作成
$ tee grails-app/domain/Message.groovy <<EOF ←ドメインクラス編集
class Message {
  String title
  String message
  Date dateCreated
  Date lastUpdated
  static constraints = {
    title(nullable:false)
    message(maxSize:5000)
  }
}
EOF
$ grails generate-all Message ←スカッフォルド実行
..省略..
Generating views for domain class Message ...
Generating controller for domain class Message ...
Finished generation for domain class Message
$ grails run-app ←アプリ起動
..省略..
2008-09-15 16:27:49.903:/simple:INFO:  Initializing Spring FrameworkServlet 'grails'
2008-09-15 16:27:49.034::INFO:  Started SelectChannelConnector@0.0.0.0:8080
Server running. Browse to http://localhost:8080/simple

ブラウザで確認、http://localhost:8080/simple/

  • 動作確認(3) 認証を・・
↓ Acegiプラグインインストール
$ grails install-plugin http://svn.codehaus.org/grails-plugins/grails-acegi/tags/RELEASE_0_3/grails-acegi-0.3.zip
..省略..
Loading with installed plug-ins: ["acegi"] ...
Executing acegi-0.3 plugin post-install script ...
Plugin acegi-0.3 installed
Plug-in provides the following new scripts:
------------------------------------------
grails create-auth-domains
grails generate-manager
grails generate-registration
grails security-targets

$ grails create-auth-domains ← 認証用ドメイン生成
$ grails generate-manager  ← 仮の認証用ドメイン管理ページなど生成
$ grails run-app ←アプリ起動

ロールを追加
http://localhost:8080/simple/role/create

ユーザを追加
http://localhost:8080/simple/user/create

権限を追加
http://localhost:8080/simple/requestmap/create

以下のページをみようとすると・・
http://localhost:8080/simple/message
ログインページへ飛ばされます。
http://localhost:8080/simple/login/auth

作成したユーザID&パスワードでログインできます。