17

Directory Structure:

Project1/ABC/file1.txt

I want the above ABC folder moved/renamed to XYZ (without leaving ABC there).

How can I do this using Gradle. Seems like in Gradle: For a right hand person, it's itching your right ear using your left hand, taking it across top of your head.

I have used the following example: but it doesn't do anything:

task renABCToXYZ(type: Copy) << {
   copy {
      from "Project1"
      into "Project1"
      include 'ABC'
      rename ('ABC', 'XYZ')
   }
}
AKS
  • 16,482
  • 43
  • 166
  • 258

3 Answers3

28

Your task declaration is incorrectly combining the Copy task type and project.copy method, resulting in a task that has nothing to copy and thus never runs. Besides, Copy isn't the right choice for renaming a directory. There is no Gradle API for renaming, but a bit of Groovy code (leveraging Java's File API) will do. Assuming Project1 is the project directory:

task renABCToXYZ {
    doLast {
        file("ABC").renameTo(file("XYZ"))
    }
}

Looking at the bigger picture, it's probably better to add the renaming logic (i.e. the doLast task action) to the task that produces ABC.

Peter Niederwieser
  • 121,412
  • 21
  • 324
  • 259
  • which is what I'm doing current in doLast. Thanks Pete. – AKS Jan 02 '14 at 19:41
  • Actually I meant, i was doing what you said in the doLast task action. Btw, rename worked like you said above, thanks for that. – AKS Jan 02 '14 at 23:46
  • Just a warning: as javadoc from [`java.io.File.renameTo`](https://docs.oracle.com/javase/7/docs/api/java/io/File.html#renameTo(java.io.File)) says, the method is dependent of underlying system - if Gradle/Groovy uses the JDK class. A better aproach seems to use `ant.move` as told by [Matthias](http://stackoverflow.com/a/26654465/1099452) or [`java.nio.file.Files`](https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html) – lucasvc Jan 05 '17 at 12:47
13

With the solution given above you can rename files and folders, you can move files, but you cannot move folders to another destination with renameTo. For this case you have to use ant.move:

 ant.move(file: sourceDir, tofile: targetDir)

Remark: The question is about renaming folders. So the answer above is correct (but the question is a little bit ambiguous). But maybe my answer is helpfull for other users who stumple upon this question and want move move a folder.

Matthias M
  • 12,906
  • 17
  • 87
  • 116
  • Thanks! I was using renameTo() to move/rename a folder 'ABC' to folder 'XYZ' overwriting the contents of the destination, but it did not overwrite.. but ant.move seems to have worked just fine :) – SamGamgee Feb 29 '16 at 13:24
  • Yes, thanks! I didn't understand why rename wasn't working on a directory until I saw your post. – archsten Apr 05 '17 at 20:50
  • It looks like the `move()` method does not exist in Gradle 5.6.2 – Thunderforge Sep 17 '19 at 20:05
  • For completeness sake, rename will also move a folder if you use the new path + old name as the target argument: `file("x/myDir").renameTo(file("y/myDir"))` moves a directory `myDir` from its parent `x` to the (existing) parent `y`. – Hans Jul 21 '20 at 11:53
3

Following code will move a file from one directory to another and will rename file

task wb764Jar( type: Jar ) {
        doFirst{
            copy {
                from 'deployment/alpha/workbench_alpha7_64.jnlp'
                into 'build/libs/jar_merge/developed/alpha64/JNLP-INF/'
                rename('workbench_alpha7_64.jnlp', 'APPLICATION.JNLP')
            }
        }
        baseName = 'WorkbenchMaster7_64'
        from files(wbLibsDir + '/jar_merge/developed/alpha64/')
        from zipTree("$wbJar.archivePath")
    }
A Jakhar
  • 706
  • 7
  • 8