gradle-git-properties icon indicating copy to clipboard operation
gradle-git-properties copied to clipboard

Use git properties for WAR filename

Open sodevel opened this issue 3 years ago • 3 comments

I am trying to use some git properties as part of the filename of the WAR file created by the war task, however Gradle always complains the property does not exist. I am using lazy GString evaluation to set the archiveFileName property of the war task but that doesn't work in constrast to the example using this approach to insert the information into the manifest.

sodevel avatar Apr 29 '21 22:04 sodevel

After doing some research i found a solution for this issue myself. The culprit is that the git information gets extracted during the execution phase but the war task gets configured during the configuration phase. Following the Gradle suggestions you have to use a separate configuration task to configure the war task:

task configureWar {
    group = "Build"
    description = "Updates WAR archive name with Git properties."

    doLast {
        tasks.war {
             archiveFileName = "${archiveBaseName.get()}##${archiveVersion.get().tokenize('.')*.padLeft(2, '0').join()}_${project.ext.gitProps['git.closest.tag.commit.count'].padLeft(4, '0')}${project.ext.gitProps['git.dirty'].toBoolean() ? 'M' : ''}.${archiveExtension.get()}"
        }
    }
}
configureWar.dependsOn(generateGitProperties)
war.dependsOn(configureWar)

This is kind of cumbersome especially if there are other tasks which require such information during their configuration phase. My SVN hosted projects use a different plugin to extract SVN related information and this plugin extracts the information during the configuration phase, this makes it easier to consume that information.

Maybe this plugin can be changed to query and expose the Git information during the configuration phase and write the properties file only during the execution phase.

sodevel avatar May 03 '21 18:05 sodevel

For Kotlin DSL and Spring Boot:

val gitProps: Map<String, String?> by extra
gitProperties.extProperty = ::gitProps.name

tasks.withType<GenerateGitPropertiesTask> {
    outputs.upToDateWhen { false }
}

val configureBootWar by tasks.registering {
    group = "build"
    description = "Updates war archive name with git commit id."
    dependsOn(tasks.withType<GenerateGitPropertiesTask>())

    doLast {
        tasks.bootWar {
            archiveVersion.set("${archiveVersion.get()}-${gitProps["git.commit.id.abbrev"]}")
        }
    }
}

tasks.bootWar {
    dependsOn(configureBootWar)
}

tzhao11 avatar Nov 04 '22 08:11 tzhao11

Recent versions of the plugin automatically set outputs.upToDateWhen to false if gitProperties.extProperty is used, so you don't have to do this manually. You could also just depend on tasks.generateGitProperties, it's the only existing task that generates the properties.

sodevel avatar Nov 04 '22 18:11 sodevel