Files

Copy Task

We can define what files should be copied, excluded, included and where these should end up.

task copyMyFiles(type: Copy) {
   from 'myfiles'
   into 'build/copied'
   exclude '**/*test*'
   include '**/*.sh'
}

More complex copy task

We can configure copy in many ways. We can include many directories, files and do many manipulations in single task.

def dataContent = copySpec {
     from 'src/data'
     include '*.data'
 }

task initConfig(type: Copy) {
   from('src/main/config') {
       include '**/*.properties'
       include '**/*.xml'
       filter(ReplaceTokens, tokens: [version: '2.3.1'])
   }
   from('src/main/config') {
       exclude '**/*.properties', '**/*.xml'
   }
   from('src/main/languages') {
       rename 'EN_US_(.*)', '$1'
   }
   into 'build/target/config'
   exclude '**/*.bak'

   includeEmptyDirs = false

   with dataContent

   rename { fileName ->
       "production-file${(fileName - 'file-template')}"
  }
}

Templates

Copy and transform a file. expand method uses SimpleTemplateEngine from Groovy.

While config can look like this (it is a template).

Filtering file content

We can filter content of copied file, line by line.

File by file

We can do operations for each file we are coping.

Files

The files() method returns collection of files based on input parameters.

FileTree

Iterates through directory structure and collects the files as it traverses through the tree structure.

FileCollection operations

FileCollections are lazily evaluated. We can contact or subtract file collections.

Source sets

Last updated

Was this helpful?