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

One thought on “A WALK THROUGH OF GRAILS-PART 2- Maven Integration

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s