简体   繁体   中英

Gradle: getting properties from other project

Is it possible to define an extra property in project A and have it visible in project B ? The root projects obviously includes both.

I tried putting this in project A 's build.gradle:

ext {
  myProps = 'something to say'
}

And this in project B 's build.gradle:

task('X', dependsOn: [':A:someTask']){
  println(project('A').myProps)
}

but I get:

FAILURE: Build failed with an exception.

...

* What went wrong:
A problem occurred evaluating project ':B'.
> Could not find property 'myProps' on project ':A'.

How can I achieve this?

An extra property is accessible from anywhere the owning object ( A 's Project object in this case) is accessible from. However, it isn't considered good style to reach out into the project model of a sibling project. One reason is that this can make it necessary to tweak the configuration order of projects, but there are others. Instead, it's better to either declare the extra property in a common parent project, or in a script plugin that gets applied to all projects that need to access the extra property.

PS: In the same vein, an explicit cross-project task dependency should be avoided whenever possible. Also note that your task tries to print the extra property in the configuration phase (rather than the execution phase), which may or may not be what you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM