Can't set parameter using annotations on maven plugin

I am trying to develop a maven plugin and it doesnt work when I use @Parameter annotation.

My dependencies:

    ...
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-plugin-api</artifactId>
        <version>3.2.3</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven.plugin-tools</groupId>
        <artifactId>maven-plugin-annotations</artifactId>
        <version>3.3</version>
    </dependency>
    ...

      

When I use:

@Parameter (property = "resources")
protected String resources;

      

Resources

are stored as null and when I change it to:

 /**
 * @parameter expression="${resources}"
 */
protected String resources;

      

Resources

are performed. I am executing my plugin like:

mvn example:goal -Dresources=whatever

      

And this is my Mojo expression:

@Mojo(name = "example", defaultPhase = LifecyclePhase.PROCESS_RESOURCES)
public class ExampleMojo extends AbstractMojo {

      

Any ideas why this is happening and what I need to do to get this annotation to work as expected?

+3


source to share


1 answer


Well, I had two problems. One reason for me and one known bug is resolved in a newer version of mvn than the one installed here.

First problem caused by me: actually my Mojo expression was as follows:

/**
 * my goal
 *
 * @goal example
 * @phase process-sources
 */
@Mojo(name = "example", defaultPhase = LifecyclePhase.PROCESS_RESOURCES)
public class ExampleMojo extends AbstractMojo {

      

This made my plugin work because of the comments from @goal and @phase. So I thought @Mojo was doing the job, but I was wrong.

The second problem is a known bug: http://jira.codehaus.org/browse/MNG-5346



There are several solutions such as adding maven-plugin-plugin dependency and multiple handles to mojo pom. But I decided to upgrade my maven to 3.2.3 and remove the annotated comments (@goal and @phase) and everything started working as expected.

Now my mojo looks like this:

@Mojo(name = "example", defaultPhase = LifecyclePhase.PROCESS_RESOURCES)
public class ExampleMojo extends AbstractMojo {    
    @Parameter(property = "resources")
    protected String resources;

    /**
     * do something nice
     * @throws MojoExecutionException
     */
    public void execute() throws MojoExecutionException {
        System.out.println(resources);
    }
}

      

And for the sake of completeness, this is my pom:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.package</groupId>
    <artifactId>example</artifactId>
    <packaging>maven-plugin</packaging>
    <version>0.1-SNAPSHOT</version>
    <name>Maven Mojo</name>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <encoding>UTF-8</encoding>
                    <showDeprecation>true</showDeprecation>
                    <compilerArgument>-Xlint:all,-serial</compilerArgument>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-plugin-api</artifactId>
            <version>3.2.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugin-tools</groupId>
            <artifactId>maven-plugin-annotations</artifactId>
            <version>3.3</version>
        </dependency>
    </dependencies>
</project>

      

+3


source







All Articles