求问,关于 maven 自定义插件的问题,很奇怪的问题,不知道是 maven 本身的 bug,还是我的 bug?

2023-04-13 14:05:50 +08:00
 huyangq

是关于获取 pom.xml 中配置参数(configuration)的。

Mojo 代码如下:

@Mojo(name = "git-version", defaultPhase = LifecyclePhase.GENERATE_SOURCES)
public class GitVersionMojo extends AbstractMojo {
    @Parameter(name = "skipGitCheck", defaultValue = "false")
    private boolean skipGitCheck;
    
    @Parameter(name = "repoDir")
    private String repoDir;
    
    @Parameter(name = "person")
    private Person person;
    
    @Parameter(name = "aa")
    private String aa;
    
     @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        getLog().info("skipGitCheck=" + skipGitCheck);
		getLog().info("repoDir=" + repoDir);
        getLog().info("person=" + person);
        getLog().info("aa=" + aa);
    }
}
public class Person {
    private int age;
    private String name;
    //get set 方法省略
}

问题来了:

如果我将<configuration>以及里头的标签放在<execution>外面,作为全局的配置,无论是单独执行命令还是使用生命周期的mvn compile都能正确输出:

<plugin>
    <groupId>com.jungle</groupId>
    <artifactId>git-version-maven-plugin
    </artifactId>
    <version>1.0.2</version>
    <configuration>
        <skipGitCheck>true</skipGitCheck>
        <repoDir>www</repoDir>
        <person>
            <age>10</age>
            <name>tom</name>
        </person>
        <aa>hello</aa>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>git-version</goal>
            </goals>
        </execution>
    </executions>
</plugin>
[INFO] skipGitCheck=true
[INFO] repoDir=www
[INFO] person=Person{age=10, name='tom'}
[INFO] aa=hello

如果将<configuration>以及里头的标签放在<execution>里面,因为我默认绑定了生命周期是 generate-sources ,所以如果我执行生命周期的操作的时候,比如直接mvn compile,那么日志中也能正确输出

<plugin>
    <groupId>com.jungle</groupId>
    <artifactId>git-version-maven-plugin
    </artifactId>
    <version>1.0.2</version>
    <executions>
        <execution>
            <goals>
                <goal>git-version</goal>
            </goals>
            <configuration>
                <skipGitCheck>true</skipGitCheck>
                <repoDir>www</repoDir>
                <person>
                    <age>10</age>
                    <name>tom</name>
                </person>
                <aa>hello</aa>
            </configuration>
        </execution>
    </executions>
</plugin>

如果将<configuration>以及里头的标签放在<execution>里面中,当我单独执行该 goal 的时候,却输出的是 null。单独执行的命令为:

mvn com.jungle:git-version-maven-plugin:1.0.2:git-version

如果直接在 IDEA 的 Plugins 中执行点击该插件执行,本质与命令行一样,输出的也是null

645 次点击
所在节点    Java
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/932232

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX