Prasoon
Prasoon

Reputation: 445

Is it possible to throw an error if no arguments are provided to Gradle?

My gradle setup accepts arguments during runtime which are checked in a shell script that Gradle Exec task calls. However, in order to reach that point, Gradle deals with dependencies and spends a good amount of time before the end script is executed, which would then throw an error if no arguments are passed.

Build task in gradle looks like below:

task buildAll(type: Exec) {
   environment['PROJECT_ROOT'] = "${projectDir}"
   workingDir rootProject.projectDir.path
   executable rootProject.projectDir.path + "/script.ksh"
      if (project.hasProperty('ARGS')) {
         args(ARGS.split(','))
      }
}

Gradle is called as follows:

./gradlew build -PARGS="-arg1,value1,-arg2,value2,-arg3,-arg4,value4"

I intend to check for -arg2 and if it is not provided, I would like the gradle build to fail with some sort of usage displayed. Is this possible?

Upvotes: 4

Views: 782

Answers (1)

Opal
Opal

Reputation: 84784

You can add an if block at the very beginning of the script:

if (!project.hasProperty('lol')) {
    throw new GradleException("'lol' property is required!")
}

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'com.google.guava:guava:18.0'
    }
}

however it will not prevent gradle from resolving the dependencies of the script itself - buildscript block will be evaluated first.

Upvotes: 3

Related Questions