Grails 與 Maven 整合概述

來源:互聯網
上載者:User

概述

Grails 簡介:Grails 是一個開源的 Web 開發架構,它採用動態語言 Groovy 進行開發,並且可以和用 Java 開發的項目進行整合。Grails 充分體現了約定優於配置的原則,將項目的配置減到最低。目前主流的 Java Web 架構都過於複雜,如 Structs、Spring、Webwork,這些架構的使用者都需要編寫和維護許多設定檔,並且用這些架構的項目寫法各有不同,沒有統一的標準。Grails 的出現解決了這些問題,使用者可以發現在 Grails 中很少去修改和添加設定檔。

Maven 簡介:Maven 是一個專案管理工具,基於項目物件模型(Project Object Model),Maven 可以管理項目的構建、報告和文檔的產生。Maven 主要用於用 Java 進行開發的項目。

Grails 對於 Maven 的支援:在 Grails 2.1 以前,Grails 外掛程式必須在 BuildConfig.groovy 檔案配置,對 Java 的依賴必須在 pom 檔案中指定。

在 Grails 2.1 以後,對 Maven 整合的支援有了大的改進,現在 pom 檔案也支援引用 Grails 外掛程式,如清單 1:

清單 1. pom.xml 對 Grails 的支援

<dependency>   <groupId>org.grails.plugins</groupId>    <artifactId>hibernate</artifactId>    <version>2.1.0.RC1</version>    <type>zip</type>    <scope>compile</scope> </dependency>

Maven 外掛程式現在可以識別 Grails 依賴的其它外掛程式和 Jar 包。並且 Grails 增加了一個新的命令:create-multi-project-build,這個命令在包括多個 Grails 應用或外掛程式的目錄中被調用,會產生對複雜項目的 Maven 構建。在 Grails2.1 後,對已有 Grails 項目的 Maven 管理,也只需要輸入 create-pom 命令來建立項目對應的 pom 檔案。

開發環境:Grails 2.1、Maven 3、JDK1.6。

單個 Grails 項目與 Maven 的整合

首先運行 create-app 命令來產生 Grails 項目,接著在 Grails 項目的根目錄下用命令 create-pom 產生 POM 檔案。具體執行個體如清單 2:

清單 2. 用命令 create-pom 產生 POM 檔案

#> grails create-app my-app       #> cd my-app       #> grails create-pom build.groupid

我們對上面三條命令進行簡要說明,第一條是 Grails 項目的建立命令,這個命令將會以使用者指定的名稱來建立 Grails 應用,我們指定的名稱是 my-app,所以會產生名稱為 my-app 的 Grails 應用。第二條命令是進入我們建立的 my-app 的根目錄下,因為第三條命令需要在 Grails 的根目錄下運行。第三條命令是為 Grials 應用建立 POM 檔案,使得 Grails 應用可以用 Maven 進行構建,參數 groupid 是指定應用隸屬的項目。

運行 create-pom 命令後,我們可以看到在 my-app 根目錄產生了檔案 pom.xml, 檔案顯示如清單 3:

清單 3. 在 my-app 根目錄產生的 pom.xml 檔案

<?xml version="1.0" encoding="utf-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0      http://maven.apache.org/maven-v4_0_0.xsd">       <modelVersion>4.0.0</modelVersion>       <groupId>build.groupid</groupId>       <artifactId>my-app</artifactId>       <packaging>grails-app</packaging>      <version>0.1</version>       <name>my-app</name>       <description>0.1</description>     ...... </project>

現在可以運行 mvn 的命令做簡單測試,如:mvn package, 可以看到結果如清單 4:

清單 4. 運行 mvn 的命令做簡單測試

|Loading Grails 2.1.0 |Configuring classpath |Running pre-compiled script . |Environment set to prod ................................. |Packaging Grails application .... |Compiling 1 source files ............ |Compiling 3 GSP files for package [myApp] .. |Compiling 4 GSP files for package [databaseMigration] .. |Building WAR file .......................................... |Done creating WAR target\my-app-0.1.war [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2:13.841s [INFO] Finished at: Tue Oct 16 13:31:29 CST 2012 [INFO] Final Memory: 16M/34M [INFO] ------------------------------------------------------------------------

查看本欄目更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.