通过build.gradle任务在项目目录中创建version.txt文件 [英] create version.txt file in project dir via build.gradle task

查看:437
本文介绍了通过build.gradle任务在项目目录中创建version.txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对我的无知事先道歉。



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

版本:1.0
版本:1z7g30jFHYjl42L9fh0pqzmsQkF
Buildtime:2016-06-14 07:16:37 EST
应用程序名称:foobarbaz应用程序



( ^修正将是HEAD的git提交散列)



我试着重复使用以下资源中的片段,但无济于事,可能是因为这些资源不存在的日期:
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



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

任何帮助和/或洞察力将非常感激d。谢谢!

解决方案

你提到的例子几乎是正确的。通过一些小调整,它可以按预期工作:

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

plugins {
idorg.ajoberstar.grgitversion1.5.1-rc.3
}

version = 1.0

任务versionTxt(){
doLast {
新文件($ projectDir / version.txt)。text =
版本:$ version
版本:$ {Grgit.open(dir:'。')。head()。id}
Buildtime:$ {new SimpleDateFormat(dd-MM-yyyy HH :mm:ss)。format(new Date())}
应用程序名称:foobarbaz应用程序

}
}

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


Apologies in advance for my ignorance. I'm very new to 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 gradle terminal command in my project root. This 'version.txt' file needs to contain version metadata of the build, such as:

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)

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.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).

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

解决方案

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.5.1-rc.3"
}

version = 1.0

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

Run gradle versionTxt to get the desired output.

这篇关于通过build.gradle任务在项目目录中创建version.txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