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

2
  • 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. Nov 2, 2018 at 17:47
  • @AndyWilkinson: Thanks! Removing springBoot.buildInfo.properties.artifact and adding bootJar.baseName worked! Nov 2, 2018 at 18:04

8 Answers 8

71

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:

2
  • 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, 2021 at 15:49
  • Why is the get() necessary? Without it Gradles generate the name with the command output.
    – Fábio
    Oct 2 at 13:59
14

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

bootJar {
   archiveName = 'whatever'
}

Have a look here.

2
  • Thanks. I did try that, as well as jar{baseName 'whatever'} and neither worked. (I think those only worked in SpringBoot 1.) Nov 2, 2018 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. Nov 2, 2018 at 17:47
11

Thanks to @AndyWilkinson for the answer!

bootJar {
  baseName "jarName"
  launchScript()
}

.

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

You can also use:

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

Or with the jar-plugin

tasks.jar {
    archiveFileName.set("app.jar")
}
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.

1
  • 4
    The archiveName property is deprecated for some reason, but I found a simplier approach: bootJar { archiveVersion = null }. This overrides default value of archiveVersion property, which is equal to project.version by default. Jar task generates the final archiveFileName without version in this case. Mar 15, 2019 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())
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().

-1

For me worked

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

inside of main build.gradle. Used

Gradle 6.X Spring Boot 2.X

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.