I set up my environment to develop a mojo and be able to debug it on intellij (version 13 at the time writing).
Here’s a very lean list of the things you need to do the job:
create the mojo project from org.apache.maven.archetypes:maven-archetype-mojo
- create your mojo entry point, it’s just a java class extending AbstractMojo and override execute() method
- comment your mojo class with a javadoc “@goal whatever” and “@phase whateverphase” . Those apparently useless comments are actually used to determine which goal and phase are associated with your mojo. You can ignore @phase and determine that later, but @goal (AFAIK) is mandatory
- create a test project that uses your brand new mojo as a plugin, specify an execution and a goal to be invoked (the same goal you wrote in the comment at step 3). At this point you can specify the phase you want your mojo to be executed into if you didn’t specify it in your comment.
- mvn install your mojo
- mvnDebug install (or specify the phase you need) your test project. mvnDebug will listen on port 8000 on localhost for incoming debug connections.
- in intellij, create a “Remote” run\debug configuration and change the settings as follows:
- transport: socket
- debugger mode: attach
- host: localhost
- port: 8000
- set a breakpoint on execute() and start debugging your mojo!
Good to knows:
- you can map a (private) variable of your mojo to a configuration parameter on your pom, just decorate your private variable with a javadoc comment such as @parameter alias=”whateverParam” (this will map <configuration><whatever> to the private String(?) decorated with that comment.
- you can map whatever class to manage configuration parameters of your mojo: just create a class and make it implement java.io.Serializable. The properties of the class are resolved and populated by reflection (it’s a very useful if you need nested objects as configuration!!!)
- more to come… 🙂