Pergunta

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?

Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top