42

I am using Spring Boot 2 in my Gradle project to do a build to jar in Jenkins, and I would like to change the name of that jar file.

By default, Spring Boot 2 used the Gradle property rootProject.name, which can be set in the /settings.gradle file.

However, I would like to change the jar file name, without changing the rootProject.name.

Here are my bootJar and springBoot sections of the build.gradle file:

bootJar {
  launchScript()
}

.

springBoot {
  buildInfo {
    properties {
      artifact = "jarName"
      group = "groupName"
      name = "projectName"
      version = "1.0"
    }
  }
}

Note: artifact is not setting the jar name, as I expected it to, after reading: https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/html/#integrating-with-actuator

Brandon Dudek
  • 849
  • 1
  • 8
  • 18
  • 1
    The `artifact` build info property is, by default, derived from the base name of the `bootJar` or `bootWar` task not the other way around. If you configure it explicitly, the base name-based default will no longer be used and the explicit value will be used instead. The base name of the `bootJar` or `bootWar` task will be unaffected. – Andy Wilkinson Nov 02 '18 at 17:47
  • @AndyWilkinson: Thanks! Removing `springBoot.buildInfo.properties.artifact` and adding `bootJar.baseName` worked! – Brandon Dudek Nov 02 '18 at 18:04

8 Answers8

70

archiveFileName is the new hotness. Everything else is deprecated.

bootJar {
   archiveFileName = "${archiveBaseName.get()}.${archiveExtension.get()}"
}

or the Kotlin DSL equivalent:

tasks.getByName<org.springframework.boot.gradle.tasks.bundling.BootJar>("bootJar") {
   this.archiveFileName.set("${archiveBaseName.get()}.${archiveExtension.get()}")
}

See:

checketts
  • 14,167
  • 10
  • 53
  • 82
Planky
  • 3,185
  • 3
  • 29
  • 39
  • I was looking for the secret sauce to manipulate the template. As I am not a seasoned gradle handler, I forgot you have to `get()` the values. This is the answer for those who wish to have FULL control over the build name – hanzo2001 Jan 25 '21 at 15:49
14

Since bootJar tasks extends Jar you can use archiveName to set name the directly:

bootJar {
   archiveName = 'whatever'
}

Have a look here.

Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242
Opal
  • 81,889
  • 28
  • 189
  • 210
  • Thanks. I did try that, as well as `jar{baseName 'whatever'}` and neither worked. (I think those only worked in SpringBoot 1.) – Brandon Dudek Nov 02 '18 at 17:37
  • 1
    This is almost right. The task that's customized needs to be `bootJar` rather than `jar` though. I have edited the answer accordingly. – Andy Wilkinson Nov 02 '18 at 17:47
11

Thanks to @AndyWilkinson for the answer!

bootJar {
  baseName "jarName"
  launchScript()
}

.

springBoot {
  buildInfo {
    properties {
      group = "groupName"
      name = "projectName"
      version = "1.0"
    }
  }
}
Brandon Dudek
  • 849
  • 1
  • 8
  • 18
9

You can also use:

tasks.bootJar {
    archiveFileName.set("app.jar")
}

Or with the jar-plugin

tasks.jar {
    archiveFileName.set("app.jar")
}
mrclrchtr
  • 948
  • 10
  • 14
8

My goal was to remove version from the archive name. I did it this way:

bootJar {
   archiveName = "$baseName.$extension"
}

Now Gradle generates "project-name.jar" instead of "project-name-1.0-SNAPSHOT.jar". This solution is general and doesn't hardcode any particular archive name.

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
  • 4
    The [archiveName](https://docs.gradle.org/current/dsl/org.gradle.api.tasks.bundling.Jar.html#org.gradle.api.tasks.bundling.Jar:archiveName) property is deprecated for some reason, but I found a simplier approach: `bootJar { archiveVersion = null }`. This overrides default value of [archiveVersion](https://docs.gradle.org/current/dsl/org.gradle.api.tasks.bundling.Jar.html#org.gradle.api.tasks.bundling.Jar:archiveVersion) property, which is equal to `project.version` by default. Jar task generates the final `archiveFileName` without version in this case. – Ruslan Stelmachenko Mar 15 '19 at 17:07
8

For Gradle 6

bootJar {
    archiveBaseName = 'freeway-server'
    archiveVersion = '1.0.0'
    archiveFileName = 'freeway-server.jar'
}

For get:-

System.out.print(bootJar.getArchiveBaseName().get())
System.out.print(bootJar.getArchiveVersion().get())
System.out.print(bootJar.getArchiveFileName().get())
Anik Mazumder
  • 188
  • 2
  • 9
1

Most people simply want to not have the version in the jar name, not change the name completely.

tasks.withType<org.springframework.boot.gradle.tasks.bundling.BootJar> {
    archiveVersion.set("")
}

will do it using Kotlin DSL. The final name is given by tasks.bootJar.get().archiveFileName.get().

Abhijit Sarkar
  • 21,927
  • 20
  • 110
  • 219
0

For me worked

project(':org.awseome.subproject') {
 jar() {
     archiveFileName = 'nameOfJar.jar'
 }
}

inside of main build.gradle. Used

Gradle 6.X Spring Boot 2.X

Tristate
  • 1,498
  • 2
  • 18
  • 38