Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle Plugin that must load the compiled classes from the project

Tags:

gradle

As part of writing a Gradle plugin for Flyway, we stumbled upon a problem when dealing with Java migrations.

What is the best way to provide a Gradle plugin access on its classpath to the compiled classes of the project so that it can load and execute them?

like image 322
Axel Fontaine Avatar asked Sep 06 '25 09:09

Axel Fontaine


1 Answers

So the situation is that we have a plugin that adds a task that wants to execute code contained in the project that the plugin is applied to. In this case, the task (class) should have an input property of type Iterable<File> that gets configured (by the plugin) with the class path of the code to be executed (e.g. sourceSets.main.runtimeClasspath). The task can then choose between the following ways to execute the code:

  • The task uses project.javaexec {} to execute the code in a separate JVM. If the code isn't directly executable, the task may need to inject some bootstrap code onto the javaexec class path. A potential alternative to using project.javaexec is to use a JavaExec task in the first place.
  • The task creates a new class loader, populates it with the class path, loads and instantiates the classes that serve as the entry point(s) to the API, and makes use of them as appropriate. If the task is written in Groovy, it can leverage duck typing, and no reflective code will be necessary beyond creating the entry points.
like image 139
Peter Niederwieser Avatar answered Sep 11 '25 03:09

Peter Niederwieser