Task with a command line argument

I’m looking to have a task that downloads a specified JS file from the web and stores it locally.

However, for that task I require a command line argument, which I simplified in the example I am posting here in the forum.

If I run the command for the task, e.g. /gradlew createJSFile -PtestArgument=testestest everything checks out fine.

However, trying any other gradle command like ./gradlew clean throws off an error, which is the following:

What went wrong:
A problem occurred evaluating root project ‘my-gradle-app’.

Could not get unknown property ‘testArgument’ for task ‘:createJSFile’ of type org.gradle.api.DefaultTask.

What is wrong with my approach? Does this mean I need to declare my property with an empty value? I couldn’t find an example in the official gradle docs dealing with this scenario.

The only examples I found use a project property like the approach I tried

buildscript {

repositories {
    mavenCentral()
    jcenter()
}

}

apply plugin: ‘groovy’
apply plugin: ‘java’

group = ‘my.gradle.app’
version = ‘0.0.1-SNAPSHOT’

task createJSFile {
def currentTimestamp = System.currentTimeMillis()
def fileName = “src/main/resources/angularJS-${currentTimestamp}.js”.toString()
def f = new File(fileName)

Properties props = new Properties()
props.put("manual.file", fileName)
props.store(new FileOutputStream("src/main/resources/resource.properties"), "")
if (testArgument) {
    println "true"
} else {
    println false
}
 if (!f.exists()) {
    new URL("https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular-animate.js").withInputStream{ i -> f.withOutputStream{ it << i }}
 }
}

You might want to try using findProperty('testArgument') instead which will return the property value or null if no such project property has been defined rather than throwing an exception.