开发者

Use maven's "version" property in java and nsis code

开发者 https://www.devze.com 2023-04-08 10:18 出处:网络
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

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 开发者_Go百科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?


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

app.version=${project.version}

Then I use this properties file in the code to get the version.


pom.properties gets built into JAR file (to META-INF/maven/<groupId>/<artifactId>/pom.properties) when the project is packaged up. It looks something like:

#Generated by Maven
#Mon Sep 26 09:03:19 EST 2011
version=1.0-SNAPSHOT
groupId=my.project.group.id
artifactId=my-artifactid

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号