Idiomatic way to generate source code that works in IDEs automatically without using specific IDE Gradle plugin

There are a lot of questions out there around source code generation with Gradle. Here are some that I found after a few minutes of searching:

There are a lot of varied answers in here.

What I am currently doing is:

  • Add a directory to the SourceSet
  • Create a task to generate the code into that directory
  • Add a task dependency using getCompileTaskName(String language) on the generation task

This works well at build time, but doesn’t integrate so well with IDEs right now. In order to see the code, you must first run something like ./gradlew assemble to generate those classes before importing or using in the IDE.
What I would like achieve is:

  • Sources generated on import in IntelliJ (or other IDEs)
  • Sources can be regenerated on plugin upgrade/configuration change
  • Up-to-date checking if possible
  • Users don’t have to execute Gradle commands on import or change to get newer generated classes
  • Users don’t need to use the idea Gradle plugin

Is there a newer answer to handle these use cases?

2 Likes

One possible answer that was brought up to me on the Gradle Slack: https://github.com/JetBrains/gradle-idea-ext-plugin

Provides hooks to be used during Gradle’s resolution process.