dam dam - 6 days ago 6
Git Question

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

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???

uav uav
Answer

Something like

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

Comments