从插件设置Maven属性 [英] Set maven property from plugin

查看:124
本文介绍了从插件设置Maven属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里阅读了一些有关如何通过maven插件设置属性的问题(其中大多数是关于应用程序的版本号的). 似乎没有简单的方法可以做到这一点,我发现最好的解决方案是拥有一个filter.properties文件,该文件从插件进行更新,并由主pom文件用来过滤所需的资源.

I've read some questions here about how to set a property (most of them talked about the version number for an application) from a maven plugin. It seems there's no easy way of doing this and the best solution I found is to have a filter.properties file which is updated from the plugin and used by the main pom file to filter the desired resources.

从Maven文档( Maven过滤器插件):

I tried another solution after I read this from the Maven documentation (Maven filter plugin):

变量可以包含在您的资源中.这些变量表示为 由$ {...}分隔符组成,可以来自系统属性 过滤器资源和命令中的项目属性 线.

Variables can be included in your resources. These variables, denoted by the ${...} delimiters, can come from the system properties, your project properties, from your filter resources and from the command line.

我发现有趣的是,可以从系统属性中读取变量.因此,我修改了插件以设置系统属性,如下所示:

I found interesting that variabled can be read from system properties. So, I modified my plugin to set a system property like this:

System.setProperty("currentVersion", appCurrentVersion);

但是,过滤后的资源似乎无法读取该值. 有人可以告诉我这种方法有什么问题吗?

However, filtered resources don't seem to read this value. Could anybody tell me what's wrong with this approach?

更新:我正在 validate 阶段运行我的插件.

UPDATE: I'm running my plugin in the validate phase.

非常感谢.

推荐答案

不要将其设置为系统属性,而是将其设置为Maven Project属性

Don't set it as System Property, set it as Maven Project property

// inject the project
@Parameter(defaultValue = "${project}")
private org.apache.maven.project.MavenProject project;

// and in execute(), use it:
project.getProperties().setProperty("currentVersion", appCurrentVersion);

请参阅:

  • Mojo Developer Cookbook
  • MavenProject javadoc

这篇关于从插件设置Maven属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