How to specify conditional Source and Destination directory for Copy task?

Hi,

I want to set the destinationDir at runtime for Copy task.

Here is the scenario I am trying:

I have on text file where I am specifying the list of files. Content of temp.lst :

/*<Directory Name> : <File Name> : <Directory Path> */
  #DIR1: abc.h : /DIR1/inc/
 #DIR1 : pqr.h : /DIR1/inc/
   #DIR2 : xyz.h : /DIR2/inc/

Here I want to copy the abc.h and pqr.h to DIR1/inc, similarly copy the xyz.h to DIR2 . ( Depends on the first string of each line) I am splitting the line with separator as " : " and I want destination Path to be dynamic depends on the first word of each line.

Below is the Task I am using to copy these listed file:

task copyListedFiles (type: Copy) {
    def gdkFile = new File ("${projectDir}/temp.lst")
    if(gdkFile.exists())
    {
            gdkFile.splitEachLine(/\s:\s/) { item ->
            if(!item.isEmpty())
            {
                if(item[0] == "#DIR1")
                {
                    from item[1]
                    def destPath = "${gls_root}/" + item[2]
                     into destPath
                }
                if(item[0] == "#DIR2")
                {
                    from item[1]
                    def destPath = "${gls_root}/" + item[2]
                     into destPath
                }
            }
        }
    }
}

The problem I am seeing here is, it is always coping all listed files in temp.lst to the DIR2 directory.

Could you please help me to resolve this issue.

Please let me know if you need more information.

Thanks Mandar

Each time you call the into() method you are resetting the target destination directory. Only the last call has any effect. This is in contrast to each call to from(), which adds elements to an internal collection.

Take a look at the CopyTask’s usage of DefaultCopySpec:

public DefaultCopySpec into(Object destDir) {

this.destDir = destDir;

return this;

}

public CopySpec from(Object… sourcePaths) {

for (Object sourcePath : sourcePaths) {

this.sourcePaths.add(sourcePath);

}

return this;

}

I don’t think you’ll be able to do this with just one Copy task. I think you’ll need one instance of the task per destination.

Perhaps Task A just determines which files go to which destination, stores that in 2 collections for files headed into DIR1 and DIR2, and then Task B does the copy to DIR1 and Task C does the copy to DIR2. Tasks B and C could depend on Task A. You could even add a Task D which depends on B and C to do the whole thing via running a single task.

1 Like