vendredi 30 décembre 2016

Gradle: automatically add a test dependency on a project

In a multiproject build, some projects depend on others, and the latter provide not only compile/runtime libraries, but also useful test libs (such as, for instance, "mock" implementations of the components they provide).

I know of a couple of ways to make the test sources of one project available to another. They are discussed, for instance as answers to this question.

What I am looking for is some magical way to make this happen automatically, so that if a subproject adds a dependency on another subproject, it automatically gets that projects test sources added to the testCompile config.

I tried the naive approach:

configure(rootProject.subprojects.findAll {
  it.configurations.getByName("compile")
    .dependencies.any {  
      it.name == project.name 
    } 
 }) {
  dependencies {
    testCompile project.sourceSets.test.output
  }
}

But this does not work ... presumably, because this code is evaluated "at the wrong stage" (or whatever the correct lingo is), and the other projects don't "know" yet that they depend on this one.

I also tried putting (an equivalent of) this at the end of root build file (hoping, that everything else would already be configured by then), but that did not work either.

Is there a way to do what I am looking for here?

Aucun commentaire:

Enregistrer un commentaire