Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtaining the Build ID in a Java Application

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.

like image 306
Mark Mayo Avatar asked Jan 21 '23 00:01

Mark Mayo


2 Answers

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>
like image 189
shsteimer Avatar answered Jan 26 '23 00:01

shsteimer


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.

like image 26
matt b Avatar answered Jan 25 '23 22:01

matt b