Developing a web application using less known technologies and tools can prove to be tricky sometimes. We have taken upon a challenge to configure debugging in Eclipse and IntelliJ for a project using Grails web application framework and Gradle build tool. But first, we'll do a brief overview of the technologies and tools mentioned in this article.
Table of Contents
1. Technologies and tools
To get a better overview we'll briefly skim though the technologies and tools that we are working with.
1.1. Grails
Grails is an open source full stack framework for developing web applications, built on top of Spring Boot. It uses a programming language called Groovy, which is currently developed by Apache and compatible with Java syntax. The main purpose of grails was to build on Java technologies (Spring, Hibernate) to reduce the complexity of developing web applications on Java platform.
1.2. Gradle
Gradle is an open source build automation tool, similar to Maven, which uses domain specific language (DSL) instead of XML based scripts. DSL is based on Groovy programming language. Grails has been using Gradle build system since version 3.1. The build is defined in build.gradle file, which defines the project version, repositories and dependencies.
1.3. Eclipse
Eclipse is an open source integrated development environment (IDE) primarely used for developing Java applications. To find out more about how to start working with Eclipse, check out our tutorial on installing Eclipse
Using Gradle with Eclipse requires an installation of a plugin called Buildship Gradle Integration 3.0. It is possible to install it through Eclipse Marketplace by going to the top menu in Eclipse and clicking on Help -> Marketplace..., typing buildship into the search area and clicking Install.
1.4. IntelliJ
IntelliJ IDEA is a Java IDE developed by JetBrains, mostly used for commercial development. Ultimate edition of IntelliJ is proprietary, but community edition has been released under Apache License v2.0.
2. Launch configuration
To run a web application we have developed, we normally have to run a set of commands in the command line or press on play/run button in our IDE. We are going to walk though three different ways of doing this: running commands in the command line, setting up launch configurations in Eclipse IDE and in IntelliJ IDE. After setting up run configuration we will also set up debug configuration, which is essential for debugging web applications.
2.1. Setting up run and debug configuration
2.1.1. Command line
To run Grails project we can, independently of our build tool, use the following Grails command:
$ grails run-app
This command is equivalent to the following Gradle command:
$ gradlew bootRun
To run the application in debug mode, add --debug-jvm:
$ gradlew bootRun --debug-jvm
These commands should always be run from within your Grails project folder.
To get command line help on commants in Gradle that you’d like to execute, you can type this:
gradlew help --task :’taskName’
In the case of bootRun the command would look like this:
gradlew help --task :bootRun
2.1.2. Eclipse
2.1.2.1. Run configuration
In the top menu of Eclipse click on Run and select Run Configurations. Select Gradle Project and create a new launch configuration. Give your configuration a meaningful name.
In the Gradle Tasks field type:
bootRun –debug-jvm
--debug-jvm tells the JVM to wait for the debugger to connect to it, using port 5005 by default.
To define the working directory, click on button Variables....
In the Select Variable window select variable project_loc and click on Configure button.
Select your resource by typing the name of your project in the top field under Select a resource to open. In our example the name of the project is TestGradle. Your project should be listed in the Matching resources field. Select it and click OK.
Your setup should now look like this:
Click on Run and continue with the next step.
2.1.2.2. Debug configuration
In the top menu of Eclipse click on Run and select Debug Configurations. Choose Remote Java Application, create a new configuration and name it.
Choose your project by clicking on Browse button and set the port to 5005. 5005 is the debug port, it specifies the port through which the debugger can connect to the JVM.
In the main menu click on Run and select Debug. By default the application will start on localhost:8080.
In case you'd like to change the application (server) port from default 8080 to another port (eg. 8888), you can add the following command to your build.gradle:
bootRun {
systemProperty 'server.port', '8888'
}
2.1.3. IntelliJ
2.1.3.1. Run configuration
In the top menu of IntelliJ click on Run/Debug Configurations, select Edit Configurations -> Add New Configuration -> Gradle. Add the Name: "mtool bootRun", name the Gradle project: mtool-web, and define Tasks: bootRun. Click OK.
After configuring run configuration, run mtool bootRun by clicking on run/play button.
2.1.3.2. Debug configuration
In the top menu click on Run/Debug Configurations and select Edit Configurations. Click on Add New Configuration -> Remote. Add the Name: "remote debug".
Open build.gradle and add the following code:
bootRun { jvmArgs = ["-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005"] }
After that, run debug configuration called remote debug by clicking on run/play button.
3. Summary
After a short overview of the technologies and tools mentioned in this article we demonstrated three different ways of setting up launch configuration for Grails project with Gradle - we set up run and debug configuration in command line, Eclipse IDE and IntelliJ IDE.