最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

openapi generator - Extract gradle task in separate file for re-usability - Stack Overflow

programmeradmin7浏览0评论

I know that you can just create a separate .gradle file and add it to the build.gradle with apply from:

But I now have a task based on the openapi-generator which needs an import:

import org.openapitools.generator.gradle.plugin.tasks.GenerateTask;

tasks.register('openApiGenerateJavaServer', GenerateTask) {
    generatorName = 'spring'
}

If I have this task in the build.gradle of my project it works, but when I try to apply it from a different file I get an error:

unable to resolve class
org.openapitools.generator.gradle.plugin.tasks.GenerateTask @ line 1, column 1. import org.openapitools.generator.gradle.plugin.tasks.GenerateTask;

I can't use the default openApiGenerate task as I need multiple generators in this project (server, client and angular-ts)

Is there a solution for this?

I know that you can just create a separate .gradle file and add it to the build.gradle with apply from:

But I now have a task based on the openapi-generator which needs an import:

import org.openapitools.generator.gradle.plugin.tasks.GenerateTask;

tasks.register('openApiGenerateJavaServer', GenerateTask) {
    generatorName = 'spring'
}

If I have this task in the build.gradle of my project it works, but when I try to apply it from a different file I get an error:

unable to resolve class
org.openapitools.generator.gradle.plugin.tasks.GenerateTask @ line 1, column 1. import org.openapitools.generator.gradle.plugin.tasks.GenerateTask;

I can't use the default openApiGenerate task as I need multiple generators in this project (server, client and angular-ts)

Is there a solution for this?

Share Improve this question asked Feb 5 at 9:02 ThomasThomas 7,0905 gold badges36 silver badges75 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You can write a precompiled script plugin instead of using a standalone .gradle file. Then, as this file is itself compiled, it can have a compilation classpath and pick up the dependency you need.

Most straightforwardly this compilation is carried out in the buildSrc project. To do this you can:

  1. Write a build file for your new compilation at buildSrc/build.gradle:

    plugins {
        id 'groovy'
    }
    
    repositories {
        gradlePluginPortal() // To find the dependency
    }
    
    dependencies {
            // Here we add the OpenAPI generator plugin as a dependency
        implementation 'org.openapi.generator:org.openapi.generator.gradle.plugin:7.11.0'
    }
    
  2. Write your precompiled script plugin at (say) buildSrc/src/main/groovy/myPlugin.groovy. This is your existing .gradle file that requires the external dependency.

  3. Then apply your new plugin using the plugins block in your main project's build.gradle file

    plugins {
        id 'myPlugin'
    }
    
  4. Remove the version number from where you apply the OpenAPI generator plugin in your main build file(s). This is because the OpenAPI generator plugin JAR was put on the classpath of the main build in step 1 and Gradle has checks to avoid different versions of the same code being placed on the build classpath.

发布评论

评论列表(0)

  1. 暂无评论