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 = ???
}
${project. basedir} is the root directory of your project.
${project.basedir} This references to the root folder of the module/project (the location where the current pom.xml file is located)
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With