I have a Gradle build file where one of the the tasks is to login into docker. In this task I want that the user/CI provides the parameter docker_username, docker_password and docker_email.
task loginDockerHub(group: "Docker", type:Exec) {
executable "docker"
args "login","-u", docker_username, "-p", docker_password, "-e", docker_email
}
Executing gradle loginDockerHub -Pdocker_username=vad1mo ...
all is working as expected.
But when I execute for example gradle build
I get the error:
Could not find property 'docker_username' on task ':loginDockerHub'.
I would expect this error on executing gradle loginDockerHub
without providing the -P parameter, but not on other tasks that don't access docker_username/password parameters.
How can I have optional parameters for my loginDockerHub
task in Gradle that don't make the parameter mandatory for any other task.
You can check if the property exists and if not return a default.
args "login", "-u", project.hasProperty("docker_username") ? docker_username : ""
Update: Starting with Gradle 2.13 you can simplify this somewhat.
args "login", "-u", project.findProperty("docker_username") ?: ""
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With