How to pass command line parameters to Exec type tasks


I need to run a 3rd party utility through my build script. The utility requires 2 arguments and each of them needs to be in special format. I’m already passing in various info through command line via -P option. When I try to put together all those options and pass it to the utility as it’s arguments, it doesn’t recognize it. I believe that’s because the task is Exec type and it gets evaluated when the parameters are not yet available. Not sure, if this is clear, but here’s what I have:

task runExportTool(type: Exec) {
    if (project.hasProperty('arg1') && project.hasProperty('arg2')) {
         commandLine "exportTool.exe"
         args = [arg1, arg2]
    else {
        logger.lifecycle "one or more property is missing"

In my case, even though I was running gradle with “-Parg1=val1 -Parg2=val2”, the actual tool that task was supposed to execute wasn’t running. I keep getting into the else statement.

If I have both arg1 and arg2 defined in a file, then it’s okay. I understand that the task gets evaluated first when gradle is started and so those parameters needs to be defined first. But, I don’t want to have these properties defined in a property file with a blank/null value because they are dynamic. I want to run "gradle runExportTool -Parg1=“val1” -Parg2=“val2”

What am I missing in my example above?