การใช้งาน Maven ในการพัฒนา Grails Application

เห็นทาง Spring66 เขามีการใช้งาน Maven มาช่วยจัดการเกี่ยวกับ dependencies ของ Library ต่างๆ รวมทั้งช่วยในการพัฒนา เช่น build, compile, deploy ..etc..

ดังนั้นทาง Grails66 จะยอมน้อยหน้าได้ไง [ มาช้าหน่อยแต่ยังดีกว่าไม่มานะ ... ]

ที่มา :: http://www.grails.org/Maven+Integration

ใน Grails version 1.1.x ขึ้นมานั้นจะสนับสนุน Maven เพื่อนำมาใช้ใน Grails Application ดังนี้

1. dependencies ของ Library
2. ใช้ในการพัฒนา Grails Application เหมือนกับการใช้ Grails Command
3. เหมาะสำหรับคนที่ใช้ Maven มาอยู่แล้วหรือ คนที่ต้องการศึกษา

ไม่พูดมาก … มาเข้าประเด็นดีกว่า …. ใช้งานยังไงบ้าง

Requirements
1. Download maven version 2.10 หรือ version ล่าสุดมาเลยครับ
2. ต้องต่อ internet ด้วยครับ ยิ่งแรงยิ่งดี เพราะว่า อย่างที่บอกไปว่า maven จะใช้จัดการ dependencies ของ
library ที่จำเป็นต้องใช้ใน application ดังนั้น maven จะไป download library จาก repository ของ maven ครับ

การติดตั้งและการปรับแต่ง
1. ทำการ extract maven ไว้สักที่ เช่น c:\maven
2. กำหนดค่าของ M2_HOME และ PATH ดังนี้
:: Windows ::
set M2_HOME=c:\maven
set PATH=.;%M2_HOME%\bin;%PATH%

:: Linux ::
M2_HOME=/usr/maven
PATH=.:$M2_HOME/bin:$PATH
export M2_HOME PATH

ถ้าสามารถ run mvn ได้ใน command line ก็ถือว่าติดตั้งและ configuration เรียบร้อยครับ

3. ทำการปรับแต่งข้อมูลใน file settings.xml ซึ่งจะอยู่ที่
%M2_HOME%/conf หรือ %YOUR_DOCUMENT_HOME%/.m2 ดังนี้

<settings>
  …
  <pluginGroups>
    <pluginGroup>org.grails</pluginGroup>
  </pluginGroups>
</settings>

4. มาเริ่มสร้าง Grails Appplication ด้วย maven กัน ด้วยชุดคำสั่งดังนี้

mvn org.apache.maven.plugins:maven-archetype-plugin:2.0-alpha-4:generate -DarchetypeGroupId=org.grails  -DarchetypeArtifactId=grails-maven-archetype  -DarchetypeVersion=1.0  -DarchetypeRepository=http://snapshots.repository.codehaus.org  -DgroupId=example -DartifactId=hello_app

หลังจาก run คำสั่งนี้ก็ใส่ค่าต่างๆ ไป เช่น
Define value for version : 1.0
Define value for package : up1.poc
แล้วตอบ Y = Yes
maven จะสร้าง folder ชื่อ hello_app ตามที่กำหนดไว้ใน -DartifactId=hello_app จะมีหน้าตาดังนี้
- pom.xml
- folder src

ต่อมาทำการสร้างโครงสร้างต่างๆ ของ Grails Application กันด้วยคำสั่ง
>cd hello_app
>mvn initialize

ผลการทำงานคือ จะสร้าง folder ต่างๆ ตามโครงสร้างของ Grails Application และ download libraries มากมายเหลือเกิน ….
ต้องรอและทำใจนิดหน่อยครับ มีหน้าตาดังนี้

ส่วนโครงสร้างจะเป็นดังรูป

5. ต่อไปทำการสร้าง Domain class และ Controller class ตามลำดับด้วยชุดคำสั่งดังนี้
>mvn grails:create-domain-class
>mvn grails:create-controller

6. ทำการ run Grails Application ด้วยชุดคำสั่ง
>mvn grails:run-app

เท่านี้ก็ใช้งาน maven สำหรับการพัฒนา Grails Application ได้แล้วครับ ลองใช้งานกันดูครับ ^_^ ^_^ ^_^

Tags:

3 Responses to “การใช้งาน Maven ในการพัฒนา Grails Application”

Trackbacks/Pingbacks

  1. เกรลส์ หกสิบหก » การใช้งาน JMS ใน Grails Part 1 :: ใช้งานร่วมกับ Spring Framework
  2. Link Meven Tutorial « Open Source Java Blog
  3. Link maven tutorial « Open Source Java Blog

Leave a Reply