Spring Boot 2 - Change Jar Name
Asked Answered
G

8

45

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

Gasper answered 2/11, 2018 at 17:8 Comment(2)
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.Taegu
@AndyWilkinson: Thanks! Removing springBoot.buildInfo.properties.artifact and adding bootJar.baseName worked!Gasper
G
10

Thanks to @AndyWilkinson for the answer!

bootJar {
  baseName "jarName"
  launchScript()
}

.

springBoot {
  buildInfo {
    properties {
      group = "groupName"
      name = "projectName"
      version = "1.0"
    }
  }
}
Gasper answered 2/11, 2018 at 18:8 Comment(0)
P
73

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:

Peyton answered 7/6, 2019 at 15:43 Comment(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 nameLuzon
Why is the get() necessary? Without it Gradles generate the name with the command output.Christyna
A
14

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

bootJar {
   archiveName = 'whatever'
}

Have a look here.

Anomalism answered 2/11, 2018 at 17:12 Comment(2)
Thanks. I did try that, as well as jar{baseName 'whatever'} and neither worked. (I think those only worked in SpringBoot 1.)Gasper
This is almost right. The task that's customized needs to be bootJar rather than jar though. I have edited the answer accordingly.Taegu
G
10

Thanks to @AndyWilkinson for the answer!

bootJar {
  baseName "jarName"
  launchScript()
}

.

springBoot {
  buildInfo {
    properties {
      group = "groupName"
      name = "projectName"
      version = "1.0"
    }
  }
}
Gasper answered 2/11, 2018 at 18:8 Comment(0)
A
10

You can also use:

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

Or with the jar-plugin

tasks.jar {
    archiveFileName.set("app.jar")
}
Adscititious answered 27/4, 2020 at 14:21 Comment(0)
O
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.

Odious answered 8/3, 2019 at 22:51 Comment(1)
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.Shipworm
R
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())
Redstone answered 13/11, 2019 at 9:3 Comment(0)
L
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().

Lashing answered 25/9, 2020 at 22:35 Comment(0)
P
-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

Parcheesi answered 30/10, 2020 at 8:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.