Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle, copy and rename file

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'
}
like image 608
gunygoogoo Avatar asked Oct 31 '18 15:10

gunygoogoo


People also ask

How do I copy and rename a file in Gradle?

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.

What is FileTree in Gradle?

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.


2 Answers

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

like image 139
M.Ricciuti Avatar answered Dec 19 '22 21:12

M.Ricciuti


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')
like image 30
jomofrodo Avatar answered Dec 19 '22 20:12

jomofrodo