Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call one Gradle Task from another with arguments

I have a gradle task which calls a script and passed the command line arguments to the script using -PARGS.

task taskAll(type: Exec, dependsOn: taskinit) {
    environment['PROJECT_ROOT'] = "${projectDir}"
    workingDir rootProject.projectDir.path
    description = 'Main task'

    executable rootProject.projectDir.path + "/execute.me"
    if (project.hasProperty('ARGS')) {
        args(ARGS.split(','))
    }
}

I call this gradle task with any of the below options

./gradlew taskAll
./gradlew taskAll -PARGS="arg1"
./gradlew taskAll -PARGS="arg2"

However, am looking to see if I split taskAll into multiple tasks, say

./gradlew taskA    #Calls task taskAll with arg1
./gradlew taskB    #Calls task taskAll with arg2

I understand that I will have to replicate the taskAll to create taskA, taskB and remove the "if" condition and hardcode args in each of these.

However, I wonder if it is possible to have a cleaner implementation by having MainTask which only calls the executable, and then have TaskA, TaskB, TaskC call MainTask and pass the arguments arg1, arg2 and arg3.

like image 753
Prasoon Avatar asked Oct 30 '15 04:10

Prasoon


2 Answers

In most cases, executing one task from another is done by configuration of task dependencies, via providing dependsOn and optionally mustRunAfter properies. In your case, it's not possible to use it, since your main task has to be executed after some configuration task. In that case, you can use finalizedBy property of the task.

For your requirements, you can create a number of tasks, which will set some script variable with predefined arguments, just as you need it. And you could leave your main task, which will call something, relying on this arguments. Only thing you need to do, is to make each custom task finilizedBy your main task. So, every call of custom task will execute the main task after excution.

Here is the short example, how to do it:

//define a variable to store arguments
def ARGS = null

//2 custom tasks, which set arguments during the execution phase
task taskA << {
    ARGS = "poperty1,property2"
}

task taskB << {
    ARGS = "property3,property4"
}

//your main task
task mainTask (type: Exec) {
    environment['PROJECT_ROOT'] = "${projectDir}"
    workingDir rootProject.projectDir.path
    description = 'Main task'

    executable rootProject.projectDir.path + "/execute.me"

    //here is the main difference, we moved arguments setting into 
    //execution phase, before execution of this task
    doFirst{
        //if you call custom task it will be executed with predefined params
        if (ARGS != null) {
            args(ARGS)
        //if you call mainTask, you are able to pass arguments via command line with -PCOMMAND_LINE_ARGS=123
        } else if (project.hasProperty('COMMAND_LINE_ARGS')) {
            args(COMMAND_LINE_ARGS)
        } else {
            throw new GradleException("No arguments found")
        }
    }
}

//finilization settings for custom tasks
taskA.finalizedBy mainTask
taskB.finalizedBy mainTask
like image 136
Stanislav Avatar answered Oct 11 '22 09:10

Stanislav


A prettier way to do that with GradleBuild API:

    task ('taskA', type: GradleBuild) {
        startParameter.projectProperties = ['ARGS':'arg1']
        tasks = ['taskAll']
    }
    
    task ('taskB', type: GradleBuild) {
        startParameter.projectProperties = ['ARGS':'arg2']
        tasks = ['taskAll']
    }

You can have complex project properties, for example command line argument -Pmyextension.config=true will become :

startParameter.projectProperties = ['myextension.config':true]

Note that this will erase CLI args. If you need to append it :

startParameter.projectProperties << project.getGradle().getStartParameter().getProjectProperties() << ['myextension.config':true]
like image 20
Karbos 538 Avatar answered Oct 11 '22 09:10

Karbos 538