44

Problem

I want to use the interactive debugger with IntelliJ. Unfortunately, I can't convince IntelliJ to load and compile the plugin. However, I can do gradle clean build and the plugin builds and runs its tests as expected.

Specifically, I'm trying to debug local changes to gradle-js-plugin and IntelliJ says it can't find com.google.javascript.jscomp.CompilerOptions as well as spock.lang.Specification. (I'm thinking maybe it's something about the way they are loaded, but that's a guess.)


Things I've tried

NOTE: I didn't revert any processes between steps.

0. My First Guess

I noticed a howto on docs.codehaus.org. IntelliJ couldn't find org.gradle.launcher.GradleMain, so I've adapted it to use GradleLauncher with the following:

import org.gradle.GradleLauncher

class GradleScriptRunner {
    public static void main(String[] args) {
        GradleLauncher.newInstance(
            "-p", 
            "/path/to/gradle-js-plugin/src/test/resources/build.gradle", 
            "clean assemble"
        )
    }
}

Per GradleLauncher's documentation.

Outcome: IntelliJ won't compile the project.


1. Per Peter Niederwieser's answer Fix idea project & debug via plugin

Steps

  1. ~# cd /path/to/gradle-js-plugin && gradle cleanIdea idea
  2. Opened the newly created project and attempted to debug using the ScriptRunner from step 0.

Outcome: Project compiles (yay!), but I can only hit breakpoints in GradleScriptRunner.groovy.


2. Per Peter Niederwieser's answer run gradle CLI w/ special options

1 & 2. Merged for clarity:

~# export GRADLE_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"
~# gradle clean assemble
Listening for transport dt_socket at address: 5005
  1. Configure IntelliJ to connect to this port and start debugging (see image): How I configured the debugger

For this step I tried the following .gradle file configurations:

1. Use only build.gradle

--build.gradle--

apply plugin: 'groovy'
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'maven'
apply plugin: 'js'

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
    }

    dependencies {
        compile findProject "/path/to/gradle-js-plugin"
    }
}

repositories {
    mavenLocal()
    mavenCentral()
}

Outcome:

FAILURE: Build failed with an exception.

* Where:
Build file '/path/to/gradle-js-plugin/src/test/resources/build.gradle' line: 13

* What went wrong:
A problem occurred evaluating root project 'resources'.
> No such property: findProject for class: org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 8 mins 50.498 secs

2. Use both build.gradle and settings.gradle

--settings.gradle--

include "/path/to/gradle-js-plugin"

--build.gradle--

apply plugin: 'groovy'
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'maven'
apply plugin: 'js'

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
    }
}

repositories {
    mavenLocal()
    mavenCentral()
}

Outcome:

FAILURE: Build failed with an exception.

* Where:
Build file '/path/to/gradle-js-plugin/src/test/resources/build.gradle' line: 5

* What went wrong:
A problem occurred evaluating root project 'resources'.
> Plugin with id 'js' not found.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 13.553 secs


My Setup

Gradle

~# gradle -v
------------------------------------------------------------
Gradle 1.0
------------------------------------------------------------

Gradle build time: Tuesday, June 12, 2012 12:56:21 AM UTC
Groovy: 1.8.6
Ant: Apache Ant(TM) version 1.8.2 compiled on December 20 2010
Ivy: 2.2.0
JVM: 1.7.0_04 (Oracle Corporation 23.0-b21)
OS: Linux 3.2.0-2-amd64 amd64

Java

~# java -version
java version "1.7.0_04"
Java(TM) SE Runtime Environment (build 1.7.0_04-b20)
Java HotSpot(TM) 64-Bit Server VM (build 23.0-b21, mixed mode)

IntelliJ

IntelliJ IDEA Ultimate 117.499 w/ Bundled Gradle plugin

Hoping for

Any tips that'll get me into debug mode within the plugin.

Community
  • 1
  • 1
