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
You can inject the current maven project into your mojo with a field declared like this:
/**
* @parameter default-value="${project}"
* @required
* @readonly
*/
MavenProject project;
The projects name is then available by simply calling project.getName().
To use this API, you need to add the maven-project artifact as a dependency:
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
<version>2.0.6</version>
</dependency>
See Tutorial: How to Create a Maven Plugin:
<dependency>
<!-- needed when injecting the Maven Project into a plugin -->
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>3.6.3</version>
<scope>provided</scope>
</dependency>
@Parameter(property = "project", readonly = true)
private MavenProject project;
maven-project for maven 2.x version was replaced with maven-model from version maven 3.x, so for new project, use
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>
<version>3.6.3</version>
</dependency>
@Component
private MavenProject project;
also works (more succinctly and intuitively) if using the new maven-plugin-annotations, which is the default for new mojos created from maven-archetype-plugin.
EDIT (thanks to @bmargulies): although the @Component Javadoc as of 3.2 suggests using it for MavenProject, apparently that is deprecated and the suggestion is dropped as of 3.3; the idiom suggested by maven-plugin-tools-annotations (as of 3.3) is something like this (both seem to work):
@Parameter(defaultValue="${project}", readonly=true, required=true)
private MavenProject project;
The preferred syntax is now:
@Parameter(defaultValue = "${project}", required = true, readonly = true)
MavenProject project;
You will have to add a dependency for maven-project to your plugin's pom:
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
<version>2.0.6</version>
</dependency>
(Thanks to others who have supplied this information already. This answer combines them in one place.)