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

I apologize in advance for my ignorance. I am very new to gradle.

My goal is to have some kind of task in my build.gradle file, where the "version.txt" file is created in my project directory whenever I run the t20> terminal command in my project root. This file "version.txt" should contain metadata for the version of the assembly, such as:

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

(^ Editing will be a git hash of the commit HEAD)

I tried to reuse fragments from the following resources, but to no avail, possibly because these resources are deprecated: 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 am using gradle version 2.14 (this is the latest version).

Any help and / or understanding would be greatly appreciated. Thanks!

+25
git file versioning build.gradle gradle
source share
1 answer

The example you are referring to is almost true. With a couple of small settings, 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 versionTxt to get the desired result.

+45
source share

All Articles