Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically (Java) retrieving the last Git tag for placing version info in software

Tags:

java

git

tags

I've begun versioning my commits with git tag -a v2.3.4 -m 'some message' so that I can label my software releases with version numbers (e.g. 2.3.4)

I'd like to have an "about" menu item in the software where a user could display what version they are running.

Is there a way to do this (preferably in Java) so that my app can "know" its version tag in git???

like image 864
dam Avatar asked Sep 15 '25 05:09

dam


1 Answers

If you have your repo local:

import java.io.*;

public class HelloWorld
{
    public static void main(String []args) throws IOException
    {
        System.out.println("Hello World");
        String command = "git describe --abbrev=0 --tags";
        Process p = Runtime.getRuntime().exec(command);
        BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line; 
        String text = command + "\n";
        System.out.println(text);
        while ((line = input.readLine()) != null)
        {
            text += line;
            System.out.println("Line: " + line);
        }
    }
}

Maybe check with https://www.tutorialspoint.com/compile_java8_online.php


If you have your repo on Github:

https://api.github.com/repos/YOURNAME/YOURREPO/tags

For example:

https://api.github.com/repos/git/git/tags

This will get you the newest 30 Git repo tags.

like image 58
qräbnö Avatar answered Sep 17 '25 03:09

qräbnö