This is done by injecting the MavenProject
and invoking the getBaseDir()
method, like this:
public class MyMojo extends AbstractMojo {
@Parameter(defaultValue = "${project}", readonly = true, required = true)
private MavenProject project;
public void execute() throws MojoExecutionException, MojoFailureException {
String baseDir = project.getBaseDir();
}
}
The @Parameter
is used to inject the value ${project}
, which resolves to the current project being built from the Maven session.
Using annotations requires the following dependency on the Maven plugin:
<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>
@Component
still works, but is now deprecated forMavenProject
, should use@Parameter(defaultValue = "${project}", readonly = true)
. Thanks! – Franko