Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle: Passing variable from one task to another

I want to pass a variable from one task to another, in the same build.gradle file. My first gradle task pulls the last commit message, and I need this message passed to another task. The code is below. Thanks for help in advance.

task gitMsg(type:Exec){     commandLine 'git', 'log', '-1', '--oneline'     standardOutput = new ByteArrayOutputStream()     doLast {        String output = standardOutput.toString()     } } 

I want to pass the variable 'output' into the task below.

task notifyTaskUpcoming << {     def to = System.getProperty("to")     def subj = System.getProperty('subj')      def body = "Hello... "     sendmail(to, subj, body) } 

I want to incorporate the git message into 'body'.

like image 447
crystallinity Avatar asked Nov 24 '15 15:11

crystallinity


People also ask

How do you pass arguments in gradle task?

If the task you want to pass parameters to is of type JavaExec and you are using Gradle 5, for example the application plugin's run task, then you can pass your parameters through the --args=... command line option. For example gradle run --args="foo --bar=true" .

How do I pass a gradle argument in command line?

Types of Input Arguments When we want to pass input arguments from the Gradle CLI, we have two choices: setting system properties with the -D flag. setting project properties with the -P flag.

How do I run a task in gradle?

In the list that opens, select Run Gradle task. In the Select Gradle Task dialog, specify the project and the task that you want to execute before launching the project. You can specify a Gradle linked project or any other Gradle project.

What is EXT in build gradle?

ext is shorthand for project. ext , and is used to define extra properties for the project object. (It's also possible to define extra properties for many other objects.) When reading an extra property, the ext. is omitted (e.g. println project. springVersion or println springVersion ).


2 Answers

I think global properties should be avoided and gradle offers you a nice way to do so by adding properties to a task:

task task1 {      doLast {           task1.ext.variable = "some value"      } }  task task2 {     dependsOn task1     doLast {          println(task1.variable)     } } 
like image 158
Rene Groeschke Avatar answered Oct 09 '22 01:10

Rene Groeschke


You can define an output variable outside of the doLast method, but in script root and then simply use it in another tasks. Just for example:

//the variable is defined within script root def String variable  task task1 << {     //but initialized only in the task's method     variable = "some value" }  task task2 << {     //you can assign a variable to the local one     def body = variable     println(body)      //or simply use the variable itself     println(variable) } task2.dependsOn task1 

Here are 2 tasks defined. Task2 depends on Task1, that means the second will run only after the first one. The variable of String type is declared in build script root and initialized in task1 doLast method (note, << is equals to doLast). Then the variable is initialized, it could be used by any other task.

like image 38
Stanislav Avatar answered Oct 08 '22 23:10

Stanislav