I'm trying to in my gradle script, after creating the bootJar for a spring app, copy and rename the jar that was created to a new name (which will be used in a Dockerfile). I'm missing how to rename the file (I don't want to have the version in docker version of the output file).
bootJar {
baseName = 'kcentral-app'
version = version
}
task buildForDocker(type: Copy){
from bootJar
into 'build/libs/docker'
}
With the Gradle copy task we can define renaming rules for the files that are copied. We use the rename() method of the copy task to define the naming rules. We can use a closure where the filename is the argument of the closure. The name we return from the closure is the new name of copied file.
A FileTree represents a hierarchy of files. It extends FileCollection to add hierarchy query and manipulation methods. You typically use a FileTree to represent files to copy or the contents of an archive. You can obtain a FileTree instance using Project.
You could directly generate the jar with your expected name, instead of renaming it after it has been generated, using archiveName
property from bootJar
extension:
bootJar {
archiveName "kcentral-app.jar" // <- this overrides the generated jar filename
baseName = 'kcentral-app'
version = version
}
EDIT
If you need to keep the origin jar filename (containing version), then you can update your buildForDocker
task definition as follows:
task buildForDocker(type: Copy){
from bootJar
into 'build/libs/docker'
rename { String fileName ->
// a simple way is to remove the "-$version" from the jar filename
// but you can customize the filename replacement rule as you wish.
fileName.replace("-$project.version", "")
}
}
For more information, see Gradle Copy Task DSL
You can also do something like this:
task copyLogConfDebug(type: Copy){
group = 'local'
description = 'copy logging conf at level debug to WEB-INF/classes'
from "www/WEB-INF/conf/log4j2.debug.xml"
into "www/WEB-INF/classes/"
rename ('log4j2.debug.xml','log4j2.xml')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With