'Dependency to generated classes

My multi-module project is structured like that:

Root project
|
|- MyRestApi
|
|- MyApplication

MyRestApi generates classes, which are added as a source set:

sourceSets {
    main {
        java.srcDirs += "${buildDir}/api/src/main/java/"
    }
}

MyApplication has the dependency to this code generation:

compileJava.dependsOn(':MyRestApi:generateJavaApi')

And also the dependency:

dependencies {
    implementation project(':MyRestApi')
    ....
}

So, now the problem is that the build of the root project fails, because MyApplication does not find the dependency. I suspect that the timing is wrong although the sequence of the build looks fine.

When running the builds of the submodules separately, it works. Running the root project a second time works as well.

I think it is not fast enough to find the generated classes during the build. Has anybody experienced this as well?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source