How can I attach a debugger to the javac process (for debugging annotation processor)

I’m working on an annotation processor. Both the annotation processor and the code I use it on are in the same gradle project (but different modules). I’d like to attach a debugger as part of implementing the annotation processor is a little exploratory at this point.

The project structure is basically:

core // the shared annotations
example // the exploratory project
plugin // the annotation processor

I do have everything wired up for the annotation processor to kick in (yay System.out.println statements), and properly, since gradle doesn’t complain (I’m on 4.8). I just haven’t figured out a way to attach a debugger to the javac code so I can poke around the inputs to the plugin.

I’ve tried using the following under the compileJava block in the project using the annotation processor:

  1. compileJava {
        options.compilerArgs << "-Xdebug"
        options.compilerArgs << "-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=7999"
    }
    
  2. compileJava {
        options.compilerArgs << "-J-Xdebug"
        options.compilerArgs << "-J-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000"
    }
    

And both of them throw an error of > invalid flag:, and then the first flag used in each each attempt.

Any ideas on how I can do this?

With recent Gradle versions make sure the Compiler is started in a forked process, for example by using a toolchain differently from what you run Gradle with and then set the debugger option you want as jvm args, so for example like

tasks.compileJava {
    javaCompiler = javaToolchains.compilerFor { languageVersion.set(JavaLanguageVersion.of(17)) }
    options.forkOptions.jvmArgs = listOf("-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=8000")
}

Ah, stupid me, you can just tell it to fork:

tasks.compileJava {
    options.isFork = true
    options.forkOptions.jvmArgs = listOf("-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=8000")
}