git 通过 build.gradle 任务在项目目录中创建 version.txt 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38275583/
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-09 04:20:42  来源:igfitidea点击:

create version.txt file in project dir via build.gradle task

gitfilegradlebuild.gradleversioning

提问by Chris Covney

Apologies in advance for my ignorance. I'm very new to gradle.

提前为我的无知道歉。我对 gradle 很陌生。

My is goal is to have some task in my build.gradle file, wherein a file 'version.txt' is created in my project directory whenever I run the gradleterminal command in my project root. This 'version.txt' file needs to contain version metadata of the build, such as:

我的目标是在我的 build.gradle 文件中有一些任务,其中每当我gradle在我的项目根目录中运行终端命令时,都会在我的项目目录中创建一个文件“version.txt” 。这个'version.txt'文件需要包含构建的版本元数据,例如:

Version: 1.0 Revision: 1z7g30jFHYjl42L9fh0pqzmsQkF Buildtime: 2016-06-14 07:16:37 EST Application-name: foobarbaz app

Version: 1.0 Revision: 1z7g30jFHYjl42L9fh0pqzmsQkF Buildtime: 2016-06-14 07:16:37 EST Application-name: foobarbaz app

(^Revision would be the git commit hash of the HEAD)

(^Revision 将是 HEAD 的 git commit 哈希)

I've tried to reuse snippets from the following resources, but to no avail, possibly because these resources are out of date: http://mrhaki.blogspot.com/2015/04/gradle-goodness-use-git-commit-id-in.htmlhttp://mrhaki.blogspot.com/2010/10/gradle-goodness-add-incremental-build.html

我尝试重用以下资源中的片段,但无济于事,可能是因为这些资源已过时:http: //mrhaki.blogspot.com/2015/04/gradle-goodness-use-git-commit- id-in.html http://mrhaki.blogspot.com/2010/10/gradle-goodness-add-incremental-build.html

I'm using gradle version 2.14 (which is the latest version).

我正在使用 gradle 2.14 版(这是最新版本)。

Any help and/or insight would be very much appreciated. Thanks!

任何帮助和/或见解将不胜感激。谢谢!

回答by Jk1

The example you're referring to is almost correct. With a couple of minor tweaks it works as expected:

您所指的示例几乎是正确的。通过一些小的调整,它可以按预期工作:

import java.text.SimpleDateFormat
import org.ajoberstar.grgit.Grgit

plugins {
    id "org.ajoberstar.grgit" version "1.7.2"
}

version = 1.0

task versionTxt()  {
    doLast {
        new File(projectDir, "version.txt").text = """
Version: $version
Revision: ${grgit.head().abbreviatedId}
Buildtime: ${new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(new Date())}
Application-name: foobarbaz app
"""
    }
}

Run gradle versionTxtto get the desired output.

运行gradle versionTxt以获得所需的输出。