0

How can I append a timestamp to the jar file name when running the gradle build command?

The jar name I have now:

abc-0.0.1-SNAPSHOT.jar

What I actually want:

abc-0.0.1-SNAPSHOT20180721101136.jar

My build.gradle file:

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.abc'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

I've tried build.date = '${timestamp}', but that doesn't work. I've also tried doFirst, but in vain.

Brandon Essler
  • 710
  • 8
  • 21
user2340345
  • 793
  • 4
  • 16
  • 38

1 Answers1

0

You can easily choose the name of the JAR file yourself by setting the archiveName property of the respective task:

jar {
    archiveName = 'myJarFile.jar'
}

By default, the archiveName will be assembled via the schema [baseName]-[appendix]-[version]-[classifier].[extension] where each part is another property of the respective task. For your specific use case I would suggest to just change the version part of the name:

jar {
    version = project.version + timestamp      // define timestamp in a way you like
}

You may want to put this configuration into a doFirst closure, as the timestamp will be more precise to the time the JAR file was actually created.

Lukas Körfer
  • 13,515
  • 7
  • 46
  • 62
  • is timestamp a function that I need to define? – user2340345 Jul 26 '18 at 11:31
  • A variable, you can set it right before setting the new `version`. I don't know how you want to define the timestamp, you can use any method from the JDK: https://stackoverflow.com/questions/23068676/how-to-get-current-timestamp-in-string-format-in-java-yyyy-mm-dd-hh-mm-ss – Lukas Körfer Jul 26 '18 at 11:44
  • Im doing like this : `def timestamp = new Date().format('yyyyMMddHHmmss') jar { version = '0.0.1-SNAPSHOT' archiveName = 'abc' + version + timestamp }` but it does not append version or timestamp to the jar – user2340345 Jul 27 '18 at 04:58
  • @user2340345 FYI, the terribly troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Feb 03 '19 at 19:32
  • 1
    @user2340345 `String dateTimePortion = OffsetDateTime.now( ZoneOffset.UTC ).truncatedTo( ChronoUnit.SECONDS ).format( DateTimeFormatter.ofPattern( "uuuuMMdd'T'HHmmss" ) ) ;` Include the `T` in the middle to follow the popular standard [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) formats for exchanging date-time values. And consider what wall-clock time you want to use for capturing the current moment (the time zone, or offset-from-UTC) – Usually best to work in UTC. – Basil Bourque Feb 03 '19 at 19:34