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

时间:2015-09-11 13:45:47

标签: maven maven-plugin mojo

我正试图在Mojo中获得${basedir}的值。我以为我可以看到这是一个普通的系统属性但是

System.getProperty("basedir") 

返回null

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

1 个答案:

答案 0 :(得分:6)

这是通过注入MavenProject并调用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用于将${project}<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> 注入到正在从Maven会话构建的当前项目中。{/ p>

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

A = 5;
B = 2;
C = A/B;
C = 
   2.5