问题
I'm trying to develop a maven plugin and it does not 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 kept as null, and when I change it with:
/**
* @parameter expression="${resources}"
*/
protected String resources;
resources get fulfilled. I execute my plugin as:
mvn example:goal -Dresources=whatever
And this is my Mojo declaration:
@Mojo(name = "example", defaultPhase = LifecyclePhase.PROCESS_RESOURCES)
public class ExampleMojo extends AbstractMojo {
Any ideas why does this happens and what do I have to do to get this annotation working as expected?
回答1:
Well, I had two problems. One cause by me and one a known bug solved in a newer version of mvn than the one installed here.
First the problem caused by me: Actually my Mojo declaration was this:
/**
* my goal
*
* @goal example
* @phase process-sources
*/
@Mojo(name = "example", defaultPhase = LifecyclePhase.PROCESS_RESOURCES)
public class ExampleMojo extends AbstractMojo {
This made my plugin work due to the comments with @goal and @phase. SO I thought @Mojo was doing the job, but I was wrong.
The second issue is this known bug: http://jira.codehaus.org/browse/MNG-5346
There are a few solutions, like adding maven-plugin-plugin dependency and a few descriptors to the mojo's pom. But I chose to update my maven to 3.2.3 and removed the annotated comments (@goal and @phase) and everything started to work 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>
来源:https://stackoverflow.com/questions/26620165/cannot-set-parameter-using-annotations-on-a-maven-plugin