2

Hi i'm trying to dynamically create and configure task based on plugin extension values, problem seems to be evaluation order, is there any way to work around it?

apply plugin: SetupPlugin 

setup {
     destDir = 'some directory set per project in build.gradle'
     sourceFile = 'some file set per project in build.gradle' 
}


class PluginExtension {
    String destDir
    String sourceFile
}

class SetupPlugin implements Plugin<Project> {
    def placeholders

    void apply(Project project) {
        project.extensions.create("setup", PluginExtension)

        project.task ("setupEnvironment", type: Copy) {
            doFirst() {
                //computes placeholders <-- project.setup has value here
            }

            into (project.setup.destDir){ //<-- project.setup is null
                from project.setup.sourceFile
            }
            filter(org.apache.tools.ant.filters.ReplaceTokens, tokens: placeholders)
        }   
    }
}
4

3 回答 3

1

将此移动到 doFirst 块可能会导致一些副作用,因为 gradle up to date 任务可能会遇到问题,因为在执行时而不是配置时重新配置复制任务的参数。一个可以解决问题的快速修复是通过使用闭包来推迟评估:

...
void apply(Project project) {
        project.extensions.create("setup", PluginExtension)

        project.task ("setupEnvironment", type: Copy) {
            doFirst() {
                //computes placeholders <-- project.setup has value here
            }

            into {project.setup.destDir}
            from { project.setup.sourceFile }
            filter(org.apache.tools.ant.filters.ReplaceTokens, tokens: placeholders)
        }   
}
...

希望有帮助!

干杯,勒内

于 2013-04-05T14:33:09.220 回答
0

原来我问了一个我认为与这个问题非常相似的问题。我可能会遗漏一个细微的区别,但如果它有帮助:就是这样。

于 2013-04-25T18:50:02.277 回答
0

这是因为apply在应用设置设置之前调用。

它之所以有效,是doFirst因为它apply在构建期间调用。也许你可以把你的副本包装成doLast?

于 2013-04-05T09:57:35.207 回答