Java Maven MOJO - getting information from project POM
Asked Answered
R

5

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 ?

Rossi answered 15/5, 2012 at 18:0 Comment(2)
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 defaultCockcroft
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.Sessions
I
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>
Inhibit answered 15/5, 2012 at 21:32 Comment(3)
Works for me in maven 3.0.3; add the dependency then inject the project!Munafo
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.Ewald
According to Tutorial: How to Create a Maven Plugin it's a:maven-core: „needed when injecting the Maven Project into a plugin“ (Apparently since 2013. The latest version of a:maven-project is from 2010). And the annotation is different, too.Chine
P
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;
Pearlstein answered 11/3, 2014 at 20:53 Comment(1)
OK; what is recommended instead (for plugins using the new API)?Pearlstein
U
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.)

Underwing answered 6/8, 2015 at 15:51 Comment(2)
Maybe for the @Parameter annotation you would like to add the maven-plugin-annotations artifact.Cozen
According to Tutorial: How to Create a Maven Plugin it's a:maven-core: „needed when injecting the Maven Project into a plugin“ (and apparently already since 2013. The latest version of a:maven-project is from 2010). And the annotation is different, too.Chine
C
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;
Chine answered 8/8, 2020 at 22:43 Comment(0)
P
-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>
Perlman answered 2/7, 2020 at 9:15 Comment(1)
Using this instead of a:maven-core: „MavenProject cannot be resolved to a typeChine

© 2022 - 2024 — McMap. All rights reserved.