Debug Gradle plugins with IntelliJ

2019-01-21 02:52发布

问题:

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):

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.

回答1:

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.



回答2:

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.



回答3:

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'