Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails 3 Debugging in IntelliJ

I am working with Grails again after a few years away from it. I am using IntelliJ 2016.01 and Grails 3.1.4. It should be easy as pie, right? Everything is working fine except I cannot launch the debugger from within IntelliJ as I am used to doing. In a former life I using IJ10 with Grails 1.0.5.

Through various searches I have come to find out that the change originated in Grails 2.3 when they began running in forked mode for various reasons. Without understanding all the technical details my understanding is that this was done for performance reasons. Anyway, I was able to find a workaround but it seems like a long way around. Does anyone know of a better way? What am I missing?

I created an ant task to call grails run-app:

<target name="start-debug-grails">
    <exec executable="cmd">
        <arg value="/c"/>
        <arg value="start"/>
        <arg value="grails"/>
        <arg value="run-app"/>
        <arg value="--debug-jvm"/>
    </exec>
</target>

If you run this task on its own you see grails runs in debug mode in a new cmd window: | Running application... Listening for transport dt_socket at address: 5005

Then I created a 'Remote' Run/Debug configuration in IntelliJ to attach on port 5005. I DO have the debug button available for this Run/Debug configuration so I click debug. It attaches fine.

First I see this in IntelliJ:

Connected to the target VM, address: 'localhost:5005', transport: 'socket'

Then a few moments later the cmd window is updated to show the app is up in debug mode:

| Running application...
Listening for transport dt_socket at address: 5005
Grails application running at http://localhost:8080 in environment: development

At this point I can navigate the app on port 8080 with no problem. I can make changes to certain parts of the code such as controllers and it will be compiled and made available immediately. Yeah.

I took it a step further and added stop ant task (which was a bit harder than it should have been) so that I could execute both tasks (stop first if process is running) then start:

<target name="stop-debug-grails" depends="-stop-debug-init, -stop-debug-kill"/>

<target name="-stop-debug-init">
    <!--check if process is running-->

    <exec executable="jps">
        <arg value="-l"/>
        <redirector outputproperty="process.pid">
            <outputfilterchain>
                <linecontains>
                    <contains value="grails"/>
                </linecontains>
                <replacestring from=" org.grails.cli.GrailsCli"/>
            </outputfilterchain>
        </redirector>
    </exec>
    <echo>
        ${process.pid}
    </echo>
    <condition property="do.kill">
        <not>
            <!--checks to make sure pid is non blank before calling kill-process-->
            <equals arg1="${process.pid}" arg2="" />
        </not>
    </condition>
</target>

<target name="-stop-debug-kill" if="do.kill">
    <echo>
        killing process ${process.pid}
    </echo>
    <exec executable="taskkill" osfamily="winnt">
        <arg value="/F"/>
        <arg value="/PID"/>
        <arg value="${process.pid}"/>
    </exec>
    <exec executable="kill" osfamily="unix">
        <arg value="-9"/>
        <arg value="${process.pid}"/>
    </exec>
</target>

I had to edit my start-debug-grails task to wait for grails to start listening on port 5005.

<!--delay to let the server start-->
<sleep seconds="20"/>

Now I can debug in IntelliJ the way I am used to through by clicking debug for the Remote process in IntelliJ. Does anyone know of a better way to handle this?

like image 545
bigMC28 Avatar asked Apr 11 '16 19:04

bigMC28


People also ask

How do you use Grails in IntelliJ?

Support for the Grails framework is not bundled with IntelliJ IDEA. You can install the Grails plugin from the JetBrains repository as described in Install plugins. The latest compatible version of IntelliJ IDEA is 2021.3. You can find the documentation for Grails support in earlier versions of IntelliJ IDEA Help.

How do I debug multiple threads in IntelliJ?

Start the debug session by clicking the Run button near the main method and selecting Debug. When the program has run, both threads are individually suspended in the addIfAbsent method. Now you can switch between the threads (in the Frames or Threads tab) and control the execution of each thread.


1 Answers

Debugging is very simple in Grails 3. Open the class Application.groovy and just click Debug.

like image 69
user3718614 Avatar answered Oct 21 '22 04:10

user3718614