Search code examples
gradlepluginsgradlew

Passing parameter to custom Gradle task


I've written simple custom Gradle task that extends DefaultTask and does some action and I would like to pass it some parameter(s) using command line. At the bottom is code for adding task to list of available tasks and "implementation" of the task.

Now, when I execute: ./gradlew customTask -PcustomParam="value" how can I retrieve customParam value in doAction method?

project.tasks.create("customTask", CustomTask::class.java

open class CustomTask : DefaultTask() {
  @TaskAction
  fun doAction() {
    // retrieve passed parameter
  }
}

Solution

  • if (project.hasProperty('customParam')) {
        println project.property('customParam')
    }
    

    @see project.property(String name)