문제

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top