Reage
Reage

Reputation: 83

Call Gradle task with parameters inside other task

I have a Gradle task which looks like this (it collects inputs from user and then passes them to the second task):

task A() {
    doFirst {
        ant.input(message: "Version:", addproperty: 'version')
        tasks.b "-Pversion=${ant.properties.version}" //attempt to invoke task B()
    }
}

The second task just print the passed variables:

task B() {
    doFirst {
        println "Version: ${version}"
    }
}

The problem is, that the task A does not invoke task B. I've tried with finalizedBy and doLast but I couldn't find a solution how to pass arguments while executing the task inside the other task. When I execute the task B via command line like:

gradle B -Pversion=1.0.0

it works fine, and params are passed correctly.

Upvotes: 3

Views: 384

Answers (1)

aSemy
aSemy

Reputation: 7139

It's not possible to 'invoke' task B from task A - Gradle tasks are not like functions.

The only way to pass data from one task to another is if

  1. the first task saves the variables to a file,
  2. then the second task reads the file produced by the first task.

There is an example in the docs, which you can adjust to capture user input

abstract class Producer extends DefaultTask {
    @OutputFile
    abstract RegularFileProperty getOutputFile()

    @TaskAction
    void produce() {
        // capture user input
        ant.input(message: "Version:", addproperty: 'version')
        String message = ant.properties.version
        // save the user input to a file
        def output = outputFile.get().asFile
        output.text = message
        logger.quiet("Wrote '${message}' to ${output}")
    }
}

Upvotes: 1

Related Questions