Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle task to rename file

I want to do a simple file rename in a gradle task. I have a jar called project-1.5.jar under the folder src and I want the jar to be renamed to just project.jar

So,

project/project-1.5.jar to project/project.jar using gradle

Any ideas are much appreciated.

like image 258
George Cimpoies Avatar asked Mar 21 '18 09:03

George Cimpoies


People also ask

How do I 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 Ziptree 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.

What is ProcessResources in Gradle?

Class ProcessResourcesCopies resources from their source to their target directory, potentially processing them. Makes sure no stale resources remain in the target directory.


2 Answers

For me this worked - does not leave source files (no duplications).

task pdfDistributions(type: Sync) {
    from('build/asciidoc/pdf/docs/asciidoc/')
    into('build/asciidoc/pdf/docs/asciidoc/')
    include '*.pdf'
    rename { String filename ->
        filename.replace(".pdf", "-${project.version}.pdf")
    }
}
like image 144
Krzysztof Wesołowski Avatar answered Sep 30 '22 21:09

Krzysztof Wesołowski


Gradle allows to call ant tasks from your task implementation. It makes moving files as easy as

ant.move(file:'oldFile.txt', tofile:'newfile.txt')
like image 37
dpolivaev Avatar answered Sep 30 '22 22:09

dpolivaev