Sonson123
Sonson123

Reputation: 11427

Store version in Java-Git projects

What is the best way to store the version of my application?

I use Git tagging to store a semantic version number in the Git repository. But I also want to print out the version number to the user of the program (f.ex. when a user calls ./myprogram --version). So I also had to manually change a String constant in my Java source code for every new version.

Is there a smarter way to keep the version numbers in the Git repository and the source code in sync?

Upvotes: 1

Views: 598

Answers (2)

musiKk
musiKk

Reputation: 15189

In our project we use the git-commit-id-plugin for maven. You can generate something that can be read at runtime by your program, e.g., a Java class that contains static fields or a properties file that is read.

If you don't use maven, there surely is a plugin for your build tool or, at the very least, the possibility to somehow call git describe (as suggested by others).

Upvotes: 1

user2298337
user2298337

Reputation:

You can get the current tag from git using

git describe [--abbrev=0] > localFileForTag

If you put that line into a script and run that script during the build process, you can store the current tag in a local file. Your program just has to read and print the file content.

Upvotes: 2

Related Questions