Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy files into flat directory in Gradle

Tags:

gradle

I'm trying to write a Gradle task to copy specific files from a deep tree into a flat folder.

First Try:

task exportProperties << {
  copy {
    from "."
    into "c:/temp/properties"
    include "**/src/main/resources/i18n/*.properties"
  }
}

This copies the correct files, but does not flatten the structure, so I end up with every single folder from my original project, and most of them are empty.

Second try, based on answers I saw here and here:

task exportProperties << {
  copy {
    from fileTree(".").files
    into "c:/temp/properties"
    include "**/src/main/resources/i18n/*.properties"
  }
}

This time, it is not copying anything.

Third Try:

task exportProperties << {
  copy {
    from fileTree(".").files
    into "c:/temp/properties"
    include "*.properties"
  }
}

Almost works, except it is copying every *.properties file when I only want the files in particular paths.

like image 278
Kip Avatar asked Nov 14 '16 20:11

Kip


3 Answers

I solved the issue in a way similar to this:

task exportProperties << {
  copy {
    into "c:/temp/properties"
    include "**/src/main/resources/i18n/*.properties"

    // Flatten the hierarchy by setting the path
    // of all files to their respective basename
    eachFile {
      path = name
    }

    // Flattening the hierarchy leaves empty directories,
    // do not copy those
    includeEmptyDirs = false
  }
}
like image 197
Julien Ruffin Avatar answered Nov 01 '22 09:11

Julien Ruffin


I got it to work like this:

task exportProperties << {
  copy {
    from fileTree(".").include("**/src/main/resources/i18n/*.properties").files
    into "c:/temp/properties"
  }
}
like image 42
Kip Avatar answered Nov 01 '22 09:11

Kip


You can modify a number of aspects of copied files on the fly by feeding a closure into the Copy.eachFile method including target file path:

copy {
    from 'source_dir'
    into 'dest_dir'
    eachFile { details ->
        details.setRelativePath new RelativePath(true, details.name)
    }
}

This copies all files directly into the specified destination directory, though it also replicates the original directory structure without the files.

like image 8
Andrei LED Avatar answered Nov 01 '22 09:11

Andrei LED