2016-11-29 109 views
1

我開始我的版本提交使用Git標籤-a v2.3.4 -m「一些信息」 ,這樣我可以標我的軟件版本與版本數字(例如2.3.4)編程(JAVA)獲取最後的Git標籤放置版本信息軟件

我想在軟件中有一個「約」菜單項,用戶可以顯示它們正在運行的版本。

有沒有辦法做到這一點(最好在Java中),以便我的應用程序可以「知道」其在git中的版本標籤?

+0

你的意思是在你的構建腳本或實時應用程序中有這個嗎?前者會容易得多 - 只需將其設置爲構建參數即可。 –

回答

0

如果你有你的回購本地:

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); 
     } 
    } 
} 

也許與https://www.tutorialspoint.com/compile_java8_online.php


檢查,如果你有你在Github上回購:

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

例如:

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

這將爲您帶來最新的30個Git回購標籤。

+0

謝謝。只要使用git命令的Runtime.exec在同一個跨平臺上工作,就可以實現這一點。 – dam

+0

剛剛意識到我的生產環境不在git管理之下,所以命令不會在我的開發環境以外的任何地方工作。我想知道是否可以在我的生產環境中執行一個空克隆,以便命令可以在那裏運行。 – dam

+0

你使用GitHub嗎? –