'Grails'에 해당되는 글 1건
A Simple Web App using Grails :: 2009/07/18 23:18

기본 사상이 같은 Ruby 언어 기반의 Ruby on Rails와 종종 비교되어 지는데, 자바 개발자들에게는 기존에 작성된 Java 라이브러리를 그대로 사용할 수 있는 Grails가 더 매력적인 것 같다. 예전에 관심을 가지고 Rails를 학습했던 적인 있었는데, 나름 매력을 느꼈었지만 그냥 새로운 기술에 대한 학습으로 만족해야만 했던 적이 있다.
참고로, Grail의 원래 의미는 the Holy Grail(성배)로 예수님의 최후에 만찬에서 사용한 잔으로 기적적인 힘을 가졌다고 알려져 왔으며 복수개가 존재하지는 않는다. 초기에 'Groovy on Rails'라고 부르다가 Ruby on Rails의 창시자인 David Heinemeier Hansson의 요청에 대한 응답으로 Grails로 바꾸어 사용했다고 한다.
Grails를 이용하여 초간단 웹어플리케이션을 만들어 보자.
1. Grails 설치하기
http://www.grails.org/Download에서 최신 버전을 다운로드한 후 설치할 디렉토리를 정해 푼다. 이 설치 디렉토리를 GRAILS_HOME 환경변수로 지정하고, 서브디렉토리인 bin 디렉토리를 PATH에 잡아주면 된다. 물론 JAVA_HOME도 환경변수로 지정되어 있어야 한다. 쉘에서 grails 커맨드를 실행해서 정상적인 메시지가 나오면 설치가 완료된 것이다. 아래는 하나의 설정 예이다.
SET JAVA_HOME=C:\Java\jdk1.5.0 SET GRAILS_HOME=C:\Tools\grails-1.1.1 SET PATH=.;%JAVA_HOME%\bin;%GRAILS_HOME%\bin;%PATH%; |
2. 어플리케이션 생성하기
빌드인 타겟(target) 중 하나인 create-app 을 실행하여 어플리케이션 프로젝트를 생성한다. 기본적으로 이클립스 프로젝트를 만들며, 아래와 같이 프로젝트 기본 디렉토리들을 생성하게 된다. 예로서 AutoMart라는 어플리케이션을 생성한 모습이다.
C:\Works>grails create-app AutoMart
Welcome to Grails 1.1.1 - http://grails.org/
Licensed under Apache Standard License 2.0
Grails home is set to: C:\Tools\grails-1.1.1
Base Directory: C:\Works
Running script C:\Tools\grails-1.1.1\scripts\CreateApp_.groovy
Environment set to development
[mkdir] Created dir: C:\Works\AutoMart\src
[mkdir] Created dir: C:\Works\AutoMart\src\java
[mkdir] Created dir: C:\Works\AutoMart\src\groovy
[mkdir] Created dir: C:\Works\AutoMart\grails-app
[mkdir] Created dir: C:\Works\AutoMart\grails-app\controllers
[mkdir] Created dir: C:\Works\AutoMart\grails-app\services
[mkdir] Created dir: C:\Works\AutoMart\grails-app\domain
[mkdir] Created dir: C:\Works\AutoMart\grails-app\taglib
[mkdir] Created dir: C:\Works\AutoMart\grails-app\utils
[mkdir] Created dir: C:\Works\AutoMart\grails-app\views
[mkdir] Created dir: C:\Works\AutoMart\grails-app\views\layouts
[mkdir] Created dir: C:\Works\AutoMart\grails-app\i18n
[mkdir] Created dir: C:\Works\AutoMart\grails-app\conf
[mkdir] Created dir: C:\Works\AutoMart\test
[mkdir] Created dir: C:\Works\AutoMart\test\unit
[mkdir] Created dir: C:\Works\AutoMart\test\integration
[mkdir] Created dir: C:\Works\AutoMart\scripts
[mkdir] Created dir: C:\Works\AutoMart\web-app
[mkdir] Created dir: C:\Works\AutoMart\web-app\js
[mkdir] Created dir: C:\Works\AutoMart\web-app\css
[mkdir] Created dir: C:\Works\AutoMart\web-app\images
[mkdir] Created dir: C:\Works\AutoMart\web-app\META-INF
[mkdir] Created dir: C:\Works\AutoMart\lib
[mkdir] Created dir: C:\Works\AutoMart\grails-app\conf\spring
[mkdir] Created dir: C:\Works\AutoMart\grails-app\conf\hibernate
......
Created Grails Application at C:\Works/AutoMart |
이클립스에서 프로젝트를 임포트하면 다음과 같은 모습이다. 프로젝트 속성의 Java Build Path에서 GRAILS_HOME이라는 변수를 추가해서 설치디렉토리를 지정을 해줘야 참조 라이브러리들을 연결할 수 있다.

