Apache Maven Accessing the project information


Example

The plugin can, among others, access information about the current Maven project being built.

@Mojo(name = "project")
public final class ProjectNameMojo extends AbstractMojo {

    @Parameter(defaultValue = "${project}", readonly = true, required = true)
    private MavenProject project;

    public void execute() throws MojoExecutionException, MojoFailureException {
        getLog().info("Hello, this is " + project.getName());
    }
}

The above example would print in the console the name of the Maven project it is run on, which is specified in the <project>/<name> element of its POM.

The MavenProject class used in the plugin requires a dependency of maven-core with a (default) compile scope in the plugin's POM:

<dependency>
    <groupId>org.apache.maven</groupId>
    <artifactId>maven-core</artifactId>
    <version>3.3.9</version>
</dependency>

Furthermore, using annotations requires the following dependency in the plugin's POM:

<dependency>
  <groupId>org.apache.maven.plugin-tools</groupId>
  <artifactId>maven-plugin-annotations</artifactId>
  <version>3.5</version>
  <scope>provided</scope> <!-- annotations are needed only to build the plugin -->
</dependency>