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

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

-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.

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?

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

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

I use this task as a very very simple subgenerator.

The last hint helped to get it run.

Thanks a lot