I need to write a gradle script to auto version my application on every commit. I need to also include the commit hash as a reference in the application for testers.
I created a Gradle plugin to do this for you. The project and full instructions are at https://github.com/lessthanoptimal/gversion-plugin
To use it add the following to your build.gradle file
plugins {
id "com.peterabeles.gversion" version "1.2.4"
}
gversion {
srcDir = "src/main/java/"
classPackage = "com.your.package"
className = "MyVersion" // optional. If not specified GVersion is used
dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'" // optional. This is the default
timeZone = "UTC" // optional. UTC is default
}
Now you just need to run the gradle task 'createVersionFile' to create the file. You might want to consider adding the following line to your gradle project project.compileJava.dependsOn(createVersionFile) This will cause it to generate the file every time Gradle builds the project. See the website above for Android instructions.
Here's what the file looks like
/**
* Automatically generated file containing build version information.
*/
public class MyVersion {
public static final String MAVEN_GROUP = "com.your";
public static final String MAVEN_NAME = "project_name";
public static final String VERSION = "1.0-SNAPSHOT";
public static final int GIT_REVISION = 56;
public static final String GIT_SHA = "a0e41dd1a068d184009227083fa6ae276ef1846a";
public static final String BUILD_DATE = "2018-04-11T12:19:03Z";
public static final long BUILD_UNIX_TIME = 1523449143116L;
}
You might also want to add the version file to your .gitignore since it is autogenerated and you don't want it in git.