Win篇/Rails環境つくれない人に捧げるGrailsであっという間にWebアプリ作れる環境構築、あるいは10分であなたのPC上にWebアプリが作れるGrails導入超入門

■1. JDK導入

導入済みの方は次へどうぞ。
ここ http://www.oracle.com/technetwork/java/javase/downloads/index.html からJDKをダウンロードします。Accept License Agreement して自分の環境に合ったものを選べばOK。Windowsなら32bitか64bitかの2択。ダウンロードが終わったら起動して、言われるがままにインストールすればおわりです。

■2. Grails導入

ここ http://grails.org/ でDownload Grails 2.0.3 をクリックしてGrailsのバイナリをダウンロードします。
解凍したフォルダ grails-2.0.3 を c:\ 直下に置いて c:\grails-2.0.3\ なるフォルダがある状態にすればOK。

■3. 環境変数

ここ http://grails.org/doc/latest/guide/gettingStarted.html#requirements にあるように、環境変数を設定します。

WindowsXP なら「マイコンピュータ」右クリックの「プロパティ」から「詳細設定」タブにある「環境変数」。
Windows7 なら「コンピュータ」右クリックの「プロパティ」「システムの詳細設定」から「詳細設定」タブにある「環境変数」。

下側のシステム環境変数で「新規」、変数名 GRAILS_HOME 変数値 c:\grails-2.0.3 として新しい環境変数をつくる。
変数名 JAVA_HOME 変数値 C:\Program Files\Java\jdk1.7.0_03 も新規につくる。
もうひとつ、変数 Path というのがあるのでそいつを「編集」して、末尾に ;%GRAILS_HOME%\bin を書き足す(セミコロン ; を忘れないこと!)。

■4. Grailsの実行

コマンドプロンプトを立ち上げ、

> cd \grails-2.0.3
> grails help

と2つコマンドを打つ。すると

 Usage (optionals marked with *):
 grails [environment]* [target] [arguments]*
 
 Examples:
 grails dev run-app
 grails create-app books
 
 Available Targets (type grails help 'target-name' for more
 grails add-proxy
 grails bootstrap
 grails bug-report
(中略)
 grails uninstall-plugin
 grails upgrade
 grails war

という感じでコマンド一覧が出力されたら、もうGrailsの環境構築は終わり!

■5. Grails上で動くコントローラの作成

お次は

> grails create-app hello-world

と打って、hello-world というアプリを作ってみましょう!少しのあいだごにょごにょ動いて、

Created Grails Application at C:\grails-2.0.3/hello-world
と出ればOKです。
> cd hello-world
でいま作ったhello-worldアプリのディレクトリに入りましょう。
> grails create-controller say
として、SayController を作成します。
Created file grails-app/controllers/hello/world/SayController.groovy
Created file grails-app/views/say
Created file test/unit/hello/world/SayControllerTests.groovy
と出るので、 grails-app/controllers/hello/world/SayController.groovy を次のように編集してみましょう。
 package hello.world
 
 class SayController {
 
     def index() { 
         render "Hello World!"
     }
 }
そして、コマンドプロンプト
> grails run-app
とすると、ごにょごにょ動いて
Compiling 9 source files....
Server running. Browse to http://localhost:8080/hello-world
と出るので、ブラウザで http://localhost:8080/hello-world を開きましょう。
Available Controllers: hello.world.SayController
というのがあるので、「hello.world.SayController」をクリックすると…! 「Hello World!」が表示されましたね! いま、あなたが作ったWebアプリの、最初の一歩です! データ(Domain Class)からControllerやViewを自動生成してくれるScaffold、ユーザのログイン機能を簡単に作れるSpring Security CoreプラグインGrails開発を便利に行えるEclipseベースの統合開発環境SpringSource Tool Suite(STS)など、まだまだ喋り足りませんが、今日はこのへんで。 見たら嬉しいもの: