Git 将自定义属性赋予从上一个任务发出的asciidoctor gradle任务

Git 将自定义属性赋予从上一个任务发出的asciidoctor gradle任务,git,gradle,asciidoctor,Git,Gradle,Asciidoctor,我正试图用gradle任务检索我最新的git标记,以便在我的asciidector文档中使用它。即使任务成功,我的AscidActor自定义属性也始终为空。以下是我检索最新git标记的方式: project.ext { latestTag= 'N/A' } task retrieveLatestTag { doLast { new ByteArrayOutputStream().withStream { os -> def result

我正试图用gradle任务检索我最新的git标记,以便在我的asciidector文档中使用它。即使任务成功,我的AscidActor自定义属性也始终为空。以下是我检索最新git标记的方式:

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

   }
}
现在是我的AscidActor任务:

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”,就像我首先设置的默认值一样,即使我的标记已成功检索。以前有人尝试过这样做吗?

您的问题在于,
asciidoctor
是在配置阶段配置的,因为
setLastStableVersion
是用
doLast
闭包声明的,它是在执行阶段执行的

您没有值的原因是,配置发生在执行之前,并且在配置
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
}

您还可以阅读有关构建生命周期的内容。

谢谢,它的工作方式很有魅力!由于我是gradle的新手,我并不真正了解这些阶段,再次感谢链接。