Grails가 제공하는 빌드인 타겟을 보려면 쉘에서 grails hlep 명령을 사용하면 아래와 같이 볼 수 있다.
C:\Works>grails help Welcome to Grails 1.1.1 - http://grails.org/ Licensed under Apache Standard License 2.0 Grails home is set to: C:\Tools\grails-1.1.1 Base Directory: C:\Works\AutoMart Running script C:\Tools\grails-1.1.1\scripts\Help_.groovy Environment set to development 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 info): grails bootstrap grails bug-report grails clean grails compile grails console grails create-app grails create-controller grails create-domain-class grails create-filters grails create-integration-test grails create-plugin grails create-script grails create-service grails create-tag-lib grails create-unit-test grails doc grails generate-all grails generate-controller grails generate-views grails help grails init grails install-plugin grails install-templates grails list-plugins grails package grails package-plugin grails plugin-info grails release-plugin grails run-app grails run-war grails schema-export grails set-proxy grails set-version grails shell grails stats grails test-app grails uninstall-plugin grails upgrade grails war |
3. 도메인 클래스 생성하기
어플리케이션 디렉토리로 이동하여 create-domain-class 타겟을 수행하여 도메인 클래스(domain class)를 생성할 수 있다. 에로서 Car 클래스를 생성한 모습이다.
C:\Works\AutoMart>grails create-domain-class Car Welcome to Grails 1.1.1 - http://grails.org/ Licensed under Apache Standard License 2.0 Grails home is set to: C:\Tools\grails-1.1.1 Base Directory: C:\Works\AutoMart Running script C:\Tools\grails-1.1.1\scripts\CreateDomainClass.groovy Environment set to development Created DomainClass for Car Created Tests for Car |
${어플리케이션}/grails-app/domain 디렉토리에 도메인 클래스명으로 하나의 .groovy 파일(Car.groovy)과 ${어플리케이션}/test/unit 디렉토리에 해당 클래스에 대한 단위 테스트 파일(CarTests.groovy)이 생성된다. 도메인 클래스인 Car.groovy 파일을 열어 다음과 같이 필드를 추가해주고 저장한다.
class Car {
String make
String model
String year
Double price
static constraints = {
}
} |
4. 컨트롤러와 뷰 생성하기
generate-all 타겟을 실행하여 도메인 클래스에 대한 컨트롤러(controller)와 뷰(view)를 생성한다. 해당 도메인 클래스에 대한 CRUD(Create/Read/Update/Delete) 기능을 구현한 하나의 컨트롤러와 뷰들을 생성하는데, Grails는 Rails에서와 마찬가지로 이러한 Scaffolding을 지원한다.
C:\Works\AutoMart>grails generate-all Car Welcome to Grails 1.1.1 - http://grails.org/ Licensed under Apache Standard License 2.0 Grails home is set to: C:\Tools\grails-1.1.1 Base Directory: C:\Works\AutoMart Running script C:\Tools\grails-1.1.1\scripts\GenerateAll.groovy Environment set to development ... Generating views for domain class Car ... Generating controller for domain class Car ... Finished generation for domain class Car |
${어플리케이션}/grails-app/controllers 밑에 도메인 클래스에 대한 하나의 컨트롤러 파일(CarController.groovy)이 생성되고, ${어플리케이션}/grails-app/views/car 와 같은 뷰 디렉토리가 생기며 그 곳에 뷰 처리용 GSP(Groovy Server Pages) 파일들이 생성이 된다. 이렇게 간단하지만 완전한 하나의 웹어플리케이션을 만든 것이다.
5. 웹어플리케이션 실행하기
다음과 같이 프로젝트 디렉토리에서 run-app 타겟을 사용하여 웹어플리케이션을 바로 실행해 볼 수 있다. 서블릿 컨테이너로는 Jetty를 내부적으로 사용한다. http://localhost:8080/AutoMart 주소로 접근하면 어플리케이션 시작 페이지를 볼 수 있다. 기본 포트인 8080 포트를 다른 포트로 바꾸려면 -Dserver.port=8081과 같은 실행 옵션에 추가해 주면 된다.
C:\Works\AutoMart>grails run-app Welcome to Grails 1.1.1 - http://grails.org/ Licensed under Apache Standard License 2.0 Grails home is set to: C:\Tools\grails-1.1.1 Base Directory: C:\Works\AutoMart Running script C:\Tools\grails-1.1.1\scripts\RunApp.groovy Environment set to development Running Grails application.. Server running. Browse to http://localhost:8080/AutoMart |
다음은 Car 객체를 생성하여 신규로 등록하는 화면이다. Grails는 개발과 테스트 모드에서 기본적으로 hsqldb를 In-memory 형태로 사용하여 데이터를 저장하고 종료시 저장 데이터는 사라진다.

다음은 등록된 Car 정보들에 대한 리스트 화면이다.

6. WAR 파일로 배포하기
어플리케이션 프로젝트 디렉토리에서 grails war 명령을 실행하면 웹어플리케이션 .war 파일이 생성된다. 표준 자바 웹어플리케이션으로 대부분의 어플리케이션 서버에 배포되어 질 수 있다. (여기 참조)
C:\Works\AutoMart>grails war Welcome to Grails 1.1.1 - http://grails.org/ Licensed under Apache Standard License 2.0 Grails home is set to: C:\Tools\grails-1.1.1 Base Directory: C:\Works\AutoMart Running script C:\Tools\grails-1.1.1\scripts\War.groovy Environment set to production ... Done creating WAR C:\Works\AutoMart/AutoMart-0.1.war |

