Welcome to this beginner’s tutorial on getting started with Grails to create your first Grails application! In this tutorial, you will learn about different ways to create a Grails application, including using the Grails command-line interface and the Grails web-based interface, and running your first application. Along the way, we’ll cover important topics such as understanding Grails directory structure, recommended and optional folders and files in a Grails application, and more. Whether you are new to Grails or just need a quick refresher, this tutorial will provide an overview of the basic steps required to get up and running with a Grails application. By the end of this tutorial, you’ll have a solid understanding of how to create and run a Grails application, and you’ll be ready to start building your own Grails projects. Let’s get started!
Grails is an open-source web application framework that uses the Groovy programming language and runs on the Java Virtual Machine (JVM). It simplifies web application development by offering a high-productivity, convention-over-configuration approach. Grails integrates seamlessly with popular JVM technologies, making it an ideal choice for developers. If you’re new to Grails, this tutorial will help you know the introduction to grails. To learn more about Grails, check out Introduction to Grails Framework: Tutorial for Beginners in-depth article.
Getting Started with Grails:
Installation
Before you can start building Grails applications, you’ll need to install the required software, including Java Development Kit (JDK), Grails, and an Integrated Development Environment (IDE) of your choice. To learn more about grails installation, please refer to our detailed installation guide here.
Creating and running a New Grails Application
There are several ways to create a new Grails application. In this tutorial, we’ll cover popular methods:
Using command-line interface:
Please follow below simple steps to create a new Grails application using the create-app
command:
- Open your terminal or command prompt.
- To verify that Grails has been installed correctly, type the command
grails -version
and press enter. You should see the version number of your Grails installation printed to the console. - Create a new Grails application by typing the command
grails create-app myapp
and press enter. This will create a new Grails application with the name “myapp” in a directory with the same name. For first creation, might be it will take some time in minutes. - Navigate to the directory where your new Grails application was created by typing the command cd myapp and press enter.
- To verify that the application was created correctly, type the command
grails run-app
and press enter. This will start the Grails development server and launch your application. - Wait for the application to start up. You should see some log messages in your command prompt/terminal indicating that the server has started.
- Once the server has started, open a web browser, and go to “
http://localhost:8080
” to see your application running. You should see the default Grails welcome page. - From here, you can start coding your application by editing the files in your project directory using your preferred text editor.
- Whenever you make changes to your code, you can refresh your browser to see the updated version of your application.
That’s it! With these steps, you can quickly create and run a new Grails application using the create-app command.
Note: If the default port number 8080 is not available or is already being used by another application, you can run the Grails development server on a different port. To do this, you need to specify the port number when starting the server. For example, if you want to use port number 8081, you can start the server using the command
grails run-app -port=8081
orgrails run-app --port=8081
. Then, you can access your Grails application in the web browser by going to “http://localhost:8081
“
Using Grails web-based interface:
Grails Application Forge is a web-based tool is a web-based tool that allows developers to quickly create and configure a new Grails project easily.
Please follow the detailed steps:
- Open your web browser and go to “https://start.grails.org/“.
- On the home page, you will see a form to create a new Grails application. Choose the type of project you want to create by clicking on one of the options, such as “Application” or “Plugin“.
- Enter a name for your project in the “Project Name” field. This can be any name you like, such as “myapp“.
- Choose the Grails version you want to use by clicking on the “Select” button next to the version number. You can choose the latest version or a specific version.
- Choose a profile for your application by clicking on the “Select” button next to the profile name. A profile is a set of configuration settings and plugins that modify the behavior of your Grails application. You can select from a variety of profiles, such as “web“, “rest-api“, “angular“, “react“, and “vue“.
- Select the features you want to include in your application by clicking on the checkboxes next to the feature names. These can include features such as Hibernate, GORM, and Spring Security.
- Click on the “Generate” button at the bottom of the form. This will create a new Grails application based on the settings you have chosen.
- Wait for the application to be generated. This may take a few minutes, depending on the size of your application and the speed of your internet connection.
- Once the application is generated, you will see a download button. Click on the “Download” button to download a ZIP file containing your new Grails application.
- Extract the ZIP file to a directory on your local machine. You can do this by double-clicking on the file in your file manager or using a command-line tool such as “unzip”.
- If you have an IDE installed, open the project in your preferred IDE. If not, open your command prompt or terminal and navigate to the directory where you extracted the ZIP file for your Grails application.
- If you are using an IDE, start the application by clicking on the “Run” or “Debug” button in your IDE. This will start the Grails development server and launch your application.
- If you are using the command prompt or terminal, type the command “
grails run-app
” and press enter. This will start the Grails development server and launch your application. - Wait for the application to start up. You should see some log messages in your IDE or command prompt/terminal indicating that the server has started.
- Once the server has started, open the web browser, and go to “
http://localhost:8080
” to see your application running. You should see the default Grails welcome page. - From here, you can start coding your application by editing the files in your project directory using your preferred text editor.
- Whenever you make changes to your code, you can refresh your browser to see the updated version of your application.
That’s it! With these steps, you can quickly create and run a new Grails application using “https://start.grails.org/
“, with or without an IDE.
Note: You can even download your project from the command line using a HTTP tool like curl (see start.grails.org for API documentation): curl -O start.grails.org/myapp.zip -d version=5.2.3
Using IntelliJ IDEA:
Below is step-by-step tutorial to creating a Grails application from IntelliJ IDEA assuming that you have already set up your Grails and Java environments:
- Open IntelliJ IDEA and select “Create New Project” from the welcome screen.
- In the “New Project” dialog, select “Grails” from the list of available project types on the left side of the screen.
- Choose the Grails SDK that you have installed by clicking on the “New” button in the “Grails SDK” section, selecting the installation directory of your Grails SDK, and clicking “OK“. If you have already set up a Grails SDK in IntelliJ IDEA, select it from the list.
- Choose the project SDK that you have installed by clicking on the “New” button in the “Project SDK” section, selecting the installation directory of your Java SDK, and clicking “OK“. If you have already set up a Java SDK in IntelliJ IDEA, select it from the list.
- In the “Project Name” field, enter a name for your new Grails application.
- Choose the location where you want to save your project files by clicking on the “Project location” field and navigating to the desired directory.
- Click on the “Create” button to create your new Grails project.
- Wait for IntelliJ IDEA to create your new project. This may take a few moments, depending on the size of your project.
- Once the project has been created, you can start coding your Grails application by editing the files in your project directory using the IntelliJ IDEA code editor.
- To run your Grails application, click on the “Run” button in the top toolbar of IntelliJ IDEA or right-click on the project name in the Project tool window and select “Run ‘myapp‘” (replace “myapp” with the name of your project).
- Wait for the application to start up. You should see some log messages in the Run tool window indicating that the server has started.
- Once the server has started, open a web browser and go to “
http://localhost:8080
” to see your application running. You should see a default Grails welcome page. - From here, you can continue coding your application by editing the files in your project directory using your preferred text editor.
That’s it! With these steps, you can quickly create and run a new Grails application from IntelliJ IDEA assuming that you have already set up your Grails and Java environments.
Understanding Grails Application Directory Structure
Grails applications follow a well-defined directory structure, which helps keep your project organized and maintainable. The structure consists of several top-level folders, each with a specific purpose and set of subdirectories.
A typical Grails application directory structure looks like this:
myapp ├───gradle │ └───wrapper ├───grails-app │ ├───assets │ │ ├───images │ │ ├───javascripts │ │ └───stylesheets │ ├───conf │ ├───controllers │ │ └───myapp │ ├───domain │ ├───i18n │ ├───init │ │ └───myapp │ ├───services │ ├───taglib │ ├───utils │ └───views │ └───layouts └───src ├───integration-test │ ├───groovy │ └───resources ├───main │ ├───groovy │ └───webapp └───test
Understanding the use if each directory:
gradle: This directory contains configuration files for the Gradle build system, which is used to build the Grails application. The wrapper subdirectory contains scripts and configuration files for the Gradle wrapper, which allows Gradle to be executed without a local installation.
grails-app: This directory contains the main source code and resources for the Grails application. It includes the following subdirectories:
assets: This subdirectory contains static assets such as images, JavaScript files, and stylesheets used by the application.
images: This subdirectory contains images used by the application.
javascripts: This subdirectory contains JavaScript files used by the application.
stylesheets: This subdirectory contains stylesheets used by the application.
conf: This subdirectory contains configuration files for the Grails application.
controllers: This subdirectory contains controllers, which are responsible for handling HTTP requests and returning responses.
myapp: This subdirectory contains controllers specific to the myapp package.
domain: This subdirectory contains domain classes, which represent the data model of the application.
i18n: This subdirectory contains internationalization (i18n) resources, such as message bundles for different languages.
init: This subdirectory contains initialization scripts for the application.
services: This subdirectory contains service classes, which provide business logic for the application.
taglib: This subdirectory contains tag libraries, which are used to define custom tags for use in views.
utils: This subdirectory contains utility classes, which provide reusable functionality for the application.
views: This subdirectory contains views, which are responsible for rendering the HTML output of the application.
layouts: This subdirectory contains layout templates, which are used to define the overall structure of HTML pages.
src: This directory contains the main source code and resources for the Grails application, organized into three subdirectories:
integration-test: This subdirectory contains integration tests, which test the integration between different components of the application.
groovy: This subdirectory contains Groovy source code for the integration tests.
resources: This subdirectory contains resources used by the integration tests, such as test data.
main: This subdirectory contains the main source code and resources for the Grails application.
groovy: This subdirectory contains Groovy source code for the application.
webapp: This subdirectory contains web resources used by the application, such as HTML files and static assets.
test: This subdirectory contains unit tests, which test individual components of the application.
groovy: This subdirectory contains Groovy source code for the unit tests.
To keep your Grails project organized and easy to maintain, it’s important to follow some best practices:
- Follow the convention-over-configuration approach provided by Grails, which means relying on sensible defaults and conventions rather than explicit configuration wherever possible.
- Use a consistent naming convention for your classes, packages, and files, to make it easy to understand the purpose of each component and locate them in your project.
- Keep related components close together, such as placing views and controllers in their respective folders. This helps to avoid clutter and makes it easy to find the code you need.
- Use Grails plugins to modularize common functionality, such as authentication or database access, to avoid duplication of effort and make it easier to maintain and update your code.
- Regularly review and refactor your project structure to maintain organization, especially as your project grows and evolves over time. This can help you avoid technical debt and make it easier to manage your codebase.
Conclusion
In this tutorial, we have covered the basics of getting started with Grails. We learned how to create a new Grails application, run the application, understand the directory structure of a Grails application, and the benefits of keeping projects organized and maintainable. Grails is a powerful and flexible web application framework that can help you build robust and scalable applications quickly. We hope this tutorial has provided you with a solid foundation to start building your own Grails applications. To learn more about Grails, we recommend reading our other Grails articles, which provide step-by-step tutorials for learning the Grails framework.
No comment