It should be possible to remove lines using filter(closure)

When copying files using the Copy task, it is possible to filter the files’ content using a filter closure that is called with each line of the source. It is currently not possible to remove a line altogether. I propose that if the closure returns null, the line should be removed.

Did you already try to use ‘FileCopyDetails’ for this yet? Here’s an example:

task myCopyTask(type: Copy) {
  from("yourDir") {
    eachFile { FileCopyDetails details ->
      if(details.file.text.contains("some word")) {
        // add custom code that modifies text
         details.file.text = modifiedText
      }
    }
  }
}

No, I didn’t think of that, thanks. So ‘FileCopyDetails.getFile()’ returns the target file? Anyway, I suggest that if the filter Closure returns ‘null’, the line should be removed.

Correct, ‘FileCopyDetails.getFile()’ is the target file. You could probably also go for one of the Ant filters. This should do the job:

filter(LineContains, negate: true, contains: ['some word'])

This filter class needs to be imported via

import org.apache.tools.ant.filters.LineContains

Thanks, that one looks fine. I’d still like to be able to remove lines with the ‘filter(Closure)’ method, but the workarounds you proposed are very helpful.

Sounds reasonable to me. Would you be interested in working on a pull request for this? If yes, have a look at the Gradle Project & Developer Guidelines. We are excited about every contribution.

I could create a patch if that helps. Sorry, but I don’t intend to go through all the legal/CLA stuff at the moment. I’m still exhausted from the last time I had to do that for ASF.