.connect{ follow ->

}

Grails Part 2 - Configuring Web Application Plugins

Click here to view the full tutorial
Continuing series: Developing a Grails Web Application.
This application will use the Grails Framework to develop a web application using:
1. MongoDB
2. Spring Security
3. Apache CXF
4. Jetty Web Server
5. Maven build integration => Removed upon Request

Previous Post: Grails Part 1 - Setting up a Project on Maven With IntelliJ
View Project on Github: Grails MongoDB Demo

These Instructions Will Walk Through:

  • Configuring the Plugins
    • MongoDB
    • Jetty
    • Spring Security
    • Apache CXF (WSClient)

Configuring the Plugins

Note: The latest version of this demo does not use maven. Maven really isn't the best first choice. To configure without maven, do not add a POM.xml to directory, and add the following definitions to the file at 'grails-app/conf/BuildConfig.groovy'


On with the maven tutorial
When working with grails we don't typically have dependency jars. Instead, Grails uses plugins for it's management of artifacts. These plugins aren't just artifacts, but also are an extension of the Grails framework. Plugins are available for download on their website, and more information on their specs can be found in the documentation.
You may also install plugins with the grails command:
grails install-plugin [plugin-name]
Since we are working with maven, the plugins will be managed by maven. These plugins are available in the Grails remote repository.
This repo must be listed in your list of remote repositories:
<repository>
    <id>grails-plugins</id>
    <name>grails-plugins</name>
    <url>http://repo.grails.org/grails/plugins</url>
</repository>
Grails typically handles it's plugin management in a configuration file "BuildConfig.groovy".
This file can be located at:
../my-app/grails-app/conf/BuildConfig.Groovy
To enable the grails command line to read the POMs, you must modify this file to use Maven when resolving dependencies.
I would suggest removing the plugins property from this script as well.
grails.project.dependency.resolution = {
    
    //Use maven for dependency resolution
    pom true
    repositories {
        
        mavenLocal()    
    }

    //REMOVE if using maven 
    plugins {
        ...
    }
}
back to top

1. MongoDB Plugin

By default, Grails comes pre-packaged with Hibernate. Since we are working with a NoSql data store, we will need to remove that plugin from our maven dependency list.
Go into your pom.xml, and remove the dependency:
    <dependency>
        <groupId>org.grails.plugins</groupId>
        <artifactId>hibernate</artifactId>
        <version>2.1.1</version>
        <scope>runtime</scope>
        <type>zip</type>
    </dependency>
And replace it with:
    <dependency>
        <groupId>org.grails.plugins</groupId>
        <artifactId>mongodb</artifactId>
        <version>1.2.0</version>
        <scope>compile</scope>
        <type>zip</type>
    </dependency>
IMPORTANT!
When replacing hibernate with MongoDB GORM, you need to also:
1. Remove the database-migration dependencey from your POM.
2. To ensure removal, run command:
  1.  grails uninstall-plugin database-migration
  2.  grails uninstall-plugin tomcat
3. Refresh by running: grails refresh-dependencies
4.There are some compatibility issues between some versions of the mongoDB plugin and Grails. If you get any errors running this application, first please consult the mongoDB plugin page.

Now your configured for MongoDB, and data will store in BSON type objects, as opposed to a relational store.

back to top

2. Jetty Plugin

Grails, by default comes with Apache Tomcat as it's web server. To change to Jetty, we need to remove the Tomcat plugin dependency from our pom.xml.
To make Jetty 7.6.0 the development time container for Grails, replace Tomcat plugin dependency with:
    <dependency>
        <groupId>org.grails.plugins</groupId>
        <artifactId>jetty</artifactId>
        <version>2.0.1</version>
        <scope>compile</scope>
        <type>zip</type>
    </dependency>
If your working in IntelliJ Idea, with auto-import turned on, IntelliJ will pull in the dependencies automatically.
back to top

3. Spring Security & Apache CXF Plugins

Add the following Maven dependencies to get Spring Security and Apache CXF support:
    <dependency>
        <groupId>org.grails.plugins</groupId>
        <artifactId>spring-security-core</artifactId>
        <version>1.2.7.3</version>
        <scope>compile</scope>
        <type>zip</type>
    </dependency>
    <dependency>
        <groupId>org.grails.plugins</groupId>
        <artifactId>webxml</artifactId>
        <version>1.4.1</version>
        <scope>compile</scope>
        <type>zip</type>
    </dependency>
The WSClient plugin is based on GroovyWS - which uses CXF
    <dependency>
        <groupId>org.grails.plugins</groupId>
        <artifactId>ws-client</artifactId>
        <version>1.0</version>
        <scope>compile</scope>
        <type>zip</type>
    </dependency>
Finally
1. Stop the current run process in IntelliJ, or if in console: Ctrl-C
2. Run: mvn clean compile (May not be necessary, but ensures a clean refresh)
3. Click Run button in IntelliJ. If your not in IntelliJ, on the command line run: mvn grails:run-app

The application is now up and running on Jetty with MongoDB. It's amazing how simple and rapid getting started with Grails is!

Click here to view the full tutorial