Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you pass variables between different gradle scripts?

I have 2 gradle scripts:

build.gradle and other.gradle

In my build.gradle I have: apply from: 'other.gradle'

task callotherscript << {
  thevar = "Thevariableiwanttogetsomeplaceelse"
  dosomecommand    

In my other.gradle I have:

task dosomecommand(type: Exec) {
  executable "someexe"
  args "aa", "<myarg>" + <thevar>, "<intomydir>"
}

My question is: How do I get "thevar" from build.gradle so I can use it in other.gradle.

I run: gradle callotherscript

The error message I am seeing is:

Could not find property 'thevar' on task ':dosomecommand'.

I have looked through the cookbook, and every gradle doc I thought was relevant, and I am just not seeing how to do it.

Thanks

like image 629
user1819471 Avatar asked Jun 07 '13 21:06

user1819471


People also ask

How do you pass a variable in Gradle?

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 you pass command line arguments in build Gradle?

The Gradle command-line parser will add all non-option arguments as tasks to be executed to the build. So if we want to pass an extra argument to our build script via the command-line we must use the option -P or --project-prop . With this option we can set a project property and then use it in the build script.

How do I set environment variables in Gradle?

If you are using an IDE, go to run, edit configurations, gradle, select gradle task and update the environment variables. See the picture below. Alternatively, if you are executing gradle commands using terminal, just type 'export KEY=VALUE', and your job is done.

What is Buildscript in Gradle?

buildscript: This block is used to configure the repositories and dependencies for Gradle. Note: Don't include dependencies here. ( those will be included in the module-level build.gradle) dependencies: This block in buildscript is used to configure dependencies that the Gradle needs to build during the project. Java.


1 Answers

You can declare an extra property, for example on project:

ext.foo = "bar"

Scripts applied further down can now access the property as foo.

It's important to understand that tasks don't call other tasks but depend on other tasks.

like image 128
Peter Niederwieser Avatar answered Oct 20 '22 09:10

Peter Niederwieser