29

I am working on a maven plugin. I seem to have a hard time figuring out, what would be a good way to get POM information from project in which you execute the MOJO ?

For instance if I execute my mojo in another maven project I would like to get project name or some other parameters.

And one more thing, there is a context MAP in AbstractMojo.java class there is private Map pluginContext, could someone correct me if I am wrong but this is suppose to be used for passing information between mojos ?

Carlos Tasada
  • 4,438
  • 1
  • 23
  • 26
Xeperis
  • 1,449
  • 2
  • 25
  • 41
  • 1
    You should clearly specify what do you want and where do you need to access this info. The project information is available in the pom by default – Carlos Tasada May 15 '12 at 21:23
  • 1
    The first question i always ask in relationship with creation of plugins is: What would you like to achieve? are you sure you need to write a plugin? Furthermore take a look into other plugins for example, maven-assembly-plugin, maven-javadoc-plugin etc to see how they work etc. – khmarbaise May 15 '12 at 21:32

5 Answers5

34

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>
Jörn Horstmann
  • 33,639
  • 11
  • 75
  • 118
  • 1
    Works for me in maven 3.0.3; add the dependency then inject the project! – Raymond Kroeker May 24 '12 at 16:55
  • I think I found this answer elsewhere on the net but was confused by the fact that it was injected by adding of a previously unknown (to my project) dependency. This clears it up perfectly, thanks. – Steve Knight Jun 21 '16 at 09:23
  • According to [Tutorial: How to Create a Maven Plugin](https://dzone.com/articles/tutorial-create-a-maven-plugin) it's a:`maven-core`: „_needed when injecting the Maven Project into a plugin_“ (Apparently [since 2013](https://search.maven.org/artifact/org.apache.maven/maven-core). The [latest version of a:`maven-project` is from 2010](https://search.maven.org/artifact/org.apache.maven/maven-project)). And [the annotation is different, too](https://stackoverflow.com/a/63320868/1744774). – Gerold Broser Aug 08 '20 at 22:46
22
@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;
Jesse Glick
  • 24,539
  • 10
  • 90
  • 112
15

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.)

Partly Cloudy
  • 6,508
  • 3
  • 27
  • 16
  • 3
    Maybe for the @Parameter annotation you would like to add the maven-plugin-annotations artifact. – Nagy Attila Sep 14 '15 at 11:57
  • According to [Tutorial: How to Create a Maven Plugin](https://dzone.com/articles/tutorial-create-a-maven-plugin) it's a:`maven-core`: „_needed when injecting the Maven Project into a plugin_“ (and apparently already [since 2013](https://search.maven.org/artifact/org.apache.maven/maven-core). The [latest version of a:`maven-project` is from 2010](https://search.maven.org/artifact/org.apache.maven/maven-project)). And [the annotation is different, too](https://stackoverflow.com/a/63320868/1744774). – Gerold Broser Aug 08 '20 at 22:45
3

See Tutorial: How to Create a Maven Plugin:

POM

        <dependency>
            <!-- needed when injecting the Maven Project into a plugin  -->
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-core</artifactId>
            <version>3.6.3</version>
            <scope>provided</scope>
        </dependency>

Mojo

@Parameter(property = "project", readonly = true)
private MavenProject project;
Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
-1

maven-project for maven 2.x version was replaced with maven-model from version maven 3.x, so for new project, use

<dependency>
  <groupId>org.apache.maven</groupId>
  <artifactId>maven-model</artifactId>
  <version>3.6.3</version>
</dependency>
Yu Jiaao
  • 4,444
  • 5
  • 44
  • 57
  • Using this instead of [a:`maven-core`](https://stackoverflow.com/a/63320868/1744774): „_MavenProject cannot be resolved to a type_“ – Gerold Broser Aug 08 '20 at 22:56