Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot 2 - Change Jar Name

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

like image 737
Brandon Dudek Avatar asked Nov 02 '18 17:11

Brandon Dudek


People also ask

How do I rename a jar in gradle?

If you want to rename the JAR, source JAR and javadoc JAR with a single setting, while still including the classifier and version, set archiveBaseName at the top level. This is how it's listed in the gradle docs.

What is bootJar in gradle?

bootJar on the other hand is a specific task added by Spring Boot Gradle plugin that, when the java plugin is present, attaches itself to the assemble lifecycle task. The assemble task is automatically configured to depend upon the bootJar task so running assemble (or build ) will also run the bootJar task.

What is JAR file in spring boot?

Spring Boot's “JarFile” Class. The core class used to support loading nested jars is org.springframework.boot.loader.jar.JarFile . It lets you load jar content from a standard jar file or from nested child jar data.


2 Answers

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:

  • https://docs.gradle.org/current/dsl/org.gradle.api.tasks.bundling.Jar.html#org.gradle.api.tasks.bundling.Jar:archiveName

  • https://docs.gradle.org/current/userguide/lazy_configuration.html

like image 98
Planky Avatar answered Sep 21 '22 12:09

Planky


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

bootJar {    archiveName = 'whatever' } 

Have a look here.

like image 28
Opal Avatar answered Sep 21 '22 12:09

Opal