Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

define global variables and functions in build.gradle

Tags:

Is there a way to define global variables in build.gradle and make them accessible from everywhere.

I mean something like this

def variable = new Variable()  def method(Project proj) {     def value = variable.value } 

Because that way it tells me that it cannot find property. Also I'd like to do the same for the methods. I mean something like this

def methodA() {} def methodB() { methodA() } 
like image 352
lapots Avatar asked Dec 29 '15 15:12

lapots


People also ask

How do you define a global variable in a function?

Variables that are created outside of a function (as in all of the examples above) are known as global variables. Global variables can be used by everyone, both inside of functions and outside.

What is the function of the Gradle?

gradle. Gradle is a build system (open source) that is used to automate building, testing, deployment, etc. “build.


1 Answers

Use extra properties.

ext.propA = 'propAValue' ext.propB = propA println "$propA, $propB"  def PrintAllProps(){   def propC = propA   println "$propA, $propB, $propC" }  task(runmethod) << { PrintAllProps() } 

Running runmethod prints:

gradle runmethod propAValue, propAValue :runmethod propAValue, propAValue, propAValue 

Read more about Gradle Extra Properties here.

You should be able to call functions from functions without doing anything special:

def PrintMoreProps(){   print 'More Props: '   PrintAllProps() } 

results in:

More Props: propAValue, propAValue, propAValue 
like image 126
RaGe Avatar answered Oct 23 '22 17:10

RaGe