如何获取 Mojo.execute() 中的 ${basedir} 值(或其他属性)?

How to get ${basedir} value (or other properties) within the Mojo.execute()?

我正在尝试获取 Mojo 中 ${basedir} 的值。我以为我可以将其视为正常系统 属性 但

System.getProperty("basedir") 

returns null.

public void execute() throws MojoExecutionException, MojoFailureException {
    String baseDir = ???
}

这是通过注入 MavenProject and invoking the getBaseDir() 方法完成的,像这样:

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();
    }

}

@Parameter is used to inject the value ${project}, which resolves 从 Maven 会话构建的当前项目。

Using annotations 需要以下对 Maven 插件的依赖:

<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>