2012-09-26 48 views
0

我正在將一個項目從ant移植到gradle。一切都很好,只有一個問題存在:目前我們有一個基於自定義的基於ant-contrib的任務,用於在repo上執行git命令,以讀取最新的xy-release標籤以獲取版本號(和生成的jar文件的版本,如project- xy.jar)。如我所見,我需要一些插件。可悲的是,通過可用的插件來看,沒有任何東西可以幫助我(存在一些與git相關的插件,但他們的目標是其他的,例如使成爲發行版,因此標記回購,而不是讀取已經制作的標籤)。 所以我需要幫助達到我的目標。我需要在Gradle中發明一切嗎?我知道我可以導入現有的ant構建文件,但我並不想。Gradle:讀取最新版本的xy-release GIT標籤

回答

0

既然git有這個命令,git describe,它應該是非常微不足道的。只需調用git並在輸出中讀取即可。爲了讓剛剛過去的* -release標籤,確切的命令是

git describe --abbrev=0 --match=*-release 

(我不知道它是否是正則表達式或水珠;如果是正則表達式,那就需要.*-release)。如果您的發佈標籤未註釋,請添加--tags

1

或者,只重用現有的Ant任務。無需導入Ant構建。

1

好吧,我試圖在Gradle中實現相同的舊東西。 (因爲我逃離螞蟻/常春藤,所以我不想保留舊東西)。這裏是輸出(可能是跛腳,因爲我是Gradle和Groovy的新手)。有用。

jar { 
    dependsOn configurations.runtime 

    // get the version 
    doFirst { 
     new ByteArrayOutputStream().withStream { execOS -> 
      def result = exec { 
       executable = 'git' 
       args = [ 'describe', '--tags', '--match', '[0-9]*-release', '--dirty=-dirty' ] 
       standardOutput = execOS 
      } 

      // calculate version information 
      def buildVersion = execOS.toString().trim().replaceAll("-release", "") 
      def buildVersionMajor = buildVersion.replaceAll("^(\\d+).*\$", "\$1") 
      def buildVersionMinor = buildVersion.replaceAll("^\\d+\\.(\\d+).*\$", "\$1") 
      def buildVersionRev = buildVersion.replaceAll("^\\d+\\.\\d+\\.(\\d+).*\$", "\$1") 
      def buildTag = buildVersion.replaceAll("^[^-]*-(.*)\$", "\$1").replaceAll("^(.*)-dirty\$", "\$1") 
      def dirty = buildVersion.endsWith("dirty") 

      println("Version: " + buildVersion) 
      println("Major: " + buildVersionMajor) 
      println("Minor: " + buildVersionMinor) 
      println("Revision: " + buildVersionRev) 
      println("Tag: " + buildTag) 
      println("Dirty: " + dirty) 

      // name the jar file 
      version buildVersion 
     } 
    } 

    // include dependencies into jar 
    def classpath = configurations.runtime.collect { it.directory ? it : zipTree(it) } 
    from (classpath) { 
     exclude 'META-INF/**' 
    } 

    // manifest definition 
    manifest { 
     attributes(
      'Main-Class': 'your.fancy.MainClass' 
     ) 
    } 
}