Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional Gradle properties

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.

like image 784
Vad1mo Avatar asked Dec 26 '14 16:12

Vad1mo


Video Answer


1 Answers

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") ?: ""
like image 57
Mark Vieira Avatar answered Dec 26 '22 07:12

Mark Vieira