(Quick Reference)

2 スタートガイド - Reference Documentation

Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith

Version: null

Translated by: T.Yamamoto, Japanese Grails Doc Translating Team.
【注意】このドキュメントの内容はスナップショットバージョンを元に*意訳*されているため、一部現行バージョンでは未対応の機能もあります。

2 スタートガイド

2.1 インストール必要条件

Before installing Grails you will as a minimum need a Java Development Kit (JDK) installed version 1.6 or above and environment variable called JAVA_HOME pointing to the location of this installation. On some platforms (for example OS X) the Java installation is automatically detected. However in many cases you will want to manually configure the location of Java. For example:
Grailsをインストールする前に、Java Development Kit (JDK) 1.6以上がインストール済みで、JAVA_HOMEが指定されている必要があります。一部のプラットフォームでは(OS Xの例で言うと)、自動的にJavaのインストール先を認識します。手動で定義する場合等、必要に応じて次のようにJavaの設定を行ってください。

export JAVA_HOME=/Library/Java/Home
export PATH="$PATH:$JAVA_HOME/bin"

Note that although JDK 1.6 is required to use Grails at development time it is possible to deploy Grails to JDK 1.5 VMs by setting the grails.project.source.level and grails.project.target.level settings to "1.5" in grails-app/conf/BuildConfig.groovy:
JDK1.6はGrailsでの開発時に必要です。grails-app/conf/BuildConfig.groovyに、grails.project.source.levelgrails.project.target.levelを"1.5"と指定することで、JDK1.5の環境に対してのデプロイが可能です。

grails.project.source.level = 1.5
grails.project.target.level = 1.5

In addition, Grails supports Servlet versions 2.5 and above. If you wish to use newer features of the Servlet API (such as 3.0) you should configure the grails.servlet.version in BuildConfig.groovy appropriately:
Grailsでは、2.5とそれ以上のサーブレットのバージョンに対応しています。 Servlet API(3.0等)の新機能を使用したい場合は、BuildConfig.groovygrails.servlet.versionの定義をしてください。

grails.servlet.version = "3.0"

2.2 ダウンロードとインストール

The first step to getting up and running with Grails is to install the distribution. To do so follow these steps:
まず最初にGrailsをインストールすることから始めましょう。手順は次のようになります:

  • Download a binary distribution of Grails and extract the resulting zip file to a location of your choice
  • Set the GRAILS_HOME environment variable to the location where you extracted the zip
    • On Unix/Linux based systems this is typically a matter of adding something like the following export GRAILS_HOME=/path/to/grails to your profile
    • On Windows this is typically a matter of setting an environment variable under My Computer/Advanced/Environment Variables
  • Then add the bin directory to your PATH variable:
    • On Unix/Linux based systems this can be done by adding export PATH="$PATH:$GRAILS_HOME/bin" to your profile
    • On Windows this is done by modifying the Path environment variable under My Computer/Advanced/Environment Variables

  • Grailsをダウンロードし、任意の場所にzipファイルを解凍します。
  • zipファイルを解凍した場所にGRAILS_HOME環境変数を設定します。
    • Unix/Linuxベースのシステムでは、次のようなものをプロファイル(.profileなど)に追加します。export GRAILS_HOME=/path/to/grails
    • Windowsでは、マイコンピュータ>詳細>環境変数に設定します。
  • さらに、PATH変数にbinディレクトリを追加する必要があります。
    • Unix/Linuxベースのシステムでは、このように設定します。export PATH="$PATH:$GRAILS_HOME/bin"
    • Windowsでは、マイコンピュータ>詳細>環境変数のPathに%GRAILS_HOME%binを追加します。

If Grails is working correctly you should now be able to type grails -version in the terminal window and see output similar to this:
ターミナルでgrailsと入力し実行することで下記のような出力がされれば、Grailsが正常に動作しています:


Grails version: 2.0.0

2.3 アプリケーション作成

To create a Grails application you first need to familiarize yourself with the usage of the grails command which is used in the following manner:
Grailsアプリケーションを作成する前に、基本的なgrailsコマンドの使用法に慣れておきましょう。

grails [コマンド名]

Run create-app to create an application:
アプリケーションを作成するコマンドはcreate-appです。


