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:
- Proper strategies for generating sources and compiling generated and non-generated code together?
- How to use gradle with generated sources?
- How can I compile generated sources?
- How to add generated sources to compile classpath without adding them to the main sourceSet?
- How to generate source code during project import?
- How do I get IntelliJ to recognize gradle generated sources dir?
- https://stackoverflow.com/questions/28345705/how-can-i-add-a-generated-source-folder-to-my-source-path-in-gradle
- https://github.com/google/protobuf-gradle-plugin/issues/109
- https://stackoverflow.com/questions/36749015/how-do-i-get-intellij-to-recognize-gradle-generated-sources-dir
- https://stackoverflow.com/questions/5025468/how-to-generate-source-files-and-compile-them-with-gradle
- https://stackoverflow.com/questions/35213894/how-can-i-add-a-generated-source-folder-to-my-source-path-in-gradle-and-intellij
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?