Java Maven MOJO - getting information from project POM
@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;
See Tutorial: How to Create a Maven Plugin:
POM
<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>
Mojo
@Parameter(property = "project", readonly = true)
private MavenProject project;
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>
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.)