Support for Code Generation

I have a multi-module project which includes a custom code generator. The code generator is simply a module in the root project. This is a simplified example of the file structure:

- build.gradle
  -> code-generator/build.gradle
  -> business-logic-module-one/build.gradle
  -> business-logic-module-two/build.gradle

My expectation is that I would be able to build the code-generator as part of my multi-module build, and then have that code generator’s JAR files used in the downstream build of business-logic-module-one and business-logic-module-two.

However, after spending days pouring over all the available documentation and this forum, I’ve discovered that this simply isn’t supported in Gradle!

To quote @Peter_Niederwieser from an old post in this forum:

Today, the only clean solutions are to keep the code generator in ‘buildSrc’, or to have a separate build for the code generator

If the code generator needs to be a regular subproject, and must be used from the same build, you’ll have to resort to one trick or another. Here is what I did for one of my own projects:

This gets tricky when the code generator (in my case it’s a report generator) has in turn dependencies on other subprojects. I solved this with Groovy duck typing, avoiding compile-time dependencies on those subprojects. Also, my code generator gets compiled twice (unless it’s up-to-date), but I can live with that.

I am unable (or unwilling) to live with either of these compromises. However, given that the referenced post is around 2 years old, I was hoping that this problem has been addressed.

Could someone point me in the right direction, or maybe just give me some advice?

How about using Pluggable Annotation Processor in code-generator project.

I’m not sure I am following what your are alluding to.

I have an existing code generator, which I am attempting to plug into the build. This generator consumes a set of XML files and produces numerous Java source files.

I cannot see how anything related to annotation processing is relevant here? Could you clarify?

Thanks for your input.

I’m familiar with Pluggable Annotation Processor for generating java codes, so I suggested :smile:

By the way, If your code-generator library has main class, you can run it via JavaExec type task.

configurations {
dependencies {
    codeGenerator project(':code-generator')
task generateCode(type: JavaExec) {
    classpath configurations.codeGenerator
    main = 'your.code.generator.MainClass'