简体   繁体   English

无法在maven插件上使用注释设置参数

[英]Cannot set parameter using annotations on a maven plugin

I'm trying to develop a maven plugin and it does not work when I use @Parameter annotation. 我正在尝试开发一个maven插件,当我使用@Parameter注释时它不起作用。

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: 资源保持为null,当我更改它时:

 /**
 * @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宣言:

@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? 任何想法为什么会发生这种情况,我需要做些什么来使这个注释按预期工作?

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. 一个原因是我和一个已知的bug在更新版本的mvn中解决了,而不是在这里安装的版本。

First the problem caused by me: Actually my Mojo declaration was this: 首先是我造成的问题:其实我的Mojo声明是这样的:

/**
 * 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. 由于@goal和@phase的评论,这使我的插件工作。 SO I thought @Mojo was doing the job, but I was wrong. 所以我认为@Mojo正在做这项工作,但我错了。

The second issue is this known bug: http://jira.codehaus.org/browse/MNG-5346 第二个问题是这个已知的错误: 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. 有一些解决方案,比如将moven-plugin-plugin依赖项和一些描述符添加到mojo的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. 但我选择将我的maven更新为3.2.3并删除带注释的注释(@goal和@phase),一切都按预期开始工作。

Now my mojo looks like this: 现在我的mojo看起来像这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM