Search code examples
javamavenclassloadermaven-plugin

How can I use a patched version of a Maven plugin?


I would like to use a modified version of a Maven plugin in my build (for experimental purposes). Therefore, I downloaded the source code of the plugin and adjusted it.

What is the best way to integrate the modified plugin? Can I intercept the loading of the Maven plugin and redirect it to a local jar? Can I alternatively add the (few) adjusted classes in a way such that the they are loaded (by the classloader) before the plugin? How?


Solution

  • Maven finds plugins the same way that it finds dependencies. So here is what I would try:

    1. Change the plugin's group id / artifact id / version number in its POM file and rebuild it.

    2. Install the plugin into your .m2repository; e.g. run mvn install.

    Alternatively after building the plugin JAR (however you do it), install it by running mvn install:install-file as described in Apache > Maven > Plugins > Apache Maven Install Plugin > Usage. This allows you to specify the group id / artifact id / version directly.

    It is advisable to choose a group/artifact/version that are unlikely to collide with an official version of the plugin. But that's for your sanity only.

    The downside of doing this is that (now) you are likely to have to publish or distribute your plugin along with your project source code. And maintain it to track changes in the original plugin. (Or don't let your experimental plugin escape.)