eastwater
eastwater

Reputation: 5598

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

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?

Upvotes: 2

Views: 3460

Answers (2)

Bjørn Vester
Bjørn Vester

Reputation: 7600

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'])
}

Upvotes: 4

PrasadU
PrasadU

Reputation: 2438

see https://docs.gradle.org/current/userguide/working_with_files.html#filtering_files

import org.apache.tools.ant.filters.ReplaceTokens
task copyFoo2(type: Copy) {
    from 'build/path'
    into "build/copy"
    include "foo.xml", "bar.xml", "hello.xml" 
    filter(ReplaceTokens, tokens: [VERSION: '1.2'])
}

Upvotes: 0

Related Questions