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

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


回答:

要解决这个问题,你可以在 <plugin> 标签内设置一个全局的 <configuration>,然后在 <execution> 内部覆盖(如果需要)这些全局配置。这样,无论是在命令行中单独执行该 goal,还是在生命周期中执行,都将使用正确的配置。

<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>

<!-- 如果需要,可以在这里覆盖全局配置 -->

<!-- <configuration> -->

<!-- </configuration> -->

</execution>

</executions>

</plugin>

使用这种配置方式,无论是在生命周期中执行还是单独执行 goal,都将使用正确的配置。

以上是 求问,关于 maven 自定义插件的问题,很奇怪的问题,不知道是 maven 本身的 bug,还是我的 bug? 的全部内容, 来源链接: utcz.com/p/945099.html

回到顶部