Gradle build runs tasks that should not run

hi, i have a gradle task that executes some php script:

task uploadRelease(type:Exec) {
    doFirst {
       //do some stuff
    }
      //run the php script - username and password are arguments that come from command line
    workingDir projectDir
    commandLine "php", "$projectDir/myscript.php", username, password, version
}

i run this task with:

gradle uploadRelease -Pusername=user -Ppassword=pass

however, when i run

gradle build

the above task also runs and it fails:

* What went wrong:
A problem occurred evaluating project ':my-proj'.
> Could not find property 'username' on task ':my-proj:uploadRelease'.

how do i pass arguments to my uploadRelease task and not failing on gradle build?

thanks

Your ‘uploadRelease’ task isn’t running when you run ‘build’. It’s being configured. This is just a consequence of the Gradle build lifecycle.

You have a few options: 1. Make your task fail gracefully by checking for username/password properties before trying to get them and default to something reasonable. 2. Put your username/password in your ~/.gradle/gradle.properties. That way it’ll always be defined. This is what usually happens on CI servers and the gradle.properties file has appropriate permissions. 3. Make a custom task that basically does #1. This is usually what you go to once your task gets beyond just setting simple properties.

Advanced usage: If you took the 1st or last option and your ‘uploadRelease’ task depended on other tasks, it might take awhile before ‘uploadRelease’ actually runs. In that case, if you forget to set the username/password, you’d like it to fail early (before executing anything). To do that, you’d have to hook into the Gradle TaskGraph, check if your ‘uploadRelease’ task was going to be executed and then check that the username and password are defined.