2

I have a such case. I need to save curent date after every release build with gradle. Is there any possibility to save date to gradle.properties file that I can read it in the next build job?

My files: gradle.properties:

version=0.0.1
date=

build.gradle:

task changeDate() {
    file = new File("changelogs/CHANGELOG_RELEASE_FROM_"+getDate()+".md");
    project.setProperty("date",getDate());
}

It dosent work and it doesn't save date variable into gradle.properties.

So I wish that I can have a date from release in my gradle.properties file:

gradle.properties:

version=0.0.1
date=12.04.2019
Anna K
  • 1,666
  • 4
  • 23
  • 47

4 Answers4

5

The methods getProperty, findProperty and setProperty are not directly related to the gradle.properties file. Instead, they provide access to properties in the scope of the Project instance against which the build.gradle gets evaluated. This scope includes a lot of different properties, among them so-called extra properties (coming from the gradle.properties files).

However, Gradle provides a task type for this functionality called WriteProperties. Just specify the target file and define some properties:

task changeDate(type: WriteProperties) {
    outputFile = file('gradle.properties')
    property 'date', getDate()
}
Lukas Körfer
  • 13,515
  • 7
  • 46
  • 62
  • 1
    This doesn't preserve existing properties in the file – Mark Jan 06 '22 at 20:39
  • 1
    @Mark You may try to [read](https://stackoverflow.com/questions/37101589/how-to-read-a-properties-files-and-use-the-values-in-project-gradle-script) the existing properties in a `doFirst` closure and then add them using the method [`properties(...)`](https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/WriteProperties.html#properties-java.util.Map-). – Lukas Körfer Jan 06 '22 at 23:05
2

You can try to do something like:

import java.time.*;

task rel {
    doLast {
        ant.propertyfile(file: "gradle.properties") {
            entry( key: "date", value: LocalDateTime.now())
        }
    }
}

Suppose, rel is your release task or any other task, which execution means, that you release was made. You have to add to it's configuration a doLast closure to run some code after task is executed. In this closure you are modifying some property in properties file.

LocalDateTime and it's import are added just for example, you can use another method to get current date fo sure.

In your case it could look like:

task changeDate() {
    doLast {
        ant.propertyfile(file: "gradle.properties") {
            entry( key: "date", value: LocalDateTime.now())
        }
    }
}

But you have to make your changeDate executed somehow, if it's not.

Stanislav
  • 27,441
  • 9
  • 87
  • 82
0

In addition to the Answer of Lukas I would like to add an example in kotlin (because I struggled with the same issue):

tasks.register<WriteProperties>("changeDate") {
    val propertyFile = project.file("gradle.properties")
    outputFile = propertyFile

    doFirst {
        val existingProperties = java.util.Properties()
        propertyFile.bufferedReader().use { existingProperties.load(it) }
        existingProperties.entries.forEach {
            property(it.key as String, it.value)
        }
        property("date", getDate())
    }
}
0

For me the solution was to write the following task:

tasks.register('writeCompileDatePropertiesFile') {
    def propertiesFile = file('src/main/resources/compileDate.properties')

    doLast {
        
        def compileDate = new Date().format('dd.MM.yyyy HH:mm:ss')
        def projectVersion = project.version

        propertiesFile.parentFile.mkdirs()
        propertiesFile.createNewFile()
        propertiesFile.withWriter { writer ->
            writer << "compileDate=${compileDate}\n"
            writer << "version=${projectVersion}"
        }
    }
}
Matthias Kuhn
  • 170
  • 2
  • 5