Filtering a war file with a copySpec

So, I was looking at this other answer How can I reuse a closure to configure multiple tasks?

And what I’ve tried to do is this:

def webappFilter = project.copySpec {
    filesMatching('**/index.jsp') {
        filter(org.apache.tools.ant.filters.ReplaceTokens, tokens: [
          'version' : version,
        ])
    }
}

war {
    with webappFilter
}

But it doesn’t work, so I guess I’ve misunderstood something along the line. Can anyone point me in the right direction?

CopySpec.with() creates a child spec, which means your filter will only be applied to child spec, not the root spec, which is what you want. You’ll want to call filesMatching() from directly within the war { } block.

Okay, but then how can I do this in a reusable way?

I’m using the gretty plugin so I had hoped to do:

war {
    with webappFilter
}

gretty {
    webappCopy = webappFilter
}

Looks like gretty takes care of this for you.

There’s no need to duplicate CopySpec-specific code in gretty and War task. You can hold all filtering code in gretty.webappCopy. Gretty automatically reconfigures War task with gretty.webappCopy.

http://akhikhl.github.io/gretty-doc/Web-app-filtering.html

D’oh!

Thank you for taking the time to say more than just RTFM :wink: