I am working on a maven plugin. I seem to have a hard time figuring out, what would be a good way to get POM information from project in which you execute the MOJO ?
For instance if I execute my mojo in another maven project I would like to get project name or some other parameters.
And one more thing, there is a context MAP in AbstractMojo.java class there is private Map pluginContext, could someone correct me if I am wrong but this is suppose to be used for passing information between mojos ?
You can inject the current maven project into your mojo with a field declared like this:
The projects name is then available by simply calling
project.getName()
. To use this API, you need to add themaven-project
artifact as a dependency:also works (more succinctly and intuitively) if using the new
maven-plugin-annotations
, which is the default for new mojos created frommaven-archetype-plugin
.EDIT (thanks to @bmargulies): although the
@Component
Javadoc as of 3.2 suggests using it forMavenProject
, apparently that is deprecated and the suggestion is dropped as of 3.3; the idiom suggested bymaven-plugin-tools-annotations
(as of 3.3) is something like this (both seem to work):The preferred syntax is now:
You will have to add a dependency for
maven-project
to your plugin's pom:(Thanks to others who have supplied this information already. This answer combines them in one place.)