Groovy & Grails


Applicable Plans - All Cloud Hosting Plans

Groovy & Grails

Overview

Groovy is an object-oriented programming language for the Java platform, and Grails is a web application framework that leverages the Groovy language to provide a high-level application development environment.

Groovy & Grails is not a Java application server, but a tool to help you build Java applications. The final result of your work in Groovy & Grails will be a file that you will then deploy on Tomcat, JBoss, or GlassFish.

We recommend that you read and understand the official documentation - http://grails.org/doc/latest/guide/index.html. The documentation for Groovy & Grails is very extensive and detailed, covering all aspects of how to use the framework.

Groovy & Grails also has an active user community that will be able to answer any questions you may have about using the application - http://grails.org/Community

Because it is used for application development, Groovy & Grails is usually installed on a separate development server, not on your production servers. This is partially due to resource requirements - to do any kind of real work in Groovy & Grails, your VS will need to have at least 1024 MB of RAM; and partially due to port conflicts - Groovy & Grails installs its own Tomcat server, which will conflict on port 8080 if you have another Java server such as Tomcat, JBoss, or GlassFish installed.

Prerequisites
Installing Applications

Using Groovy & Grails
Hello World! with Groovy & Grails


Prerequisites

To build Java applications using Groovy & Grails, you will need to install Java SE 6 or Java SE 7 and Groovy and Grails. Make sure to install Java first!

Installing Applications

To install Groovy and Grails and either Java SE 6 or Java SE 7, navigate in ISPmanager to Server Settings > Applications. This screen lists both the installed and available applications.

Click on the application you want to install to highlight it, and then click Install in the upper right corner of the screen. Remember to install Java first, then Groovy and Grails. Java and Groovy and Grails can take up to 5 minutes each to install, due to the size of applications. You may need to refresh the screen to see if they are installed.

Once the applications are installed, they will be listed in the Installed version column.


Using Groovy & Grails

To work with Groovy & Grails, you will need to be able to connect to your Virtual Server using SSH, and be able to work as the root user. You will need to know how to navigate the Linux filesystem, run standard Linux commands, and edit files in a text editor (vim and nano are available by default).

You can also connect to Groovy & Grails using your IDE. The official Groovy & Grails documentation has instructions for connecting with several IDEs here - http://grails.org/doc/latest/guide/gettingStarted.html#ide. If your particular IDE is not listed, please contact the support community for that IDE to see if they can provide any guidance.

Hello World! with Groovy & Grails

This example creates a basic "Hello World!" webpage in Groovy & Grails. This will help you to get some familiarity with the application. This example is taken from the official Groovy & Grails documentation - http://grails.org/doc/latest/guide/gettingStarted.html#creatingAnApplication

Remember that this is only a very basic example, intended to familiarize you with the interface. You will need to read the official documentation for more advanced usage and options.

If you were connected to the VS via SSH when Groovy & Grails was installed, you may need to log out and then back in so that your shell can see the correct path for all the grails commands. If you see this the first time you try to run the grails command, log out and then back in.

[root@eapps-example ~]# grails
-bash: grails: command not found


In order to create the "Hello World!" application, change directories to the grails root directory, which is /opt/grails. Then use the grails create-app helloworld* command to create the application. Note that there are no spaces in "helloworld*"

This starts the Grails Interactive Console, which will display some status messages as the "helloworld" application is created.

[root@eapps-example ~]# cd /opt/grails/
[root@eapps-example grails]# grails create-app helloworld
Created Grails Application at /opt/grails/helloworld

Change directories to the new helloworld directory, and open the interactive console with the grails command.

The very first time you run the grails command, you will be asked if you want to send anonymous usage information to the Grails team. Answer however you wish.

[root@eapps-example grails]# cd helloworld
[root@eapps-example helloworld]# grails
|Downloading: resources-1.1.6.zip
> ##########################################################.
Grails would like to send information to VMware domains to improve your experience. We include anonymous usage information as part of these downloads.

The Grails team gathers anonymous usage information to improve your Grails experience, not for marketing purposes. The information is used to discover which Grails plugins are most popular and is published on the plugin portal.

We also use this information to help guide our roadmap, prioritizing the features and Grails plugins most valued by the community and enabling us to optimize the compatibility of technologies frequently used together.

Please see the Grails User Agent Analysis (UAA) Terms of Use at http://www.springsource.org/uaa/terms_of_use for more information on what information is collected and how such information is used. There is also an FAQ at http://www.springsource.org/uaa/faq for your convenience.

To consent to the Terms of Use, please enter ‘Y’. Enter ‘N’ to indicate your do not consent and anonymous data collection will remain disabled.
##########################################################.
Enter Y or N:[y,n] n
| Enter a script name to run. Use TAB for completion:
grails>


The next thing you need to do is create the controller to display "Hello World!" in the browser. Do this with the create-controller hello command.

grails> create-controller hello
| Compiling 38 source files

| Created file grails-app/controllers/helloworld/HelloController.groovy
| Created file grails-app/views/hello
| Created file test/unit/helloworld/HelloControllerTests.groovy
grails>

This will create the controller in the /opt/grails/helloworld/grails-app/controllers/helloworld directory called HelloController.groovy.

Exit the interactive console with the exit command, and then change directories to the correct location. Then edit the HelloController.groovy file (this example uses vim, nano is also available).

grails> exit
[root@eapps-example ~]# pwd
/opt/grails/helloworld
[root@eapps-example helloworld]# cd grails-app/controllers/helloworld
[root@eapps-example helloworld]# vim HelloController.groovy

This is the default file:

package helloworld

class HelloController {

    def index() { }
}

Make the following change to the file - the render "Hello World!" between the curly braces after def index()

package helloworld

class HelloController {

    def index() {
       render "Hello World!"
  }
}

Once you have made your changes, save and exit the file.

Once you have made your change, change directories back to the project directory at /opt/grails/helloworld, and enter the interactive console with the grails command. You must change directories back to the project directory! If you don't, then the next steps will not work correctly.

[root@eapps-example helloworld]# cd /opt/grails/helloworld
[root@eapps-example helloworld]# grails
| Enter a script name to run. Use TAB for completion:
grails>

At the grails prompt, enter the run-app command. This will start the built-in web server and compile your application.

grails> run-app
| Compiling 38 source files

| Server running. Browse to http://localhost:8080/helloworld
| Application loaded in interactive mode. Type 'exit' to shutdown.
| Enter a script name to run. Use TAB for completion:
grails>


In your browser, go to http://eapps-example.com:8080/helloworld (substitute your domain name or IP address for eapps-example.com).

This is what you will see in the browser:

Hello World - Main page


Click on the link for helloworld.HelloController under Available Controllers to see the "Hello World!" message.

Hello World!


When you're done, type exit at the prompt to stop the Grails server.

This completes the "Hello World" example. For more information on how to use Groovy & Grails, please read the official documentation.



Comments

Please login to comment