Stamping the version number and the build time of an application in a properties file so that it could be displayed by an application at runtime seemed like it should be a pretty straightforward task, although it took a bit of time to find a solution that didn’t require the timestamp, version, or ant-run plugins.
I started with a version.txt file at the default package level in src/main/resources of my project, which looks as follows.
version=${pom.version} build.date=${timestamp}
By default the Maven resources plug-in will not do text substitution (filtering), so it will need to be enabled within the <build> section of the pom.xml file.
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
Maven does actually define a ${maven.build.timestamp} property which in theory could have been used in the version.txt file, rather than the ${timestamp} property, but unfortunately a bug within Maven prevents the ${maven.build.timestamp} property from getting passed to the resource filtering mechanism. (issue: http://jira.codehaus.org/browse/MRESOURCES-99).
The workaround is to create another property within the pom.xml file and set that new property to the timestamp value, in this case, the property name is “timestamp”, which is used above in the version.txt file. The maven.build.timestamp.format is an optional property for (obviously) defining the timestamp format.
<properties> <timestamp>${maven.build.timestamp}</timestamp> <maven.build.timestamp.format>yyyy-MM-dd HH:mm</maven.build.timestamp.format> </properties>
Now, when the build is executed we end up with the version number and build time of the application in the version.txt file.
version=1.0.2-SNAPSHOT build.date=2012-03-16 15:42
twitter: @RobTerp
twitter: @LimitUpTrading
Gotcha – check you aren’t filtering config file properties – see maven docs for ‘excludes’
This was very helpful. I did the following to get the info:
Properties prop = new Properties();
String resource = ClassLoader.getSystemResource(“my/path/to/version.txt”).getPath();
prop.load(new FileInputStream(new File(resource)));
String version = prop.getProperty(“version”);
String timestamp = prop.getProperty(“build.date”);
Sorry, Font get why
version=${pom.version}
build.date=${timestamp}
is not
version=1.0
build.date=1.1.2013
In your version.txt. I would expect ${version} in your pom file?
I didn’t want to have to define version twice in the POM file, once in the POMs header, and another time in the POM properties.
Thank you! This was very helpful for me.
Great, thanks for the feedback!
this was a good guide.!!
Can you tell me where did you specify the version?
The version is specified in the section, usually towards the top of our pom.xml file.
The timestamp part does not work for me :/, I have maven 3.2.3 and I can see that the build process actually put a timestamp in the generated/copied version.txt file but then it replaces it back to whatever placeholder was original used (version). Could you share the whole pom.xml which you have used?
works for me! great this is realy helpful
Worked for me, thank you!
Great. it is very helpful