【问题标题】:How to get ${basedir} value (or other properties) within the Mojo.execute()?如何在 Mojo.execute() 中获取 ${basedir} 值(或其他属性)?
【发布时间】:2015-12-08 02:33:09
【问题描述】:

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

System.getProperty("basedir") 

返回null

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

【问题讨论】:

    标签: maven maven-plugin mojo


    【解决方案1】:

    这是通过注入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} 注入到正在从 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>
    

    【讨论】:

    • 赞成。 @Component 仍然有效,但现在已弃用 MavenProject,应使用 @Parameter(defaultValue = "${project}", readonly = true)。谢谢!
    • @donsenior 你说得对,我用非弃用方法编辑,并添加了更多解释。
    猜你喜欢
    • 2021-06-26
    • 2012-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-22
    • 2019-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多