V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
huyangq
V2EX  ›  Java

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

  •  
  •   huyangq · 2023-04-13 14:05:50 +08:00 · 637 次点击
    这是一个创建于 378 天前的主题,其中的信息可能已经有所发展或是发生改变。

    是关于获取 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

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3283 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 12:37 · PVG 20:37 · LAX 05:37 · JFK 08:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.