Can you reference a configuration within a copy spec filter?


(richard murray) #1

I’m using Gradle 1.1 and I’m attempting to replace a token in a file with a list of Jar’s from a configuration. I have the following within a War task.

from (configurations.warClient) {
    exclude 'amclientsdk.jar'
    into ('/gui/')
}
  doFirst {
                         from ('skeleton') {
        include "**/bpm-webstart.jnlp"
        filter(ReplaceTokens, tokens: ['client_jars': configurations.warClient.collect {
            File file -> file.name}.sort().join(System.lineSeparator()) ])
        into ('/')
    }
}

This filter against configurations.warClient only seems to work properly if I put it into a doFirst block, otherwise some of the JAR’s are missed out.

Am I doing something wrong here? I don’t understand how the JAR’s are resolved correclty in the standard 'from (configurations.warClient) block, but not in a filter.


(Peter Niederwieser) #2

It’s the classical configuration phase vs. execution phase pitfall. Your filtering has to be done in the execution phase (e.g. in a ‘doFirst’). Otherwise, you’ll run into two problems:

  1. The work (which involves resolving a configuration, because you are requesting its files with ‘collect()’) will be done for each and every build, no matter which tasks are invoked.

  2. The configuration might not yet be ready to be resolved (depending on when and how it gets configured/populated).


(richard murray) #3

Thanks Peter. Is there any way this could be made clearer in the docs or a warning output in the build? The build runs OK without the doFirst block but some of the files I expect are missing.

I take it using a filter without the reference to files in a configuration is OK in the configuration phase, the docs seem to show this: http://www.gradle.org/docs/current/userguide/working_with_files.html#sec:copying_files


(Peter Niederwieser) #4

We are constantly making usability improvements, and maybe we’ll be able to catch this one day.

I take it using a filter without the reference to files in a configuration is OK in the configuration phase

Yes. Even using configurations is fine (because they are lazy collections), as long as you don’t explicitly or implicitly request their elements. Examples for lazy operations on ‘Configuration’/‘FileCollection’ are ‘plus’, ‘minus’ and ‘filter’.