fncomp
  • 6,040
  • 3
  • 33
  • 42
  • FWIW, I am able to step through the plugin code using Peter's technique. Please let me know if you have any troubles with this. – Eric Wendelin Jul 03 '12 at 03:22

3 Answers3

34

I was able to debug gradle sources (including plugins) using -Dorg.gradle.debug=true (found on gradle forum):

  1. Stop daemons if any:

    ./gradlew --stop

  2. Run

    ./gradlew <task> --no-daemon -Dorg.gradle.debug=true

  3. Connect remotely to gradle process (port 5005) - if using IntelliJ IDEA, see OP's image above

It should stop on breakpoints now.


BTW, I have created a separate IntelliJ IDEA project for gradle sources. Since I use gradle wrapper, I have grabbed the sources from

~/.gradle/wrapper/dists/gradle-1.11-all/7qd8qq8te5j4f5q9aaei3gh3lj/gradle-1.11/src

In IDEA I did File->Import Project, then selected the above path, then - "Create project from existing sources". Hit Next couple of times (made sure I didn't include any jars from lib/plugins directory, since IDEA would complain that most project files already exist).

I then created a remote debug configuration in that IDEA project and used it for debugging gradle.

  • For some reason this works to debug plugins, while using `GRADLE_OPTS` as per the accepted answer does not. Thanks. – Raman May 28 '19 at 17:41
28

First, it sounds like there is a problem with your IDEA Gradle project. If you run gradlew cleanIdea idea and then open the generated project from IDEA (rather than using the JetGradle plugin), all should be fine.

Second, if you still can't get the GradleMain/GradleLauncher (the former class does exist) approach to work, another approach is to debug the Gradle build as an external application. For that you need to add -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005 to the GRADLE_OPTS environment variable, run the build from the command line, wait until it suspends, and then start a "Remote" run configuration (with corresponding settings) from IDEA. At that point the debugger should connect to the Gradle process and you should be up and running.

Peter Niederwieser
  • 121,412
  • 21
  • 324
  • 259
  • +1 IntelliJ now recognizes the classes, but doesn't hit breakpoints inside the plugin yet. I'm going to try the external approach. To be clear, I do hit break points inside of main, but I don't hit breakpoints inside the plugin when running main via the debug tool. I'll mark as answer if I get to a break point in the plugin. Thanks. – fncomp Jun 21 '12 at 02:06
  • I can't seem to get a debugger inside the plugin. I'm updating the question now. – fncomp Jun 21 '12 at 02:59
  • Seems like you forgot to declare the "js" plugin in the build script section. If the debugger doesn't stop at breakpoints, there may be a mismatch between the versions of the "js" plugin and Gradle that you use in the build, and the versions that you have configured in the IDE. Make sure that they are the same. – Peter Niederwieser Jun 21 '12 at 07:27
  • I thought `compile findProject "/path/to/gradle-js-plugin"` would create an instance of the plugin at runtime. Is this incorrect? If not, can you point me at how I should be including the plugin to use the local version. – fncomp Jun 21 '12 at 16:21
  • Note exactly sure what you are trying to achieve, but you can't apply a plugin in the same build it is declared in. Also, in the last `build.gradle` you don't have a `dependencies` section at all. – Peter Niederwieser Jun 21 '12 at 21:27
  • I see, I am willing to setup my project however I need to in order to get the debugger running inside of a plugin. I'm new to Gradle, so I might be doing something completely wrong. Is there a boilerplate project that would contain a debuggable gradle plugin? – fncomp Jun 21 '12 at 21:53
  • A regular remote debugging setup should suffice, but I recommend to ask the `js` plugin authors. – Peter Niederwieser Jun 21 '12 at 22:21
  • I wound up using test cases to get to a breakpoint in the plugin, but the advice you gave led me to where I needed to go. Thanks for all the help. – fncomp Jun 22 '12 at 20:17
18

IntelliJ IDEA 12.1 provides ability to debug gradle tasks out of the box - right-click target task at the JetGradle tool window tasks list and choose 'debug'

denis.zhdanov
  • 3,734
  • 20
  • 25