Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm trying to get the value of the ${basedir} within a Mojo. I thought I could see that as a normal System property but

System.getProperty("basedir") 

returns null.

public void execute() throws MojoExecutionException, MojoFailureException {
    String baseDir = ???
}
like image 638
Andrea Avatar asked Sep 11 '15 13:09

Andrea


People also ask

Where is ${ Basedir?

${project. basedir} is the root directory of your project.

What is ${ Basedir?

${project.basedir} This references to the root folder of the module/project (the location where the current pom.xml file is located)

What is properties Maven plugin?

The Properties Maven Plugin is here to make life a little easier when dealing with properties. It provides goals to read properties from files and URLs and write properties to files, and also to set system properties. It's main use-case is loading properties from files or URLs instead of declaring them in pom.


1 Answers

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>
like image 103
Tunaki Avatar answered Oct 26 '22 06:10

Tunaki