Java Maven MOJO - getting information from project POM

会有一股神秘感。 提交于 2019-12-03 01:14:46

You can inject the current maven project into your mojo with a field declared like this:

/**
 * @parameter default-value="${project}"
 * @required
 * @readonly
 */
MavenProject project;

The projects name is then available by simply calling project.getName(). To use this API, you need to add the maven-project artifact as a dependency:

<dependency>
    <groupId>org.apache.maven</groupId>
    <artifactId>maven-project</artifactId>
    <version>2.0.6</version>
</dependency>
@Component
private MavenProject project;

also works (more succinctly and intuitively) if using the new maven-plugin-annotations, which is the default for new mojos created from maven-archetype-plugin.

EDIT (thanks to @bmargulies): although the @Component Javadoc as of 3.2 suggests using it for MavenProject, apparently that is deprecated and the suggestion is dropped as of 3.3; the idiom suggested by maven-plugin-tools-annotations (as of 3.3) is something like this (both seem to work):

@Parameter(defaultValue="${project}", readonly=true, required=true)
private MavenProject project;

The preferred syntax is now:

@Parameter(defaultValue = "${project}", required = true, readonly = true)
MavenProject project;

You will have to add a dependency for maven-project to your plugin's pom:

<dependency>
    <groupId>org.apache.maven</groupId>
    <artifactId>maven-project</artifactId>
    <version>2.0.6</version>
</dependency>

(Thanks to others who have supplied this information already. This answer combines them in one place.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!