grails create-app helloworld

This will create a new directory inside the current one that contains the project. Navigate to this directory in your console:
このコマンドを実行することにより、現在のディレクトリ内にプロジェクトが含まれる新しいディレクトリが作成されます。コンソールで、このディレクトリに移動してください。


cd helloworld

2.4 ハローワールド例

To implement the typical "hello world!" example cd into the "helloworld" directory created in the previous section and activate interactive mode:
単純な"Hello World!"を実装してみましょう。前のセクションで作成されたディレクトリ"helloworld"にcdコマンドで移動してインタラクティブモードを開始します。

$ cd helloworld
$ grails

Grails' interactive mode will be activated and you should see a prompt that looks like the following:
Grailsのインタラクティブモードが開始されたら、プロンプトが次のように表示されます:

Now run the create-controller command:
では、create-controllerコマンドを実行してみましょう:


grails> create-controller hello

This will create a new controller (Refer to the section on Controllers for more information) in the grails-app/controllers directory called helloworld/HelloController.groovy.
このコマンドによって、grails-app/controllersディレクトリ内に、HelloController.groovyという新しいコントローラ(詳細についてはコントローラのセクションを参照してください)が生成されます。

If no package is specified with create-controller script, Grails automatically uses the application name as the package name. This default is configurable with the grails.project.groupId attribute in Config.groovy.
create-controllerスクリプトでパッケージが指定されなかった場合はGrailsが自動的にアプリケーション名をパッケージ名として使用します。デフォルト値はConfig.groovyにgrails.project.groupIdを指定することで変更可能です。

Controllers are capable of dealing with web requests and to fulfil the "hello world!" use case our implementation needs to look like the following:
コントローラは、Webリクエストを処理します。"hello world!"を表示するには、次のように記述します。

package helloworld

class HelloController {

def world() { render "Hello World!" } }

Job done. Now start-up the container with another new command called run-app:
作業は以上です。次にrun-appコマンドで、このアプリケーションを起動させましょう:


grails> run-app

This will start-up a server on port 8080 and you should now be able to access your application with the URL: http://localhost:8080/helloworld
これで8080番ポートでサーバを起動します。http://localhost:8080/helloworldをブラウザで見てみましょう。

The result will look something like the following screenshot:
結果は次のスクリーンショットのようになります:

This is the Grails intro page which is rendered by the web-app/index.gsp file. You will note it has a detected the presence of your controller and clicking on the link to our controller we can see the text "Hello World!" printed to the browser window.
これはgrails-app/views/index.gspファイルによって描写されたアプリケーションの初期画面です。画面にはコントローラへのリンクが表示されています。このリンクをクリックすることで、画面に"Hello World!"が表示されます。

2.5 インタラクティブモードの利用

Grails 2.0 features an interactive mode which makes command execution faster since the JVM doesn't have to be restarted for each command. To use interactive mode simple type 'grails' from the root of any projects and use TAB completion to get a list of available commands. See the screenshot below for an example:
Grails 2.0では、コマンドの起動を速くするために、コマンド毎にJVMに再起動が必要無いインタラクティブモードを提供しています。インタラクティブモードを使用するには、プロジェクトルートで単に'grails'と入力するだけです。使用可能なコマンドをタブ補完することもできます。例としてスクリーンショットを参照してください:

For more information on the capabilities of interactive mode refer to the section on Interactive Mode in the user guide.
さらなるインタラクティブモードの能力と情報に関しては、ユーザガイドのインタラクティブモードセクションを参照してください。

2.6 IDEの設定

IntelliJ IDEA

IntelliJ IDEA and the JetGroovy plugin offer good support for Groovy and Grails developers. Refer to the section on Groovy and Grails support on the JetBrains website for a feature overview.
IntelliJ IDEAJetGroovyプラグインは、Groovy&Grailsの開発者に十分な機能を提供します。機能概要については、JetBrainsのウェブサイトのGroovy and Grailsを参照してください。

To integrate Grails with IntelliJ run the following command to generate appropriate project files:
Grails 1.2でIntelliJの設定をするには、次のコマンドを実行してIntelliJ用のプロジェクトファイルを生成します。

grails integrate-with --intellij

Eclipse

