问题
Does anyone have a simple suggestion for recording a build ID (generated at compile time) which is displayed in the title bar of the app at runtime?
Building from within Eclipse, all I need is the ID, I can then pass it up to the title.
回答1:
If you are using Ant, you can easily set up your "jar" or "package" target so that it generates a file including the current timestamp and include this in your jar
output.
If using Maven, there are a few ways to achieve something similar, such as dropping down to Ant using the antrun plugin.
回答2:
If you're using Maven, especially if you want the build number from SVN (though it can generate unique build numbers for you through a configuration), look at the buildnumber-maven-plugin.
You simply add a snippet similar to the following to your pom.xml file:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.0-beta-3</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>create</goal>
</goals>
</execution>
</executions>
<configuration>
<doCheck>true</doCheck>
<doUpdate>true</doUpdate>
</configuration>
</plugin>
Then use ${buildNumber} later on in your pom to refer to the build id. I use it to write that number to the manifest like so, using the maven-war-plugin.
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
<manifestEntries>
<Implementation-Build>${buildNumber}</Implementation-Build>
</manifestEntries>
</archive>
回答3:
If you want to use a timestamp for the build, you can obtain that from the jar or class modifation dates or the MANIFEST file.
Maven populates a MANIFEST file with the module version number in the jar. You can read this to obtain the version of all the maven modules you are using.
Making the timestamp part of the "unique id" ensures every build has a different id.
回答4:
If you are completely building within Eclipse, you need to create a build action which generates a resource in your source folder with the information you need - a property file will do nicely - which then propagates to your binary output and can be read at runtime after which you can do what you need to show it.
来源:https://stackoverflow.com/questions/4314739/obtaining-the-build-id-in-a-java-application