2016-12-02 44 views
2

我試圖檢索我的最新的git標籤與gradle任務爲了使用它在我的asciidoctor文檔。即使我的任務成功,我的asciidoctor自定義屬性始終爲空。這是我如何取回我的最新的git標籤:給自定義屬性asciidoctor gradle任務發出從前面的任務

project.ext { 
    latestTag= 'N/A' 
} 
task retrieveLatestTag { 
    doLast { 

     new ByteArrayOutputStream().withStream { os -> 
      def result = exec { 
       commandLine('git', 'rev-list', '--tags', '--max-count=1') 
       standardOutput = os 
      } 
      ext.latestTagName = os.toString().trim() 
     } 

    } 
} 
task setLastStableVersion { 
    dependsOn retrieveLatestTag 
    doLast { 

     new ByteArrayOutputStream().withStream { os -> 
      def result = exec { 
       commandLine('git', 'describe', '--tags', retrieveLatestTag.latestTagName) 
       standardOutput = os 
      } 
      project.ext.latestTag = os.toString().trim() 
     } 

    } 
} 

喏,這就是我的asciidoctor任務:

asciidoctor { 
    dependsOn setLastStableVersion 
    attributes \ 
    'build-gradle' : file('build.gradle'), 
    'source-highlighter': 'coderay', 
    'imagesdir': 'images', 
    'toc': 'left', 
    'toclevels': '4', 
    'icons': 'font', 
    'setanchors': '', 
    'idprefix': '', 
    'idseparator': '-', 
    'docinfo1': '', 
    'tag': project.latestTag 
} 

我的自定義屬性標籤始終是「N/A」就像我首先設置的默認值即使我的標籤被成功檢索。有沒有人試圖做過這樣的事情?

回答

1

你這裏的問題是這樣的事實,即asciidoctor被配置在配置階段,因爲setLastStableVersiondoLast關閉,這是在執行階段執行的聲明。

你有沒有值的原因是這樣的事實,執行之前的配置情況,當asciidoctor是越來越配置,沒有setLastStableVersion任務,也不retrieveLatestTag被執行。

你不需要有任務去獲得你的git標籤,只需從你的任務中刪除doLast或者更好地將你的邏輯放在任何任務之外,因爲每次配置你的構建時都需要它,順序如下:

new ByteArrayOutputStream().withStream { os -> 
     def result = exec { 
      commandLine('git', 'rev-list', '--tags', '--max-count=1') 
      standardOutput = os 
     } 
     project.ext.latestTagName = os.toString().trim() 
    } 

new ByteArrayOutputStream().withStream { os -> 
     def result = exec { 
      commandLine('git', 'describe', '--tags', latestTagName) 
      standardOutput = os 
     } 
     project.ext.latestTag = os.toString().trim() 
    } 

asciidoctor { 
    dependsOn setLastStableVersion 
    attributes \ 
    'build-gradle' : file('build.gradle'), 
    'source-highlighter': 'coderay', 
    'imagesdir': 'images', 
    'toc': 'left', 
    'toclevels': '4', 
    'icons': 'font', 
    'setanchors': '', 
    'idprefix': '', 
    'idseparator': '-', 
    'docinfo1': '', 
    'tag': project.latestTag 
} 

here你可以閱讀關於構建生命週期。

+0

謝謝,它的作品就像一個魅力!由於我對gradle很陌生,因此我不太瞭解這些階段,再次感謝鏈接。 –