无法在Maven插件上使用注释设置参数 [英] Cannot set parameter using annotations on a maven plugin

查看:109
本文介绍了无法在Maven插件上使用注释设置参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一个Maven插件,当我使用@Parameter注释时,它不起作用.

I'm trying to develop a maven plugin and it does not work when I use @Parameter annotation.

我的依赖项:

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

资源保留为空,当我用以下方法更改时:

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

这是我的Mojo声明:

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?

推荐答案

好,我有两个问题.我的一个原因和一个比此处安装的版本更新的mvn版本中解决的已知错误.

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.

首先由我引起的问题:实际上我的Mojo声明是这样的:

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 {

由于使用@goal和@phase进行注释,这使我的插件正常工作.所以我以为@Mojo在做这项工作,但是我错了.

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.

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

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

有一些解决方案,例如向mojo的pom添加maven-plugin-plugin依赖关系和一些描述符.但是我选择将Maven更新到3.2.3,并删除了带注释的注释(@goal和@phase),一切都按预期开始工作.

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.

现在我的魔力像这样:

@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:

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>

这篇关于无法在Maven插件上使用注释设置参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