Search code examples
spring-bootgradlejarjava-15

How to append timestamp to jar in spring boot gradle project?


How to append a timestamp to jar in the spring boot Gradle project?

./gradlew build

Creates jar app-0.0.1-SNAPSHOT.jar, how to create jar with timestamp

app-0.0.1-SNAPSHOT-2020-11-09.02-30.jar

-------- Gradle

plugins {
    id 'org.springframework.boot' version '2.3.5.RELEASE'
    id 'io.spring.dependency-management' version '1.0.10.RELEASE'
    id 'java'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}

test {
    useJUnitPlatform()
}

Solution

  • How about

    bootJar {
        archiveFileName = "app-0.0.1-SNAPSHOT-"+new Date().format('yyyy-MM-dd.HH-mm')+ ".jar"
    }
    

    Tested with gradle 6.6.1 and spring boot plugin version 2.3.4