Java Maven MOJO - getting information from project POM

后端 未结 5 775
醉话见心
醉话见心 2021-02-05 02:50

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

相关标签:
5条回答
  • 2021-02-05 03:30

    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>
    
    0 讨论(0)
  • 2021-02-05 03:34

    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;
    
    0 讨论(0)
  • 2021-02-05 03:35

    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>
    
    0 讨论(0)
  • @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;
    
    0 讨论(0)
  • 2021-02-05 03:47

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

    0 讨论(0)
提交回复
热议问题