0

There is a gradle project, based on Groovy, that I develop and modify with VSCode. I have the gradle extension installed (vscjava.vscode-gradle, version 3.16.4) from Microsoft.

More often than not, I struggle due to some mistakes that I do in the gradle configuration file. I would like to debug like IntelliJ offers: https://www.jetbrains.com/help/idea/work-with-gradle-tasks.html#debug_gradle Image from IntelliJ documentation

I tried to add the -Dorg.gradle.debug=true --no-daemon option in gradle for the build, and I can then attach to the gradle build (I think?) with:

{
    "type": "java",
    "name": "Debug gradle",
    "request": "attach",
    "hostName": "localhost",
    "port": "5005"
},

However, I am not able to add breakpoints, pause the build, etc.

Relevant stackoverflow links found:

3
  • I think you are fighting an uphill battle with this. I would be surprised if VS Code supports debugging of Gradle scripts. It's not that long ago that you couldn't debug Gradle scripts even in IntelliJ. If you intend to use Gradle seriously I would strongly advise moving to IntelliJ, which has a free version (the Community edition). Having said that, in general you want to keep code to a minimum in your Gradle scripts so this should be necessary too often. Commented Jan 12 at 12:39
  • 1
    If you insist on using VS Code this approach might help you, for if you move your code out into a Java program (or other suitable JVM language) and have your build script call it then that might enable you to debug it if VS Code can handle that. The simplest way to do that would be to use the buildSrc folder. This folder is compiled and made available to project build scripts Commented Jan 12 at 12:41
  • Thank you @SimonJacobs for your insight. I would indeed prefer to stick to VS code, as I would like to avoid mixing IDEs. Out of curiosity, would you think it would be easier if I converted all the gradle files in kotlins instead of Groovy? However, I am not sure that I understand the buildSrc trick. Does it suggest to reimplement the logic in a custom function that I would reimplement from scratch? Commented Jan 12 at 16:39

1 Answer 1

1

As noted in the comments, I would be surprised if it is possible to debug a Gradle build script in VS Code since it wasn't that long ago that you couldn't even do that in IntelliJ1.

You may be able to get debugging to work by moving your Gradle logic into separate Java files; VS Code breakpoints may well work in Java files.

Extracting build logic into buildSrc folder

To move your logic to a Java file you can use the buildSrc folder. As an example of this:

  1. Put a new Gradle build file at buildSrc/build.gradle ready to write Java code:

    plugins {
        id 'java'
    }
    
  2. Place your Java function in a Java class, say in buildSrc/main/java/MyBuildHelper.java:

    import org.gradle.api.Project;
    
    public class MyBuildHelper {
    
        void helpMe(Project project) {
            project.getTasks()
                .create("myNewTask", task -> {
                    task.doLast(taskInner -> {
                        String output = taskInner.getName() + " is a runner!";
                        System.out.println(output);
                    });
                });
        }
    
    }
    
  3. This class is then exposed to your build script so you can write in your root build.gradle:

    new MyBuildHelper().helpMe(project)
    
  4. Now you can test it by running the new task added by the Java code:

    $ ./gradlew myNewTask
    

    which should print myNewTask is a runner! to the console.

This Java file may well be debuggable in VS Code using the methods you describe in your question. (Other JVM languages can be used in a similar fashion but I imagine Java support is more robust in VS Code.)

Use of Kotlin

You also asked about Kotlin. I would definitely recommend using Kotlin for your build file (and indeed as much of your code as possible! I am a Kotlin fan). This is because it is statically typed and will help you write correct code as you go along.

However I imagine Kotlin support on VS Code is not great. If you want to stick with VS Code, Kotlin may not be preferable if the VS Code support is insufficient; I cannot comment knowledgeably on Kotlin in VS Code. If you want to use Kotlin I would again push you to use IntelliJ (a step that I took a number of years ago and have not regretted).


1If you're not aware, IntelliJ IDEA is an application published by JetBrains, the authors of Kotlin, which is tightly integrated with Gradle, so they have a commercial interest in making IntelliJ IDEA work well with Gradle and Kotlin.

Sign up to request clarification or add additional context in comments.

1 Comment

Many thanks! I discover a new feature of gradle and I like it! (I plan on moving to Kotlin at some point for gradle files). It would however requires to rewrite my build files. Thank you for your effort in the answer. I will wait to have answer on how to debug with Gradle with vscode, to mark it as solved,for the new visitors on this page that struggle with it. But you got your well deserved +1!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.