3

I'm quite new to gradle, and I would like to automate the following tasks :

My build.gradle.kts is looking like that:

plugin{
  id("org.springframework.boot") version "2.7.4"
  id("io.spring.dependency-management") version "1.0.14.RELEASE"

  //springdoc-openapi-gradle-plugin
  id("org.springdoc.openapi-gradle-plugin") version "1.4.0"

  //https://github.com/int128/gradle-swagger-generator-plugin
  id("org.hidetake.swagger.generator") version "2.19.2"

  ...
}

repositories {
  mavenCentral()
}

dependencies{
  swaggerCodegen("org.openapitools:openapi-generator-cli:3.3.4")
  ...
}

swaggerSources {
   register("petstore") {
     setInputFile(file("${buildDir}/openapi.json"))
     code.language = "typescript-fetch"
   }
}

// ---!!! the following makes my gradle script fail !!!---
tasks.named("generateSwaggerCodePetstore"){
  dependsOn(tasks.generateOpenApiDocs)
}

My problem is that when I would like to run any gradle task, I get as output:

FAILURE: Build failed with an exception.

  • Where: Build file '[...]/build.gradle.kts' line: 153

  • What went wrong: Task with name 'generateOpenApiDocs' not found in root project 'getmad'.

If I remove the following code from my build script ...

tasks.named("generateSwaggerCodePetstore"){
  dependsOn(tasks.generateOpenApiDocs)
}

... I can easily run these commands:
$ ./gradlew clean generateOpenApiDocs
And then:
$ ./gradlew generateSwaggerCode
But somehow I'm not able to chain these commands neither to write a programatic dependency between theses tasks in my build script.
I'm having a single gradle Project (no subproject yet).

Has anyone a solution to my problem ?

1 Answers1

0

The task generateOpenApiDocs is not configured yet so you can't just access it through tasks' properties. You have to access it in other way:

tasks.named("generateSwaggerCodePetstore") {
    dependsOn(tasks.named("generateOpenApiDocs"))
}

The order will be like:

$ ./gradlew generateSwaggerCodePetstore --dry-run
:app:compileJava SKIPPED
:app:processResources SKIPPED
:app:classes SKIPPED
:app:bootRunMainClassName SKIPPED
:app:forkedSpringBootRun SKIPPED
:app:generateOpenApiDocs SKIPPED
:app:forkedSpringBootStop SKIPPED
:app:generateSwaggerCodePetstore SKIPPED

BUILD SUCCESSFUL in 3s
chehsunliu
  • 1,559
  • 1
  • 12
  • 22
  • Thanks for you answer but it did not work. I still get the same error as before. This is weird because if I add the following task to my script: `tasks.register("printKnownTasks"){ println("All known tasks at configuration time:") println(rootProject.getAllTasks(true))}` The tasks is printed: ./gw pKT All known tasks at configuration time: {root project 'getmad'=[task ':assemble',[...], task ':generateEffectiveLombokConfig', [...] – Jonathan Kaeser Sep 28 '22 at 18:24
  • Do u find the solution ? Got the same problem – Sercurio Mar 08 '23 at 14:36