【问题标题】:Cannot set parameter using annotations on a maven plugin无法使用 Maven 插件上的注释设置参数
【发布时间】:2014-12-24 13:20:49
【问题描述】:

我正在尝试开发一个maven插件,当我使用@Parameter注解时它不起作用。

我的依赖:

    ...
    <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 声明:

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

任何想法为什么会发生这种情况,我必须做什么才能让这个注释按预期工作?

【问题讨论】:

  • 你能显示完整的 pom 文件吗?
  • 嗨@khmarbaise 我发现了问题。我正在发布它。感谢您提供帮助。

标签: java maven plugins mojo


【解决方案1】:

嗯,我有两个问题。一个是我的原因,一个是已知的错误,它在比这里安装的新版本的 mvn 中解决了。

首先是我造成的问题:实际上我的 Mojo 声明是这样的:

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

由于带有@goal 和@phase 的cmets,这使我的插件工作。所以我认为@Mojo 正在做这项工作,但我错了。

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

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

现在我的 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);
    }
}

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

【讨论】:

    猜你喜欢
    • 2019-10-20
    • 2012-04-30
    • 2022-10-18
    • 2018-06-03
    • 2016-01-27
    • 1970-01-01
    • 2013-01-16
    • 2013-06-29
    • 1970-01-01
    相关资源
    最近更新 更多