从插件设置 Maven 属性

发布于 2024-12-07 02:35:02 字数 614 浏览 3 评论 0原文

我在这里阅读了一些关于如何从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

第七度阳光i 2024-12-14 02:35:02

不要将其设置为系统属性,将其设置为 Maven 项目属性

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

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

请参阅:


建议使用 Properties.put() 而不是 Properties.setProperty() 进行编辑。虽然从技术上讲,Properties 实现了 Map,但明确不鼓励这种使用在属性 javadoc 中。

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

See:


An edit suggested using Properties.put() instead of Properties.setProperty(). While technically, Properties implements Map, this usage is discouraged explicitly in the Properties javadoc.

狼亦尘 2024-12-14 02:35:02

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文