[Java] Add time stamp to JAR file name with Gradle

less than 1 minute read

Overview

  • Add time stamp to file name when creating JAR file with Gradle
  • Operating environment: Gradle 6.6 + Groovy 2.5.12

build.gradle

Add date and time character string to project.version in build.gradle file.

plugins {
  id'java'
}

project.version = '1.2.3' +'_' + getTimestamp()

def getTimestamp() {
  // year month date _ hour minute second _ millisecond
  return new Date().format('yyyyMMdd_HHmmss_SSS')
}

Create a JAR file with the jar task or build task.

$ gradle build

You can see that jar is created in build/libs with the following file name.

$ ls build/libs
myapp-1.2.3-20200815_220925_236.jar

Reference materials