如何使用 Gradle 提交/推送 Git 标签?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17910465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 08:50:46  来源:igfitidea点击:

How to commit/push a Git tag with Gradle?

gitgradletagging

提问by rfgamaral

I have created a specific Gradle task that should only be called in the Jenkins build system. I need to make this task depend on another one which should tag the HEAD of the master branch after a successful compilation of the project.

我创建了一个特定的 Gradle 任务,它只能在 Jenkins 构建系统中调用。我需要让这个任务依赖于另一个应该在成功编译项目后标记 master 分支的 HEAD 的任务。

I have no idea how can I commit/push/add tags to a specific branch in remote repository using Gradle. What's the easiest way to achieve this?

我不知道如何使用 Gradle 向远程存储库中的特定分支提交/推送/添加标签。实现这一目标的最简单方法是什么?

Any help is really appreciated...

任何帮助真的很感激......

采纳答案by forvaidya

You can use Exec as pointed in above comment or use JGit to push tag. Create a plugin / class in java and use it gradle

您可以使用上面评论中指出的 Exec 或使用 JGit 推送标签。在java中创建一个插件/类并使用它gradle

回答by Benjamin Muschko

Here's how you can implement your scenario with the Gradle Git plugin. The key is to look at the provided Javadocsof the plugin.

以下是使用Gradle Git 插件实现场景的方法。关键是查看插件提供的Javadoc

buildscript {
   repositories { 
      mavenCentral() 
   }

   dependencies { 
      classpath 'org.ajoberstar:gradle-git:0.6.1'
   }
}

import org.ajoberstar.gradle.git.tasks.GitTag
import org.ajoberstar.gradle.git.tasks.GitPush

ext.yourTag = "REL-${project.version.toString()}"

task createTag(type: GitTag) {
   repoPath = rootDir
   tagName = yourTag
   message = "Application release ${project.version.toString()}"
}

task pushTag(type: GitPush, dependsOn: createTag) {
   namesOrSpecs = [yourTag]
}

回答by Oleksandr Bondarchuk

I love this:

我喜欢这个:

private void createReleaseTag() {
    def tagName = "release/${project.version}"
    ("git tag $tagName").execute()
    ("git push --tags").execute()
}


EDIT: A more extensive version

编辑:更广泛的版本

private void createReleaseTag() {
    def tagName = "release/${version}"
    try {
        runCommands("git", "tag", "-d", tagName)
    } catch (Exception e) {
        println(e.message)
    }
    runCommands("git", "status")
    runCommands("git", "tag", tagName)
}

private String runCommands(String... commands) {
    def process = new ProcessBuilder(commands).redirectErrorStream(true).start()
    process.waitFor()
    def result = ''
    process.inputStream.eachLine { result += it + '\n' }
    def errorResult = process.exitValue() == 0
    if (!errorResult) {
        throw new IllegalStateException(result)
    }
    return result
}

You can handle Exception.

您可以处理异常。