I started learning grails framework for backend development and thought i will document what i go through during this learning process. I am primarily a C#.NET developer and love to explore unrelated and interesting technologies.
Note that i am not writing this for someone who want to learn grails. This is for someone like me who has a basic understanding of Java, IntelliJ, gradle conceptually and trying to leverage that knowledge to learn grails.
I will be writing all the things that you would see different with grails than with a regular web application framework, starting with installation till writing a full blown web api. So let's get started. If you are a ruby on rails programmer this post will be a skim through for you to just grasp the differences and learn it even faster.
- Install JDK. In general for any version of grails 1.8 should work without any issues.
- To install grails, the recommended method is sdkman. But it's not that straightforward in windows. There are multiple ways to workaround it and the one i found useful is
- Install Git for windows
- Install Gow. Gow hosts some of the tools that are need by sdkman and not shipped by default with Git for windows.
- Add Gow bin path to environment variables in
git bash. To do that open git bash and edit the
~/.bashrcfile and add the path of \bin.
- I am interested in installing version 3.3.11 and here is how to do it
sdk install grails 3.3.11. The
sdkexecutable is installed as part of the sdkman installation.
- Once the installation is complete close the command prompt and open it again so the part variables are set correctly and grails is ready to use.
- Download and install IntelliJ IDEA Community
Grails projects cannot be created from an IDE, since there are many dependencies to a basic grails project, and there isn't a free version of IDE that can scaffold generate the project. The advised way is to scaffold the code using the grails command line and when done, open the generated .gradle file in the IDE. I use Intellij IDEA Community to work with, because it supports Java, groovy, gradle toolset natively and works out of the box.
Open git bash.
archa@DESKTOP-EKEC9PU MINGW64 ~/ $ mkdir work archa@DESKTOP-EKEC9PU MINGW64 ~/ $ cd work archa@DESKTOP-EKEC9PU MINGW64 ~/work $ grails grails > create-app chirp | Application created at C:\Users\archa\work | Resolving Dependencies. Please wait... ... Download https://repo.grails.org/grails/core/org/gebish/geb-exceptions/1.1.1/geb-exceptions-1.1.1.jar
After the above step completes successfully, you should be able to see the files and directories as shown below.
archa@DESKTOP-EKEC9PU MINGW64 ~/work $ ls build/ build.gradle gradle/ gradle.properties gradlew* gradlew.bat grails-app/ grailsw* grailsw.bat grails-wrapper.jar src/
Opening the project in IntelliJ
Now open Intellij IDEA Community and select "Open or Import project" and point to the build.gradle file as shown above. The project will get loaded and you should see the project explorer showing something like the below
You can walk through this understand a lot of details, but a few noteworthy things are
- The grails-app folder is the main folder for the application, unlike a typical project where src has all the important source files.
- The starting point of the application is inside
- By default the
grails create-appscaffold command will not generate a controller with hello world. That has to be done separately.
Apart from the above pretty much everything in else in the project can be walked through and understood based on your understanding of java / spring / intellij / gradle.
There seems to be options to generate an intellij Idea project from the gradle file. From the root folder of the project run
./gradlew ideato generate .iml project file that can be opened from IntelliJ. But i don't know how this will accomodate any future changes that we do, like adding controllers, domains etc., so i continued with the approach of gradle project.
Building the application
Once the project is opened in Intellij, just click on the
Build Menu ->
Build Project to run it. Since Intellij can understand a gradle project structure and can invoke the build there is no customization needed here. It should work out of the box.
If there are errors during compilation (or) want to see the build output, click on the
Build. You should see the errors / output messages.
Running the application
By running the application i mean running it locally and not hosting it. Hosting has a whole bunch of networking, scalability, security concerns, but this is just about running it for development and debugging purposes.
For running an application, IntelliJ requires a Run configuration which has information on how to run a specific application. In this case running
gradlew tasksfrom the root folder of the application, showed the bunch of gradle tasks that are supported for this project and one of them is
bootRun, which compiles and runs the application.
So the key here is to configure the
bootRun gradle task as part of the Run configuration in IntelliJ. To do that, goto
Run menu ->
Edit Configuration. The following window will appear.
- Click on the + icon on the top left. Select gradle. In the configuration tab against gradle project enter the name of the project. If you click on the folder icon it will show up the name automatically.
- In the tasks tab enter
bootRun. The text box will auto suggest the tasks.
- Once done, go back to the project and click
Run. Now you should see that your application can be browsed at http://localhost:8080 .
Debugging the application
Another important aspect of development is debugging, and i find that quiet surprisingly easy with IntelliJ and Grails (possibly i should call this groovy, but for the context let's keep it grails). After configuring the build process, there isn't anything that we need to do to support debugging. I was able to set a breakpoint on
Main method and from the
Run menu select
Debug work[bootRun] and comfortably debugged the application.
I still completely don't understand how this can work, but a look into this article made me realize the the consistency between, the .class files generated by the groovy compiler to that of the java compiler is what matters. Groovy compiler compiles to bytecodes directly and looks like it does the job. IntelliJ as an IDE is still going to rely on the contracts of Java debug interface and doesn't have to do anything special. But i learned that Groovy MOP (metaprogramming) can be a challenge to debuggers because you can add properties and methods to a class dynamically, that might not be available in original class structure. To learn more read here.
By now we have a setup that is good enough to start working on solving problems. Rather than writing my own CURD application i am going to take some design problems well defined out there in the internet and post my learnings on top of them. The problem i plan to refer are sourced from here .
There are some free problems that can be taken up and solved as part of the Part #2 of this post. Stay tuned.