Gradle - Move a folder from ABC to XYZ

AKS picture AKS · Jan 1, 2014 · Viewed 27.3k times · Source

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')
   }
}

Answer

Peter Niederwieser picture Peter Niederwieser · Jan 1, 2014

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.