Call one Gradle Task from another with arguments
Asked Answered
S

3

19

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.

Scratchy answered 30/10, 2015 at 4:9 Comment(0)
P
8

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
Properly answered 30/10, 2015 at 5:14 Comment(5)
There is no native support for calling execution of one task from another in gradle, moreover it's highly discouraged!Sarena
@Sarena you mean, it's possible to use costomeTask.execute()?Properly
Yes, sure. Why not? You can call execute on other task. This is perfectly valid gradle script.Sarena
@Sarena Yes, maybe I was wrong with formulation. But as I know, it's not a advisable way to do it, if you can do it via tasks dependecies.Properly
Of, course it's not. That's what I wrote in my first comment :)Sarena
H
5

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]
Hardhearted answered 15/10, 2021 at 15:29 Comment(1)
I have a similar problem. Maybe you can help me? https://mcmap.net/q/668085/-run-gradle-task-with-command-line-parameters-several-times-from-groovy-script/4167198Knocker
A
0

You can use ext:

task outraTask << {
    printf(arg0)
    printf(arg1)
}

project(':projetc2').tasks.outraTask {
    ext.arg0 = "0"
    ext.arg1 = "1"
}.execute()

output:

> Task :projetc2:outraTask
0
1
Aksum answered 4/1, 2020 at 18:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.