How to use parameter in gradle copy program in destination folder?

Given the following task in gradle, the developer will launch a new module:

gradle initModule -PmoduleName=derp

task initModule(type: Copy) {
    description "Initialize an empty module based on the template. Usage: gradle initModule -P moduleName=derp"

    onlyIf { project.hasProperty("moduleName") }

    from "$rootDir/modules/template"
    into "$rootDir/modules/$moduleName"
}    

I cannot start gradle since the module name is not defined during the evaluation, although I was hoping that only "onlyIf" would do this.

To solve this problem, I assign it to a locally defined variable in the protection block:

def modName = ""

if (!project.hasProperty("moduleName")) {
    logger.error("Invalid moduleName : It can't be")
} else {
    modName = moduleName
}

and finally, use the new variable to survive the configuration phase.

Is this the right way to do this? He just doesn't feel good.

Also, if I used the rule to make it more elegant:

tasks.addRule("Pattern: initModule_<mod name>") { String taskName ->

    if (taskName.startsWith("initModule_")) {
        def params = taskName.split('_');
        task(taskName)  {    
            doLast {
                project.ext.moduleName = params.tail().head()
                tasks.initModule.execute()
            }    
        }
    }
}

The module name is not passed (even if I change it to finalizedBy).

Any help is appreciated.

+4
source share
1 answer

, .

,

into "$rootDir/modules/" + project.property("moduleName")

into "$rootDir/modules/$moduleName"
+1

All Articles