theAnonymous
theAnonymous

Reputation: 1804

how to set gradle build with version info from git automatically?

inside project build.gradle

allprojects{
    version '' //how to set this?
}

How to set the version to current git's branch name?

Upvotes: 2

Views: 6618

Answers (2)

Mr Orange
Mr Orange

Reputation: 301

You can also use plain git CLI commands. It will be more complecated but without additional libs.

allprojects {
    def gitBranchName = { ->
        def stdout = new ByteArrayOutputStream()
        exec {
            commandLine 'git', 'branch', '--show-current'
            standardOutput = stdout
        }
        return stdout.toString().trim()
    }

    def gitHash = { ->
        def stdout = new ByteArrayOutputStream()
        exec {
            commandLine 'git', 'rev-parse', '--short', 'HEAD'
            standardOutput = stdout
        }
        return stdout.toString().trim()
    }

    version("$gitBranchName" + "_" + "$gitHash")
}

Upvotes: 1

Daniele
Daniele

Reputation: 2837

You can use the palantir-git-version plugin; see the example below. (various other Gradle plugins exist; they are usually based on Jgit).

This will set the version to branch_#[short-git-hash]; if you want to use the branch name only, use version(details.branchName) .


sample

buildscript {
  repositories {
    maven {
      url "https://plugins.gradle.org/m2/"
    }
  }
  dependencies {
    classpath "gradle.plugin.com.palantir.gradle.gitversion:gradle-git-version:0.12.0-rc2"
  }
}

apply plugin: 'base'

allprojects{
  apply plugin: "com.palantir.git-version"

  def details = versionDetails()
  version(details.branchName + "_" + details.gitHash)
  println "version=$version"
}

Upvotes: 2

Related Questions