Search code examples
gradlespringdoc

springdoc-openapi-gradle-plugin hook generating doc after build task in Gradle


I would like to have openapi file generated during build time.

When I configure generateOpenApiDocs to be run after build:

tasks.named("build") {
    finalizedBy("generateOpenApiDocs")
}

I get the following error:

Some problems were found with the configuration of task ':forkedSpringBootRun' (type 'JavaExecFork').
  - Gradle detected a problem with the following location: '/Users/user/projects/service/build/classes/java/aot'.
    
    Reason: Task ':forkedSpringBootRun' uses this output of task ':compileAotJava' without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed.

I tried forkedSpringBootRun/doNotTrackState but it does not work for me - I still get the error mentioned before.

Repository with reproduction - https://github.com/kkocel/openapigradlerepro


Solution

  • TL;DR;

    Additionally configure forkedSpringBootRun in your build script:

    tasks {
      forkedSpringBootRun {
        doNotTrackState("See https://github.com/springdoc/springdoc-openapi-gradle-plugin/issues/102")
      }
    }
    

    If after that Gradle build still fails with the same message you have to define in forkedSpringBootRun all the first level dependent tasks in dependsOn(), e.g.:

    tasks {
        forkedSpringBootRun {
            dependsOn(
                project.tasks.named("distTar"),
                project.tasks.named("test"),
                ..
            )
        }
    }
    

    Known Issue

    The project itself still has open issue regarding issue with the task accessing files from output of another task not having a direct dependency on it.

    Issue reported in the project: https://github.com/springdoc/springdoc-openapi-gradle-plugin/issues/128

    Consequences

    Remember this is only a workaround until plugin properly declared task dependencies. As a result this task won't be optimized and can slightly impact your build performance. Read more about untracked tasks.