0

Sorry if this is obvious, I'm new to Gradle and I'd like to include the latest git commit tag in my builds.

So far I have this task that simply outputs the string I want to save.

tasks.register<Exec>("get-git-latest") {
    executable("git")
    args("log", "--oneline", "-1", "--format=format:%h", ".")
}

Ideally I'd like to get this output into a variable that can be reused by other Gradle tasks, what is the best way to do this with Kotlin DSL? Any suggestions are welcome.

aSemy
  • 5,485
  • 2
  • 25
  • 51
G. Tabs
  • 37
  • 4
  • 1
    Does this answer your question? [How to use exec() output in gradle](https://stackoverflow.com/questions/11093223/how-to-use-exec-output-in-gradle) – aSemy Jan 31 '23 at 17:07
  • @aSemy I couldn't quite get the kotlin DSL answer towards the bottom of that question to work. I may be missing some fundamental knowledge about how to use variables in my gradle.build.kts – G. Tabs Jan 31 '23 at 17:35
  • Or, you could use a plugin? https://github.com/n0mer/gradle-git-properties – User51 Jan 31 '23 at 18:13

1 Answers1

0

Upon revisitng the question that @aSemy linked, I was able to get the value into a variable in my Kotlin DSL like so:

val gitLatestCommit: String = ByteArrayOutputStream().use { outputStream ->
    project.exec {
        executable("git")
        args("log", "--oneline", "-1", "--format=format:%h", ".")
        standardOutput = outputStream
    }
    outputStream.toString()
}

I still need to figure out how to inject this into certain property files, but this is a great start. Thank You.

G. Tabs
  • 37
  • 4