Spring Boot 2 - Change Jar Name

2019-06-16 10:09发布

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

4条回答
倾城 Initia
2楼-- · 2019-06-16 10:50

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

bootJar {
   archiveName = 'whatever'
}

Have a look here.

查看更多
我命由我不由天
3楼-- · 2019-06-16 10:57

Thanks to @AndyWilkinson for the answer!

bootJar {
  baseName "jarName"
  launchScript()
}

.

springBoot {
  buildInfo {
    properties {
      group = "groupName"
      name = "projectName"
      version = "1.0"
    }
  }
}
查看更多
Root(大扎)
4楼-- · 2019-06-16 11:04

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.

查看更多
5楼-- · 2019-06-16 11:12

archiveFileName is the new hotness. Everything else is deprecated.

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

Additionally, baseName and extention say they are deprecated, but their replacements, archiveBaseName and archiveExtention currently print out gibberish for me in gradle 5.1.

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

查看更多
登录 后发表回答