Skip to main content

Getting Started with Grails on GlassFish

Posted by arungupta on April 11, 2008 at 9:07 AM PDT



This blog has published href="http://blogs.sun.com/arungupta/tags/rubyonrails+glassfish">good
amount of content on how Ruby-on-Rails
applications can be deployed on GlassFish.
Grails is another
popular MVC-based framework that leverages the href="http://groovy.codehaus.org/">Groovy
language. Basically, Grails : Groovy :: Rails : Ruby.



Starting today, I plan to start publishing content on Grails and talk
about how href="http://wiki.glassfish.java.net/Wiki.jsp?page=PlanForGlassFishV3">GlassFish
v3 is turning out to be a home for several scripting
languages - Ruby/JRuby/Rails, JavaScript/ href="http://phobos.dev.java.net">Phobos,
Groovy/Grails and more to be added.



This blog shows how to get started with Grails and deploy a simple
application on GlassFish v3. Lets go!

  1. Download href="http://dist.codehaus.org/grails/grails-bin-1.0.2.zip">Grails
    1.0.2 (docs,
    release
    notes
    , other
    builds
    ) and unzip.


    cellpadding="2" cellspacing="2">
    ~/testbed/grails-1.0.2
    >export
    GRAILS_HOME=`pwd`


    ~/testbed/grails-1.0.2 >export
    PATH=$GRAILS_HOME/bin:$PATH



    On MacOS 10.5, I had to manually set the executable permissions using
    "chmod +x grails" and then saw the following:


    cellpadding="2" cellspacing="2">
    ~/testbed/grails-1.0.2
    >grails



    Welcome to Grails 1.0.2 - http://grails.org/

    Licensed under Apache Standard License 2.0

    Grails home is set to: /Users/arungupta/testbed/grails-1.0.2



    No script name specified. Use 'grails help' for more info


  2. Create & Deploy a Grails HelloWorld:
    1. Create a Grails app "helloworld" as:


      cellpadding="2" cellspacing="2">
      style="font-weight: bold;">~/testbed/grails-1.0.2/samples
      >grails create-app
      helloworld




      Welcome to Grails 1.0.2 - http://grails.org/

      Licensed under Apache Standard License 2.0

      Grails home is set to: /Users/arungupta/testbed/grails-1.0.2



      Base Directory: /Users/arungupta/testbed/grails-1.0.2/samples

      Note: No plugin scripts found

      Running script
      /Users/arungupta/testbed/grails-1.0.2/scripts/CreateApp.groovy

      Environment set to development

          [mkdir] Created dir:
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/src

          [mkdir] Created dir:
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/src/java

          [mkdir] Created dir:
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/src/groovy

          [mkdir] Created dir:
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/grails-app

          [mkdir] Created dir:
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/grails-app/controllers

          [mkdir] Created dir:
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/grails-app/services

          [mkdir] Created dir:
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/grails-app/domain

          [mkdir] Created dir:
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/grails-app/taglib

          [mkdir] Created dir:
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/grails-app/utils

          [mkdir] Created dir:
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/grails-app/views

          [mkdir] Created dir:
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/grails-app/views/layouts

          [mkdir] Created dir:
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/grails-app/i18n

          [mkdir] Created dir:
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/grails-app/conf

          [mkdir] Created dir:
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/test

          [mkdir] Created dir:
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/test/unit

          [mkdir] Created dir:
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/test/integration

          [mkdir] Created dir:
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/scripts

          [mkdir] Created dir:
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/web-app

          [mkdir] Created dir:
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/web-app/js

          [mkdir] Created dir:
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/web-app/css

          [mkdir] Created dir:
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/web-app/images

          [mkdir] Created dir:
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/web-app/META-INF

          [mkdir] Created dir:
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/lib

          [mkdir] Created dir:
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/grails-app/conf/spring

          [mkdir] Created dir:
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/grails-app/conf/hibernate

      [propertyfile] Creating new property file:
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/application.properties

           [copy] Copying 2 files to
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld

           [copy] Copying 2 files to
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/web-app/WEB-INF

           [copy] Copying 5 files to
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/web-app/WEB-INF/tld

           [copy] Copying 87 files to
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/web-app

           [copy] Copying 17 files to
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/grails-app

           [copy] Copying 1 file to
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld

           [copy] Copying 1 file to
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld

           [copy] Copying 1 file to
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld

      [propertyfile] Updating property file:
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/application.properties

      Created Grails Application at
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld



      The top-level directory structure is:


      cellpadding="2" cellspacing="2">
      total
      56

      drwxr-xr-x  14 arungupta 
      arungupta   476 Apr 10 10:44 .

      drwxr-xr-x   3 arungupta 
      arungupta   102 Apr 10 10:44 ..

      -rw-r--r--   1 arungupta 
      arungupta  5911 Apr 10 10:44 .classpath

      -rw-r--r--   1 arungupta 
      arungupta   553 Apr 10 10:44 .project

      -rw-r--r--   1 arungupta 
      arungupta   115 Apr 10 10:44 application.properties

      -rw-r--r--   1 arungupta 
      arungupta  1464 Apr 10 10:44 build.xml

      drwxr-xr-x  10 arungupta 
      arungupta   340 Apr 10 10:44 grails-app

      -rw-r--r--   1 arungupta 
      arungupta  1805 Apr 10 10:44 helloworld.launch

      -rw-r--r--   1 arungupta 
      arungupta  2287 Apr 10 10:44 helloworld.tmproj

      drwxr-xr-x   2 arungupta 
      arungupta    68 Apr 10 10:44 lib

      drwxr-xr-x   2 arungupta 
      arungupta    68 Apr 10 10:44 scripts

      drwxr-xr-x   4 arungupta 
      arungupta   136 Apr 10 10:44 src

      drwxr-xr-x   4 arungupta 
      arungupta   136 Apr 10 10:44 test

      drwxr-xr-x   8 arungupta 
      arungupta   272 Apr 10 10:44 web-app



      This directory is created using href="http://en.wikipedia.org/wiki/Convention_over_Configuration">Convention
      over Configuation and the purpose of each directory is
      explained href="http://grails.org/doc/1.0.x/guide/2.%20Getting%20Started.html#2.5%20Convention%20over%20Configuration">here.

    2. The app can be started as:


      cellpadding="2" cellspacing="2">
      ~/testbed/grails-1.0.2/samples/helloworld
      >grails run-app



      Welcome to Grails 1.0.2 - http://grails.org/

      Licensed under Apache Standard License 2.0

      Grails home is set to: /Users/arungupta/testbed/grails-1.0.2



      Base Directory: /Users/arungupta/testbed/grails-1.0.2/samples/helloworld

      Note: No plugin scripts found

      Running script
      /Users/arungupta/testbed/grails-1.0.2/scripts/RunApp.groovy

      Environment set to development

          [mkdir] Created dir:
      /Users/arungupta/.grails/1.0.2/projects/helloworld/classes

        [groovyc] Compiling 6 source files to
      /Users/arungupta/.grails/1.0.2/projects/helloworld/classes

          [mkdir] Created dir:
      /Users/arungupta/.grails/1.0.2/projects/helloworld/resources/grails-app/i18n

      [native2ascii] Converting 10 files from
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/grails-app/i18n
      to
      /Users/arungupta/.grails/1.0.2/projects/helloworld/resources/grails-app/i18n

           [copy] Copying 1 file to
      /Users/arungupta/.grails/1.0.2/projects/helloworld/classes

           [copy] Copying 1 file to
      /Users/arungupta/.grails/1.0.2/projects/helloworld/resources

           [copy] Copying 1 file to
      /Users/arungupta/.grails/1.0.2/projects/helloworld

      Running Grails application..

      2008-04-10 10:47:51.501::INFO:  Logging to STDERR via
      org.mortbay.log.StdErrLog

      2008-04-10 10:47:51.603::INFO:  style="font-weight: bold;">jetty-6.1.4

      2008-04-10 10:47:51.755::INFO:  No Transaction manager found -
      if your webapp requires one, please configure one.

      2008-04-10 10:47:52.503:/helloworld:INFO:  Set web app root
      system property: 'helloworld' =
      [/Users/arungupta/testbed/grails-1.0.2/samples/helloworld/web-app/]

      2008-04-10 10:47:52.503:/helloworld:INFO:  Initializing Log4J
      from
      [file:/Users/arungupta/.grails/1.0.2/projects/helloworld/resources/log4j.properties]

      2008-04-10 10:47:52.582:/helloworld:INFO:  Initializing Spring
      root WebApplicationContext

      [0] spring.GrailsWebApplicationContext Refreshing
      org.codehaus.groovy.grails.commons.spring.GrailsWebApplicationContext@dccb42:
      display name
      [org.codehaus.groovy.grails.commons.spring.GrailsWebApplicationContext@dccb42];
      startup date [Thu Apr 10 10:47:53 PDT 2008]; parent:
      org.springframework.web.context.support.XmlWebApplicationContext@188af2

      [0] spring.GrailsWebApplicationContext Bean factory for application
      context
      [org.codehaus.groovy.grails.commons.spring.GrailsWebApplicationContext@dccb42]:
      org.springframework.beans.factory.support.DefaultListableBeanFactory@413ebc

      2008-04-10 10:47:55.602:/helloworld:INFO:  Initializing Spring
      FrameworkServlet 'grails'

      2008-04-10 10:47:55.652::INFO:  Started
      SelectChannelConnector@0.0.0.0:8080

      Server running. Browse to http://localhost:8080/helloworld style="font-weight: bold;">
      2008-04-10 10:48:15.133:/helloworld:INFO:  GSP servlet
      initialized



      The output in the browser looks like:



      src="http://blogs.sun.com/arungupta/resource/images/grails-v3-helloworld-on-jetty.png">

      Jetty is used as the development environment server but soon href="http://wiki.glassfish.java.net/Wiki.jsp?page=PlanForGlassFishV3">GlassFish
      v3 can be used, stay tuned on that!



      Anyway, the WAR format is used for deployment. Lets see how
      this app can be easily deployed as a WAR file on GlassFish
      v3.

  3. Download href="https://glassfish.dev.java.net/downloads/v3-techPreview-2.html">GlassFish
    v3 Preview 2 b08 and unzip.
  4. Create and Deploy WAR
    1. GlassFish certainly supports WAR-based deployment
      of Grails apps but direct deployment (aka native or directory-based
      deployment) of Grails applications will
      be supported soon as well. Anyway, for now, create a WAR file of the
      application as:


      cellpadding="2" cellspacing="2">
      ~/testbed/grails-1.0.2/samples/helloworld
      >grails war



      Welcome to Grails 1.0.2 - http://grails.org/

      Licensed under Apache Standard License 2.0

      Grails home is set to: /Users/arungupta/testbed/grails-1.0.2



      Base Directory: /Users/arungupta/testbed/grails-1.0.2/samples/helloworld

      Note: No plugin scripts found

      Running script /Users/arungupta/testbed/grails-1.0.2/scripts/War.groovy

      Environment set to production

         [delete] Deleting:
      /Users/arungupta/.grails/1.0.2/projects/helloworld/resources/web.xml

         [delete] Deleting directory
      /Users/arungupta/.grails/1.0.2/projects/helloworld/classes

         [delete] Deleting directory
      /Users/arungupta/.grails/1.0.2/projects/helloworld/resources

          [mkdir] Created dir:
      /Users/arungupta/.grails/1.0.2/projects/helloworld/classes

        [groovyc] Compiling 6 source files to
      /Users/arungupta/.grails/1.0.2/projects/helloworld/classes

          [mkdir] Created dir:
      /Users/arungupta/.grails/1.0.2/projects/helloworld/resources/grails-app/i18n

      [native2ascii] Converting 10 files from
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/grails-app/i18n
      to
      /Users/arungupta/.grails/1.0.2/projects/helloworld/resources/grails-app/i18n

           [copy] Copying 1 file to
      /Users/arungupta/.grails/1.0.2/projects/helloworld/classes

           [copy] Copying 1 file to
      /Users/arungupta/.grails/1.0.2/projects/helloworld/resources

          [mkdir] Created dir:
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/staging

           [copy] Copying 93 files to
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/staging

           [copy] Copied 19 empty
      directories to 1 empty directory under
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/staging

           [copy] Copying 12 files to
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/staging/WEB-INF/grails-app

           [copy] Copying 26 files to
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/staging/WEB-INF/classes

          [mkdir] Created dir:
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/staging/WEB-INF/spring

           [copy] Copying 1 file to
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/staging/WEB-INF/classes

          [mkdir] Created dir:
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/staging/WEB-INF/templates/scaffolding

           [copy] Copying 6 files to
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/staging/WEB-INF/templates/scaffolding

           [copy] Copying 49 files to
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/staging/WEB-INF/lib

           [copy] Copying 1 file to
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/staging/WEB-INF

         [delete] Deleting:
      /Users/arungupta/.grails/1.0.2/projects/helloworld/resources/web.xml

           [copy] Warning:
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/plugins not
      found.

      [propertyfile] Updating property file:
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/staging/WEB-INF/classes/application.properties

          [mkdir] Created dir:
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/staging/WEB-INF/plugins

           [copy] Warning:
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/plugins not
      found.

            [jar] Building jar:
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/helloworld-0.1.war

         [delete] Deleting directory
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/staging

      Done creating WAR
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/helloworld-0.1.war



      The create WAR file is 18,012,069
      bytes
      .

    2. Start the GlassFish container as:


      cellpadding="2" cellspacing="2">
      ~/testbed/glassfish/v3/p2-b08/glassfish
      >bin/startserv
      --verbose


      Apr 10, 2008 11:03:28 AM
      com.sun.enterprise.admin.launcher.GFLauncherLogger info

      INFO: JVM invocation command line:

      /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home/bin/java

      -cp

      /Users/arungupta/testbed/glassfish/v3/p2-b08/glassfish/modules/glassfish-10.0-SNAPSHOT.jar

      . . .

      INFO: Listening on port 8080

      Apr 10, 2008 11:03:29 AM
      com.sun.enterprise.v3.services.impl.GrizzlyEmbeddedHttpConfigurator
      configureSSL

      WARNING: HTTP listener on port: 8181 is secured, but SSL configuration
      is not found!

      Apr 10, 2008 11:03:29 AM
      com.sun.enterprise.v3.services.impl.GrizzlyProxy start

      INFO: Listening on port 8181

      Apr 10, 2008 11:03:29 AM
      com.sun.enterprise.v3.services.impl.GrizzlyProxy start

      INFO: Listening on port 4848

      Apr 10, 2008 11:03:29 AM
      com.sun.enterprise.v3.admin.adapter.AdminConsoleAdapter setContextRoot

      INFO: Admin Console Adapter: context root: /admin

      Apr 10, 2008 11:03:29 AM com.sun.enterprise.v3.server.AppServerStartup
      run

      INFO: com.sun.enterprise.v3.services.impl.GrizzlyService@b24c9a startup
      done in 681 ms

      Apr 10, 2008 11:03:29 AM
      com.sun.enterprise.v3.services.impl.ApplicationLoaderService
      postConstruct

      INFO: loader service postConstruct started at 1207850609785

      Apr 10, 2008 11:03:29 AM com.sun.enterprise.v3.server.AppServerStartup
      run

      INFO: Application Loader startup done in 717 ms

      Apr 10, 2008 11:03:29 AM com.sun.enterprise.v3.server.AppServerStartup
      run

      INFO: Glassfish v3
      started in 717 ms


      Apr 10, 2008 11:03:29 AM
      com.sun.enterprise.v3.admin.adapter.AdminConsoleAdapter ready

      INFO: AdminConsoleAdapter is ready ...
    3. Deploy the WAR file as:


      cellpadding="2" cellspacing="2">
      ~/testbed/grails-1.0.2/samples/helloworld
      >~/testbed/glassfish/v3/p2-b08/glassfish/bin/asadmin
      deploy helloworld-0.1.war


      helloworld-0.1 deployed successfully

      properties=(name=helloworld-0.1)

      Command deploy executed successfully.



      The output in the browser now looks like:



      src="http://blogs.sun.com/arungupta/resource/images/grails-v3-helloworld-on-glassfish.png">

  5. Add Controller and Re-deploy
    1. Create a new controller as:


      cellpadding="2" cellspacing="2">
      C~/testbed/grails-1.0.2/samples/helloworld
      >grails
      create-controller hello




      Welcome to Grails 1.0.2 - http://grails.org/

      Licensed under Apache Standard License 2.0

      Grails home is set to: /Users/arungupta/testbed/grails-1.0.2



      Base Directory: /Users/arungupta/testbed/grails-1.0.2/samples/helloworld

      Note: No plugin scripts found

      Running script
      /Users/arungupta/testbed/grails-1.0.2/scripts/CreateController.groovy

      Environment set to development

           [copy] Copying 1 file to
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/grails-app/controllers

      Created Controller for Hello

          [mkdir] Created dir:
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/grails-app/views/hello

           [copy] Copying 1 file to
      /Users/arungupta/testbed/grails-1.0.2/samples/helloworld/test/integration

      Created ControllerTests for Hello



      and change the controller as shown:


      cellpadding="2" cellspacing="2">
      ~/testbed/grails-1.0.2/samples/helloworld
      >vi
      grails-app/controllers/HelloController.groovy




      class HelloController {



             def world = {

                     
      render "Hello World!"

              }

          //def index = { }

      }
    2. Re-create & re-deploy the WAR as described in
      bullet # 4. And now
      "http://localhost:8080/helloworld-0.1" shows the output as shown:



      src="http://blogs.sun.com/arungupta/resource/images/grails-v3-helloworld-on-glassfish-with-controller.png">



      and clicking on "HelloController" shows the output as:



      src="http://blogs.sun.com/arungupta/resource/images/grails-v3-helloworld-on-glassfish-controller-output.png">



      The same view is also available at
      "http://localhost:8080/helloworld-0.1/hello/world" as shown below:



      src="http://blogs.sun.com/arungupta/resource/images/grails-v3-helloworld-on-glassfish-complete-url.png">

So we deployed a simple Grails application as a WAR on GlassFish v3.
Here are some tasks that we are working on:

  • Deploy Grails app on GlassFish v3 using "grails" command
    (in development environment)
  • Deploy Grails app directly on GlassFish v3 without creating
    a WAR (in production environment)
  • Seamless integration with NetBeans
    to provide direct
    development/deployment to GlassFish

And there is much more, stay tuned!



If you have questions, ask them at href="http://forums.java.net/jive/forum.jspa?forumID=56&start=0">GlassFish
forum or Users
Alias
.



Technorati: groovy
grails
glassfish
v3 href="http://technorati.com/tag/scripting">scripting
netbeans

Related Topics >>