There are a couple of approaches that you could take to invoke a main method in an external Jar:
- Use a
JavaExec
task, or a custom task that leverages theproject.javaexec
method. - Put the external Jar on the build script class path and invoke its main method directly or reflectively from a custom task. The external Jar could be a transitive dependency of the plugin Jar. Alternatively, it could be left to the user to put the external Jar on the build script class path (
buildscript { dependencies { classpath ... } }
). - Write a custom task that creates its own class loader, puts the external Jar on it, and calls its main method reflectively.
The first approach will run the code in a separate JVM, which is often desirable. The second and third approaches can also invoke methods other than main methods. In the first or third approach, the plugin could add a configuration which allows the user to supply the external Jar by declaring a dependency in the dependencies
block. (The plugin could provide a default dependency.) This is what many built-in Gradle plugins do, for example the code quality plugins (checkstyle
etc.). You may want to study their code to get some inspiration.
External plugins should only use classes from Gradle's public API, that is classes that are documented in the Javadoc or Groovydoc. (Not all internal classes have internal
in the package name.)