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