Gradle copy: filesMatching(...) multiple files without a pattern

Viewed 1112

Gradle copy: filesMatching(multiple files) without a pattern. E.g.,

task copyFoo(type: Copy) {

   from ("/path") {
      filesMatching("foo.xml") {
         filter(ReplaceTokens, tokens : [VERSION, '1.2'])
      }

      filesMatching("bar.xml") {
         filter(ReplaceTokens, tokens : [VERSION, '1.2'])
      }

      filesMatching("hello.xml") {
         filter(ReplaceTokens, tokens : [VERSION, '1.2'])
      }
   }

}

Is there a way to merge them? like

      filesMatching("foo.xml" | "hello.xml" | "bar.xml") {
         filter(ReplaceTokens, tokens : [VERSION, '1.2'])
      }

Can the pattern be a regex?

2 Answers

When in doubt about what you can do, head over to the API documentation. Here you will see that the filesMatching method takes a String, which describes an Ant pattern:

CopySpec filesMatching​(String pattern, Action<? super FileCopyDetails> action)

Configure the FileCopyDetails for each file whose path matches the specified Ant-style pattern.

There are no methods that take a regex pattern. But there is an overloaded method that takes an iterable (like a list) of Ant patterns:

CopySpec filesMatching​(Iterable<String> patterns, Action<? super FileCopyDetails> action)

Configure the FileCopyDetails for each file whose path matches any of the specified Ant-style patterns.

This means you can do:

filesMatching(["bar.xml", "hello.xml", "foo.xml"]) {
   filter(ReplaceTokens, tokens : [VERSION: '1.2'])
}
Related