使用maven的“版本” java 和 nsis 代码中的属性
我们需要在 java 代码和 NSIS 安装程序脚本中提供 Maven 项目的软件版本号。遵循DRY原则,版本号应该只存储在maven pom中。在 Java 代码和 NSIS 脚本中获取此版本号的最佳方法是什么?当然,版本号的更新应该被分发,而开发人员不必关心它。
当前的方法:只要需要版本号,就插入 ${"versionNr"} 作为替代。然后,在maven构建阶段,所有java和NSIS源文件都会被过滤,并且key会被版本号替换。为了避免更改签入的源代码,过滤后的文件实际上被复制到不在 scm 内的其他位置。原始源和由 Maven 过滤的源会导致很多混乱,我想避免这种情况。
有什么提示吗?
we require the software version number of a maven project both in the java code and in the NSIS installer script. Following the DRY principle, the version number should be stored in the maven pom only. What is the best way to get this version number in the Java code as well as in the NSIS script? Updates on the version number should of course be distributed without the developer having to care about it.
The current approach: Wherever the version number is needed, ${"versionNr"} is inserted as a substitute. Then, during the maven build phase, all java and NSIS source files are filtered and the key is replaced by the version number. To avoid changes in the checked in source code, the filtered files are actually copied to a different location not within the scm. Having the original source and the source filtered by maven causes a lot of confusion, which I would like to avoid.
Any hints?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通常将版本参数(如 ${project.version})放在属性文件中,并且仅对 Maven 构建中的该文件应用过滤。就像
然后我在代码中使用这个属性文件来获取版本。
I typically put the version parameter (like ${project.version}) in a properties-file and only apply filtering on that one file in the maven build. Like
Then I use this properties file in the code to get the version.
当项目打包时,
pom.properties
被内置到 JAR 文件中(到META-INF/maven///pom.properties
) 。它看起来像:您可以将其作为 Java 代码中的资源读取,并使用 Property API 读取版本。
不确定 NSIS 脚本是否可以读取属性文件,但根据 NSIS 插件的源代码 它创建了一些 !define,包括 PROJECT_VERSION 获取项目直接来自 POM 的版本。也许你可以用这个。
pom.properties
gets built into JAR file (toMETA-INF/maven/<groupId>/<artifactId>/pom.properties
) when the project is packaged up. It looks something like:You could read this as a resource in your Java code, and use Property API to read the version out.
Not sure whether NSIS scripts can read property files, but according to the source code of the NSIS plugin it creates a few !defines, including PROJECT_VERSION which gets the project version straight from the POM. Maybe you can use this.