Spring Boot 2 - Change Jar Name

给你一囗甜甜゛ 提交于 2019-12-08 15:05:23

问题


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


回答1:


archiveFileName is the new hotness. Everything else is deprecated.

bootJar {
   archiveFileName = "${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




回答2:


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

bootJar {
   archiveName = 'whatever'
}

Have a look here.




回答3:


Thanks to @AndyWilkinson for the answer!

bootJar {
  baseName "jarName"
  launchScript()
}

.

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



回答4:


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:


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())


来源:https://stackoverflow.com/questions/53123012/spring-boot-2-change-jar-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!