Grails and Java
Grails Framework based on JAVA Platform. Grails is a framework used to build web applications with the Groovy programming language. The core framework is very extensible and there are numerous plugins available that provide easy integration of add-on features.
We are using latest version of Grails that is Grails 3.3.0, for Java we are using JDK 8.144 and for Maven we are using 3.5.0 as our configuration.
Before installing Grails 3.3.0 , make sure that Java SDK 1.8.121 or higher and Maven 3.5.0 is installed on your machine. Set environmental variable to point to the path where you have installed Java and Maven.
- Download the latest version of prerequisites:
- Extract the download file in appropriate location.
- Create a environment variable for each application .i.e.
- Grails: GRAILS_HOME
- Java: JAVA_HOME
- Maven: MAVEN_HOME
- In the PATH environment variable, point it to the /bin directory of each application i.e %VARIABLE_NAME%\bin
- After that, check that the files are installed in your system through Command Prompt using commands as: “Java -version” for java, “grails -version” for grails and “mvn -version” for maven.
To setup your new application on grails, you should first have basic understanding of terminologies like domain classes, controllers, services, GSP’s, and unit & integration tests.
Creating a Grails app is about as simple as could be – once you have installed Grails, make a default directory where the project is to be stored and simply run the command prompt on the directory and use command:
$ grails create-app mySampleApp
Now you have created your sample application, you can run and check what functionality have grails given to you, but before you run the application change you directory to the new directory which was auto generated by the system.
$ cd mySampleApp
Now after that, run the application
$ grails run-app
You can also use the grails interactive mode to run the grails runtime from which you can issue any Grails command without waiting for the runtime to spin up for each task.
After running any of the above commands, Grails will launch your application using an embedded Tomcat server, and make it available (by default) at
Configuration in Grails is generally split into 2 areas: build configuration and runtime configuration.
Build configuration is generally done via Gradle and the build.gradle file. Runtime configuration is by default specified in YAML in the grails-app/conf/application.yml file.
For build configuration open the build.gradle file in any text editor installed in you machine and add
to your dependencies, under the heading
this will connect the grails with the MySQL database in runtime environment to store the data entered during development phase.
And for runtime configuration open the application.yml file from above mention directory and perform the following task:
- Create a MySQL database under the convenient heading.
- Change the driverClassName in DataSource, from
- Change the Environment field of Development phase, from
This changes helps to permanently store the data in MySQL database entered during the development phase which otherwise is erased each time the application is terminated.
- Also change the username and password field in dataSource as per the username and password set in your MySQL i.e.
Create Domain Class
A domain class fulfills the M in the Model View Controller (MVC) pattern and represents a persistent entity that is mapped onto an underlying database table. In Grails a domain is a class that lives in the grails-app/domain directory.
The class name, by default, is mapped to the table name in lower case and separated by underscores instead of camel case.
$ grails create-domain-class <<name>>
Grails supports a feature known as static scaffolding which generates a CRUD (Create/Read/Update/Delete) interface for a given domain class. Once generated, the controller and its views can be modified by you but they won’t automatically update when you change the domain class.
$ grails generate-controller <<domain.class.name>>
As like generate-controller function Grails also helps to generate the view function which generates just the GSP views that implement CRUD for the given domain class.
$ grails generate-view <<domain.class.name>>
As like above two generate function, this function helps to generate both the controller and view of the given domain class. The generate-all command generates an implementation of CRUD including a controller and views for the given domain class
$ grails generate-all <<domain.class.name>>
Creating WAR file
A common approach to Grails application deployment in production is to deploy to an existing Servlet container via a WAR file. Containers allow multiple applications to be deployed on the same port with different paths.
This will produce a WAR file that can be deployed to a container, in the build/libs directory.
Creating a WAR file is as simple as executing the war command:
$ grails war
Note: You now have a basic framework to start a new grails application project. This is the power of Grails. It leverages the coding by convention paradigm to give you access to robust and mature Java open source frameworks such as Spring and Hibernate.
Now that you understand the basics of Grails, be on the lookout for the follow-up articles in future.