How to debug a Gradle build.gradle file (in a debugger, with breakpoints)?

后端 未结 5 861
攒了一身酷
攒了一身酷 2020-12-12 13:01

Is there a tool that will allow me to set breakpoints in a build.gradle file and step through tasks in a debugger?

Note: I believe that I\'m asking a different ques

5条回答
  •  伪装坚强ぢ
    2020-12-12 13:41

    After reading various answers here the following steps will help you debug build.gradle to being able to break and investigate variables inside any custom task. I am using Eclipse remote debugging facilities

    1. Place this simple code where you want to break: try {throw new RuntimeException('Break');} catch (RuntimeException e) {}
    2. As recommended start your task with gradle mytask -Dorg.gradle.debug=true --no-daemon in the command prompt (don't have to do it in Eclipse)
    3. In Eclipse do Run -> Add Java Exception Breakpoint, choose RuntimeException and click "OK"
    4. Again in Eclipse go to Run -> Debug Configurations -> Remote Java Application and create new configuration that listens on localhost:5005. Name it whatever you want. Select a project that contains build.gradle you are debugging. Click Apply and Debug
    5. At this point the execution will start but will pause at the Exception-throwing line. And you can then start looking at your variables in the `Debug -> Variables" view, inspect the stacktrace, step through the code etc.
    6. No magic, alas, you will not see anything highlighted in build.gradle but you can pretty much guess where you are at
    7. Obviously on subsequent runs you don't need step 3 and in 4 you can reuse previously created configuration
    8. If you want to use this in the multiple places simply create a method, use different type of exception and feel free to enhance this idea in any way possible

    For example:

    void halt() {
        try {
            throw new RuntimeException('Break');
        } catch (RuntimeException e) {
            print('Paused')
        }
    }
    
    task iterateDeclaredDependencies {
        doLast {        
            Object configs = configurations.all
            halt();
            print(configs)
        }
    }
    

提交回复
热议问题