从插件设置 Maven 属性
我在这里阅读了一些关于如何从 Maven 插件设置属性的问题(其中大多数讨论了应用程序的版本号)。 似乎没有简单的方法可以做到这一点,我发现的最佳解决方案是拥有一个从插件更新的 filter.properties 文件,并由主 pom 文件使用来过滤所需的资源。
在我从 Maven 文档中阅读此内容后,我尝试了另一个解决方案(Maven 过滤器插件 ):
变量可以包含在您的资源中。这些变量,表示为 通过 ${...} 分隔符,可以来自系统属性,您的 项目属性,来自您的过滤器资源和命令 线。
我发现有趣的是,可以从系统属性中读取变量。因此,我修改了我的插件以设置如下系统属性:
System.setProperty("currentVersion", appCurrentVersion);
但是,过滤后的资源似乎无法读取此值。 有人能告诉我这种方法有什么问题吗?
更新:我正在验证阶段运行我的插件。
多谢。
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.
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?
UPDATE: I'm running my plugin in the validate phase.
Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要将其设置为系统属性,将其设置为 Maven 项目属性
请参阅:
MavenProject
javadoc建议使用
Properties.put()
而不是Properties.setProperty()
进行编辑。虽然从技术上讲,Properties
实现了Map
,但明确不鼓励这种使用在属性 javadoc 中。Don't set it as System Property, set it as Maven Project property
See:
MavenProject
javadocAn edit suggested using
Properties.put()
instead ofProperties.setProperty()
. While technically,Properties
implementsMap
, this usage is discouraged explicitly in the Properties javadoc.Maven 在初始化阶段设置属性。我假设在该阶段 Maven 加载系统属性。之后,maven 不会再次加载系统属性。如果您尝试在此阶段之后添加系统属性,则该属性不会加载。
尝试在验证阶段运行您的插件。
Maven sets properties in initialize phase. I assume that in that phase maven loads system properties. And after that maven doesn't load system properties again. If you try to add a system property after this phase than it's not loaded.
Try to run your plugin in validate phase.