A WALK THROUGH OF GRAILS-PART 2- Maven Integration

Maven
Maven

This is the part 2 of Walk through Grails. In this post we can see how Maven can be integrated with Grails. You can see the part-1 here.

Why I like Maven
I like maven because of its dependency management and build on top of typical build life cycle. So it is easy to manage the dependency lib files as well as manage the entire build life cycle. The profile based configuration is another important feature where you can manage your dependency and build process as different for each environments.

How to use Maven advantage in Grails?
As we saw in the first post of Grails introduction, the conventional over configuration makes Grails application development fast and reliable. The power of Maven could be a significant advantage on Grails based application. There are two stages of a Grails project we can integrate Maven.

  • Existing Grails Project
  • Grails project from scratch

Existing Grails Project
Grails comes with pretty good command line tool that makes easier your job. Just typing the below command on your Grails Project Home will add maven support.

grails create-pom <group-id> 

create-pom command will expect your group id of the project.

Grails Project from the scratch
The traditional archtype command will do the job for you.

mvn archetype:generate -DarchetypeGroupId=org.grails \
    -DarchetypeArtifactId=grails-maven-archetype \
    -DarchetypeVersion=1.0 \
    -DgroupId=grails-app -DartifactId=grails

Note:
once you generated pom.xml, you need to change

<plugin>
  <artifactId>maven-compiler-plugin</artifactId>
  <configuration>
    <source>1.5</source>
    <target>1.5</target>
  </configuration>
</plugin>

in to

<plugin>
  <artifactId>maven-compiler-plugin</artifactId>
  <configuration>
    <source>1.6</source>
    <target>1.6</target>
  </configuration>
</plugin>

The go to the project home and issue

mvn initialize

Job done. You have integrated maven with Grails application. You can still use all the Grails commands as maven goals. All you need to do is to add the Grails commands in to Maven goals. Some of the commands I have integrated like,


<plugin>
        <groupId>org.grails</groupId>
        <artifactId>grails-maven-plugin</artifactId>
        <version>${grails.version}</version>
        <extensions>true</extensions>
        <executions>
          <execution>
            <goals>
              <goal>init</goal>
              <goal>maven-clean</goal>
              <goal>validate</goal>
              <goal>config-directories</goal>
              <goal>maven-compile</goal>
              <goal>maven-test</goal>
              <goal>maven-war</goal>
              <goal>maven-functional-test</goal>
               <goal>exec</goal>
               <goal>create-controller</goal>
                <goal>run-app</goal>
                <goal>console</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

So if you want to run the application with in-build server all you need to do,

mvn grails run-app 
Advertisements

A walk through of Grails-Part 1-Introduction

Grails logo
Grails

This is the part 1 of Grails (Note: click here to see why it is not called Groovy on Grails?) articles which I will cover the flexibility of Grails. You can see the part 2 here.

One of my friend through this to me to try it out Grails. I explored Ruby on Rails and Codeigniter and I always found more bond of convention over configuration or code over configuration right from the age of Java Beans.The simplicity will reduce the development time and increase the productivity.

With all these years with the web development and specially with java, I found these are the important feature any java web frameworks should support.

I will explore one by one with an example on my fourth coming blog posts. Another important aspect what I found interesting is that the command line tool which Grails provide make our job more easier. The only disappointment I had is it is not supporting Servlet 3.0 Spec, so you can’t really run it on Tomcat 7 and higher.

Setup Grails.

Grails running on Groovy. So we need Groovy to be setup on your machine. (Note: This setup is for Ubuntu)
Groovy can be installed from the Ubuntu repository.

>sudo apt-get install groovy

To test the installation

>groovysh

This should open a python like interactive prompt to test your scripts quickly

>groovyconsole

This will open a GUI based swing tool to test your scripts quickly.

Installing Grails:

The simplest way to install Grails is to install from Ubuntu repositories.

>sudo apt-cache search grails

and get the latest version of Grails. In my demo I have used

> sudo apt-get install grails-1.3.7

But you can’t get the latest version from the repository. You need to do either manual installation or integrate with maven which I will cover on my next blog.
To start a new project simply issue this below command.

 >grails create-app grails-app

The folder structure of the hello world application will be like,

Grails folder Structure

conf:
The conf folder has the configuration files for hibernate and spring if you don’t want to use GORM and override it.
controllers
It holds all the controllers groovy files
domains
All your domain objects which interact with data source goes here
view
The view part of Grail Server Pages (gsp) goes here
services
If you want to expose the app as services all your web services code goes here
i18n
The internationalization support goes here
taglib
Grails support you to develop its own Taglibs as well.
plugins
Grails allow you to develop plugins. There are some you can found it here
src
If you want to develop any components on java or Grails you can create and reuse with Grails.
test
All your test cases for Grails goes here.
webapp
Typical java web application container holds all the js, image, css and container configuration files.

The Hello world:
Lets create a simple controller and run our hello world. Grails have interesting set of command tools which help to speed up the process. you can check their help tool by executing,

 grails help 

Now to create a controller issue,

grails create-controller com.test.hello

This will create HelloController.groovy file inside com.test package with index() file.

package com.test
class HelloController {
    def index() {
         def myname = "hello groovy"         
         render "This is cool ${myname}"
    }
   
}

now run the build-in server to test it out.

> grails run-app

you can see

server running on http://localhost:8080/grails-app

Now on your browser just try

http://localhost:8080/grails-app/hello/index

There you are, you cooked your first fish on Grails.