We recommend that users of Eclipse looking to develop Grails application take a look at SpringSource Tool Suite, which offers built in support for Grails including automatic classpath management, a GSP editor and quick access to Grails commands. See the STS Integration page for an overview.
Eclipse ユーザーがGrailsアプリケーションの開発をする際は、SpringSource Tool Suiteを探して取得することをお勧めします。それは自動クラスパス管理機能、GSPエディタやGrailsコマンドへの迅速なアクセス機能を含んだGrailsのためのサポートが組み込まれて提供されています。概要については、STS Integrationのページを参照してください。

NetBeans

NetBeans provides a Groovy/Grails plugin that automatically recognizes Grails projects and provides the ability to run Grails applications in the IDE, code completion and integration with the Glassfish server. For an overview of features see the NetBeans Integration guide on the Grails website which was written by the NetBeans team.
他にも良好なオープンソースのIDEとしてSunのNetBeansがあります。NetBeansはGroovy/Grailsプラグインで自動的にGrailsプロジェクトを認識します。また、IDEでのGrailsアプリケーションの実行、コード補完、SunのGlassfishサーバとの連携などの機能も提供しています。機能概要については、NetBeansチームによって記述されたGrails公式サイト上のNetBeans Integrationガイドを参照してください。

TextMate

Since Grails' focus is on simplicity it is often possible to utilize more simple editors and TextMate on the Mac has an excellent Groovy/Grails bundle available from the Texmate bundles SVN.
単純さに焦点があたっているGrailsは、より単純なエディタを利用することが可能です。そしてMac上の TextMateTextmateにバンドルされたSVNから優秀なGroovy/Grailsバンドルを利用可能です。

To integrate Grails with TextMate run the following command to generate appropriate project files:
GrailsでTextMateの設定をするには、次のコマンドを実行してTextMate用のプロジェクトファイルを生成します。

grails integrate-with --textmate

Alternatively TextMate can easily open any project with its command line integration by issuing the following command from the root of your project:
またTextMateはプロジェクトのルートから次のコマンドを発行することによりコマンドラインと統合し、任意のプロジェクトを簡単に開くことができます:

mate .

2.7 Convention over Configuration 設定より規約

Grails uses "convention over configuration" to configure itself. This typically means that the name and location of files is used instead of explicit configuration, hence you need to familiarize yourself with the directory structure provided by Grails.
Grailsは、"convention over configuration"を使用して、自動的に設定をおこないます。一般的に、名前とファイルの位置が明確な構成の代わりに使われることを意味します。それゆえに、Grailsによって提供されるディレクトリ構造に慣れ親む必要があります。

Here is a breakdown and links to the relevant sections:
概要と関連するセクションへのリンクです。:

2.8 アプリケーションの起動

Grails applications can be run with the built in Tomcat server using the run-app command which will load a server on port 8080 by default:
Grailsアプリケーションは、run-appコマンドを使用することで組み込みTomcatで実行することができます。デフォルトポートは8080番です。

grails run-app

You can specify a different port by using the server.port argument:
起動オプションにserver.portを指定して別のポートで起動することもできます。

grails -Dserver.port=8090 run-app

Note that it is better to start up the application in interactive mode since a container restart is much quicker:

$ grails
grails> run-app
| Server running. Browse to http://localhost:8080/helloworld
| Application loaded in interactive mode. Type 'exit' to shutdown.
| Downloading: plugins-list.xml
grails> exit
| Stopping Grails server
grails> run-app
| Server running. Browse to http://localhost:8080/helloworld
| Application loaded in interactive mode. Type 'exit' to shutdown.
| Downloading: plugins-list.xml

More information on the run-app command can be found in the reference guide.
run-appコマンドの詳細は、リファレンスガイドを参照してください。

2.9 アプリケーションのテスト

The create-* commands in Grails automatically create unit or integration tests for you within the test/unit or test/integration directory. It is of course up to you to populate these tests with valid test logic, information on which can be found in the section on Testing.
Grailsのcreate-*コマンドは、自動的にUnitテストまたは統合テストをそれぞれtest/unitまたtest/integrationディレクトリに生成します。スケルトンのテストのロジックは各自で実装してください。テストの詳細についてはTestingを参考にしてください。

To execute tests you run the test-app command as follows:
テストを実行する場合は、test-appコマンドを使用します:

