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

2024-03-19

我正在尝试开发一个 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 {

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


嗯,我有两个问题。一个是我造成的,一个是已知错误,在比此处安装的 mvn 版本更新的版本中解决了。

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

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

由于@goal 和@phase 的评论,这使得我的插件可以工作。所以我以为@Mojo 正在做这项工作,但我错了。

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

有一些解决方案,例如向 mojo 的 pom 添加 maven-plugin-plugin 依赖项和一些描述符。但我选择将我的 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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法在 Maven 插件上使用注释设置参数 的相关文章

随机推荐