How to reference a class built in one module from a build script of another module

( #1

Hi folks, I have a multi-module project where I want one of the modules to be able to invoke a class that is built by one of the other modules.

Specifically, I’m building a code generator and I want to be able to invoke that generator in order to produce a sample of generated code and also to run tests on the generated code. So I have 3 modules in my multi-module project; generator, sample, testing.

I’ve tried various ways to organize things but I get stuck on the fact that in order for the build to invoke the generator, the build script has to import the generator class. But when I run the build, it fails because at the point where the build script is compiled the generator class has not yet been built and therefore cannot be resolved.

So, is there any way I can work around this issue? If it helps, I’m packaging the generator as a Gradle plugin but I don’t know if its possible to apply that plugin to the sample module. I imagine I’ll have a similar problem whereby the plugin has not yet been built at the time the build script is compiled.

(Peter Niederwieser) #2

With Groovy being a compiled language, the challenge is to come up with a task that invokes the code generator without statically referencing any generator classes. Some solutions:

  • Declare a task that executes an inline script using a ‘GroovyShell’. The inline script interacts with the generator (which needs to be placed on the ‘GroovyShell’'s class path). * Declare a task (e.g. of type ‘JavaExec’) that invokes the generator as a command line application. The main class can live either in the generator or in the sample project. * Declare a task that creates a class loader for the generator’s code, loads and instantiates the generator’s entry point(s), and interacts with the generator using Groovy method calls (without any static typing).

( #3

Thanks very much Peter.

I tried your second suggestion and it worked perfectly.