如何获取当前的maven目标版本

How to get current maven goal version

在 Maven 插件项目中,如果你使用

@Parameter(defaultValue = "${project}")
private MavenProject project;

然后你得到运行这个插件的项目。如何在 Maven 目标本身中获取当前 Maven 目标的版本,更具体地说,是项目的这一部分 pom.xml?

<plugins>
      <plugin>
        <groupId>com.my.maven.plugins</groupId>
        <artifactId>my-maven-plugin</artifactId>
        <version>!! I want to get this !!</version>
      </plugin>
<plugins>

谢谢!

将此添加到您的魔力中:

@Parameter( defaultValue = "${plugin}", readonly = true ) // Maven 3 only
private PluginDescriptor plugin;

可在 https://maven.apache.org/plugin-tools/maven-plugin-tools-annotations/index.html#Supported_Annotations

找到其他可用实例

PluginDescriptor 仅适用于 Maven 3,如果你不想被 Maven 版本限制,你可以尝试添加这个

@Parameter( defaultValue = "${mojoExecution}", readonly = true )
private MojoExecution mojo;

并获取版本

mojo.getPlugin().getVersion()