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

时间:2016-04-28 17:37:55

标签: java maven plugins runtime maven-plugin

如果你使用

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

谢谢!

2 个答案:

答案 0 :(得分:2)

将此添加到你的mojo:

@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

找到

答案 1 :(得分:0)

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

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

并获取版本

mojo.getPlugin().getVersion()