2014-10-28 78 views
3

我正在尝试开发一个maven插件,并且在使用@Parameter注释时它不起作用。无法在maven插件上使用注释设置参数

我的依赖关系:

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

当我使用:

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

资源被保留为空,当我改变它:

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

资源得到满足。我执行我的插件为:

mvn example:goal -Dresources=whatever 

这是我的魔声明:

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

任何想法,为什么发生这种情况和我有什么做的就是这个注解按预期工作?

+0

你能告诉你的全POM文件? – khmarbaise 2014-10-29 07:53:39

+0

嗨@khmarbaise我发现了这个问题。我发布它。感谢您的帮助。 – 2014-10-29 11:30:15

回答

3

那么,我有两个问题。一个原因是我和一个已知的bug在新版本的mvn中解决,而不是在这里安装的。

首先引起我的问​​题:其实我的魔声明是这样的:

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

这让我的插件工作,由于与@goal和@phase的意见。所以我认为@莫乔正在做这项工作,但我错了。

第二个问题是这个已知的错误:http://jira.codehaus.org/browse/MNG-5346

有一些解决方案,比如增加了Maven插件,插件依赖性和几个描述符的魔力的POM。但我选择将我的Maven更新为3.2.3,并删除了注释评论(@goal和@phase),并且所有内容都按预期工作。

现在我的魔力看起来是这样的:

@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); 
    } 
} 

并为完整起见,这是我的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>