How do I create a copy task able to use properties from commandline in an the expand method?


(Hank Lapidez) #1

I tried to following:

task mycopyTask(type: Copy){
    from 'path/to/my/template/directory'
    into "destination/directory"
    include 'mytemplate'
    rename { file -> newFilename+'.txt' }
 expand(placeholder1: newFilename, placeholder2 : newFilename.toLowerCase())
}

And want to call the task something like this

gradle mycopyTask -PnewFilename=document12

But It doesn’t work.

I tried a different notation

task mycopyTask(type: Copy) << {
 newFilenameLow = newFilename.toLowerCase()
       from 'path/to/my/template/directory'
    into "destination/directory"
    include 'mytemplate'
    rename { file -> newFilename+'.txt' }
 expand(placeholder1: newFilename, placeholder2 : newFilenameLow)
}

but the same call outputs only:

:mycopyTask UP-TO-DATE

Thanks a lot for help and hints


(Peter Niederwieser) #2

-P sets a project property, so you need to access it with ‘project.newFilename’. Unqualified access will work at the outer level but not in a task action.


(Hank Lapidez) #3

Thanks for the hint.

I change the task to the following and the first run was successful

task mycopyTask(type: Copy){
 newFilename =""
 if(project.hasProperty("newFilename")){
  newFilename = project.newFilename
 }
    from 'path/to/my/template/directory'
    into "destination/directory"
    include 'mytemplate'
    rename { file -> newFilename+'.txt' }
 expand(placeholder1: newFilename, placeholder2 : newFilename.toLowerCase())
}

But the second run ended up with the UP-TO-DATE message.

How can I change his behaviour, so that the task will always copy?


(Peter Niederwieser) #4

Why do you want to copy if everything is up-to-date?

Anyway, “mycopyTask.outputs.upToDateWhen { false }” will do the trick.


(Hank Lapidez) #5

I use this task as a very very simple subgenerator.

The last hint helped to get it run.

Thanks a lot