Friday, July 12, 2013

M2Eclipse and unsupported plugins

Recently I got a mail from a friend, to explain a very usual pattern: his Apache Maven build uses a plugin, that is not supported by M2E (the well known "plugin execution not covered" problem). Moreover, his plugin was actually generating some source, and hence, the M2E imported project was "all red", unusable in IDE.

After exchanging few mails, I decided to publish the stuff here, just for future reference, might help someone else too. In this example I'll use the hawtbuf maven plugin, as it completely fits into this scenario: it would generate some sources, you would code against.

On M2E side to make this work, you need to install the "m2e connector for build-helper-maven-plugin". All the other changes are done in project POMs, by adding profiles that are triggered only when running in Eclipse IDE with M2E, hence, they will not stir anything in "normal" CLI triggered builds (like CI jobs or you building in console).

Also, the example assumes your POM's plugins section already contains a configuration for org.fusesource.hawtbuf:hawtbuf-protoc Maven plugin, and that all works as expected when executed from CLI (so, sources are generated,  plugin is well configured and it's execution is bound). If project is in this state, and works fine from CLI, then all you need to make it work in M2E is a bit of M2E specific profiles, here is it:


A bit of explanation.

This plugin will activate itself only when project runs within (is imported into) Eclipse/M2E, no other build, like those execute from CLI or on CI are affected (unless you explicitly enable this profile with -Pm2e which I don't think you want). Profile is activated on presence of "m2e.version" system property, that is set by M2E, see line 6.

Then, the pluginManagement section adds the lifecycle mapping plugin and targets the hawtbuf plugin (line 21-23). Here you can customise this to fit to your plugin's GAV. Line 29 tells M2E to <execute/> the plugin, which is what we want, to have generated sources available in IDE. It should be noted, that the org.eclipse.m2e:lifecycle-mapping plugin referred in plugin management is actually a "fake" plugin, it does not exists, it's purpose is solely to "sneak in" the needed information into POM, and to make M2E know from where to source it. Also, since it's in pluginManagement section, Maven will not even touch it! Basically, this structure in POM is used only to make M2E able to consume it.

Lastly, the build helper plugin is used to make M2E add the generated sources folder to the project build class path. This time, we deal with a real plugin, but it will be executed only when running project from IDE.

Hope helps and have fun!