grails test-app

2.10 アプリケーションのデプロイ

Grails applications are deployed as Web Application Archives (WAR files), and Grails includes the war command for performing this task:
GrailsアプリケーションはWebアプリケーションアーカイブ(WARファイル)としてデプロイされます。Grailsにはアーカイブを作成するためのwarコマンドがあります:

grails war

This will produce a WAR file under the target directory which can then be deployed as per your container's instructions.
コンテナにデプロイ可能なWarファイルがtargetディレクトリ以下に生成されます。

Unlike most scripts which default to the development environment unless overridden, the war command runs in the production environment by default. You can override this like any script by specifying the environment name, for example:
他のほとんどのスクリプトと違い、warコマンドでは、環境がdevelopmentにオーバーライドされて、productionがデフォルトになります。他のスクリプトと同じく環境名を指定することで変更可能です。

grails dev war

NEVER deploy Grails using the run-app command as this command sets Grails up for auto-reloading at runtime which has a severe performance and scalability implications
Grailsを本番運用する際はWARをデプロイしてください。run-appコマンドでの運用は基本的に自動リロードなどが設定されているため、パフォーマンスやスケーラビリティに影響します。

When deploying Grails you should always run your containers JVM with the -server option and with sufficient memory allocation. A good set of VM flags would be:
Grailsをデプロイする場合は、-serverオプションと十分なメモリを割り当てて、Webコンテナを動作させましょう。JVM起動オプションの良い設定は次のようになります:

-server -Xmx512M -XX:MaxPermSize=256m

2.11 サポートされている Java EE コンテナ

Grails runs on any container that supports Servlet 2.5 and above and is known to work on the following specific container products:
GrailsはServlet 2.5をサポートする任意のWebコンテナで動作します。次の製品で動作することが確認されています。
  • Tomcat 7
  • Tomcat 6
  • SpringSource tc Server
  • Eclipse Virgo
  • GlassFish 3
  • GlassFish 2
  • Resin 4
  • Resin 3
  • JBoss 6
  • JBoss 5
  • Jetty 7
  • Jetty 6
  • IBM Websphere 7.0
  • IBM Websphere 6.1
  • Oracle Weblogic 10.3
  • Oracle Weblogic 10
  • Oracle Weblogic 9

Some containers have bugs however, which in most cases can be worked around. A list of known deployment issues can be found on the Grails wiki.
一部のWebコンテナにはバグがありますが、ほとんどの場合では回避することができます。既知の開発時における課題の一覧は、GrailsのWikiにあります。

2.12 アプリケーション生成

To get started quickly with Grails it is often useful to use a feature called Scaffolding to generate the skeleton of an application. To do this use one of the generate-* commands such as generate-all, which will generate a controller (and its unit test) and the associated views:
Grailsでは、アプリケーションのスケルトンを生成するスカッフォールディングの機能を使用することにより、素早く開発することができます。Grailsにはアプリケーションのスケルトンを生成するスカッフォールディングという機能があります。これをするには、コントローラと関連するビューを生成するgenerate-allなどを使用します。

grails generate-all Book

2.13 アーテファクトの作成

Grails ships with a few convenience targets such as create-controller, create-domain-class and so on that will create Controllers and different artefact types for you.
Grailsにはコントローラや様々なアーテファクトを生成するcreate-controllercreate-domain-classなどのコマンドが存在します。
These are just for your convenience and you can just as easily use an IDE or your favourite text editor.
アーテファクト生成は便利機能です。IDEやテキストエディタを使用してもかまいません。
For example to create the basis of an application you typically need a domain model:
例としてドメインモデルを生成するには:

grails create-domain-class book

This will result in the creation of a domain class at grails-app/domain/Book.groovy such as:
このコマンドを実行すると、grails-app/domain/Book.groovyに以下のようなドメインクラスが作成されます:

class Book {
}

There are many such create-* commands that can be explored in the command line reference guide.
他にもcreate-*コマンドがあります。詳しくはコマンドライン・リファレンス・ガイドを参照しましょう。

To decrease the amount of time it takes to run Grails scripts, use the interactive mode.
interactiveモードを使用することでGrailsスクリプトの起動時間を減らすことができます。