首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >关于我的自定义生成器,我该如何告诉傲慢的伪君子?

关于我的自定义生成器,我该如何告诉傲慢的伪君子?
EN

Stack Overflow用户
提问于 2016-04-22 15:34:20
回答 1查看 11.9K关注 0票数 7

我试图在现有JAX生成器的基础上制作一个自定义的swagger代码生成器。我按照Swagger页面上的说明,使用命令java -jar GitHub meta -o output/myLibrary -n ABCCodegen -p com.abc.codegen生成了一个模块。我创建了我需要的模板,并更新了AbcCodegenGenerator.java文件。

当我试图运行代码时,我遇到了问题。命令java -cp -cp io.swagger.codegen.Codegen -l ABCCodegen -o ./test使我无法找到或加载主类io.swagger.codegen.Codegen。在查看了上的帖子之后,我尝试运行命令java -cp ABCCodegen- Swagger -codegen-1.0.jar -jar swagger-codegen-cli.jar langs。但是,我的自定义模块abc没有出现在列表中。在java类中,我是否需要以编程方式做一些事情来告诉codegen有关我的生成器的信息?

这是我的班级AbcCodegenGenerator.java:

代码语言:javascript
复制
package com.abc.codegen;



import io.swagger.codegen.*;
import io.swagger.models.Operation;
import io.swagger.codegen.languages.*;

import java.util.*;
import java.io.File;



public class AbcCodegenGenerator extends AbstractJavaJAXRSServerCodegen {
 public AbcCodegenGenerator(){
  super();

  sourceFolder = "src/main/java";
        invokerPackage = "io.swagger.api";
        artifactId = "com.abc";
        outputFolder = "generated-code/ABCCodegen";

        modelTemplateFiles.put("model.mustache", ".java"); 

        //Classes for the API
        apiTemplateFiles.put("api.mustache", ".java");
        apiTemplateFiles.put("apiService.mustache", ".java");
        apiTemplateFiles.put("apiServiceImpl.mustache", ".java");
        apiTemplateFiles.put("apiServiceFactory.mustache", ".java");
        apiPackage = "io.swagger.api";

        additionalProperties.put("title", title);

        //The location templates will be read from
        templateDir = "src/main/resources/ABCCodegen";



        //Adds ABCCodegen to the CliOptions list??
        CliOption library = new CliOption(CodegenConstants.LIBRARY, "library template (sub-template) to use");
        library.setDefault(DEFAULT_LIBRARY);

        Map<String, String> supportedLibraries = new LinkedHashMap<String, String>();
        supportedLibraries.put(DEFAULT_LIBRARY, "abc");
        library.setEnum(supportedLibraries);

        cliOptions.add(library);
        cliOptions.add(new CliOption(CodegenConstants.IMPL_FOLDER, CodegenConstants.IMPL_FOLDER_DESC));
        cliOptions.add(new CliOption("title", "a title describing the application"));
 }

 @Override
    public String getName()
    {
        return "abc"; 
    }

    @Override
    public String getHelp()
    {
        return "Generates a ABC Server application based on Jersey framework.";
    }

    @Override
    public void postProcessModelProperty(CodegenModel model, CodegenProperty property) {
        super.postProcessModelProperty(model, property);
        if("null".equals(property.example)) {
            property.example = null;
        }
    }

    @Override
    public void processOpts() {
        super.processOpts();

        if ( additionalProperties.containsKey(CodegenConstants.IMPL_FOLDER) ) {
            implFolder = (String) additionalProperties.get(CodegenConstants.IMPL_FOLDER);
        }

        supportingFiles.clear();
        writeOptional(outputFolder, new SupportingFile("pom.mustache", "", "pom.xml"));
        writeOptional(outputFolder, new SupportingFile("README.mustache", "", "README.md"));
        supportingFiles.add(new SupportingFile("ApiException.mustache", (sourceFolder + '/' + apiPackage).replace(".", "/"), "ApiException.java"));
        supportingFiles.add(new SupportingFile("ApiOriginFilter.mustache", (sourceFolder + '/' + apiPackage).replace(".", "/"), "ApiOriginFilter.java"));
        supportingFiles.add(new SupportingFile("ApiResponseMessage.mustache", (sourceFolder + '/' + apiPackage).replace(".", "/"), "ApiResponseMessage.java"));
        supportingFiles.add(new SupportingFile("NotFoundException.mustache", (sourceFolder + '/' + apiPackage).replace(".", "/"), "NotFoundException.java"));
        supportingFiles.add(new SupportingFile("jacksonJsonProvider.mustache", (sourceFolder + '/' + apiPackage).replace(".", "/"), "JacksonJsonProvider.java"));
        supportingFiles.add(new SupportingFile("BadRequestException.mustache", (sourceFolder + '/' + apiPackage).replace(".", "/"), "BadRequestException.java"));
        supportingFiles.add(new SupportingFile("JavaRestResourceUtil.mustache", (sourceFolder + '/' + apiPackage).replace(".", "/"), "JavaRestResourceUtil.java"));

        writeOptional(outputFolder, new SupportingFile("bootstrap.mustache", (implFolder + '/' + apiPackage).replace(".", "/"), "Bootstrap.java"));

        writeOptional(outputFolder, new SupportingFile("web.mustache", ("src/main/webapp/WEB-INF"), "web.xml"));
        writeOptional(outputFolder, new SupportingFile("index.mustache", ("src/main/webapp"), "index.html"));

        writeOptional(outputFolder, new SupportingFile("log4j.mustache", ("conf"), "log4j.properties"));

        writeOptional(outputFolder, new SupportingFile("logback.mustache", ("src/main/resources"), "logback.xml"));

        if ( additionalProperties.containsKey("dateLibrary") ) {
            setDateLibrary(additionalProperties.get("dateLibrary").toString());
            additionalProperties.put(dateLibrary, "true");
        }

        if ( "joda".equals(dateLibrary) ) {
            supportingFiles.add(new SupportingFile("JodaDateTimeProvider.mustache", (sourceFolder + '/' + apiPackage).replace(".", "/"), "JodaDateTimeProvider.java"));
            supportingFiles.add(new SupportingFile("JodaLocalDateProvider.mustache", (sourceFolder + '/' + apiPackage).replace(".", "/"), "JodaLocalDateProvider.java"));
        } else if ( "java8".equals(dateLibrary) ) {
            supportingFiles.add(new SupportingFile("LocalDateTimeProvider.mustache", (sourceFolder + '/' + apiPackage).replace(".", "/"), "LocalDateTimeProvider.java"));
            supportingFiles.add(new SupportingFile("LocalDateProvider.mustache", (sourceFolder + '/' + apiPackage).replace(".", "/"), "LocalDateProvider.java"));
        }
    }

    @Override
    public void addOperationToGroup(String tag, String resourcePath, Operation operation, CodegenOperation co, Map<String, List<CodegenOperation>> operations) {
        String basePath = resourcePath;
        if (basePath.startsWith("/")) {
            basePath = basePath.substring(1);
        }
        int pos = basePath.indexOf("/");
        if (pos > 0) {
            basePath = basePath.substring(0, pos);
        }

        if (basePath == "") {
            basePath = "default";
        } else {
            if (co.path.startsWith("/" + basePath)) {
                co.path = co.path.substring(("/" + basePath).length());
            }
            co.subresourceOperation = !co.path.isEmpty();
        }
        List<CodegenOperation> opList = operations.get(basePath);
        if (opList == null) {
            opList = new ArrayList<CodegenOperation>();
            operations.put(basePath, opList);
        }
        opList.add(co);
        co.baseName = basePath;
    }

    public void hideGenerationTimestamp(boolean hideGenerationTimestamp) {
        this.hideGenerationTimestamp = hideGenerationTimestamp;
    }

}

提前感谢您的帮助!

詹妮弗

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-24 23:12:10

这就是java加载类的方式,特别是使用SPI (服务提供者接口)。

这里有文件:

https://github.com/swagger-api/swagger-codegen#making-your-own-codegen-modules

问题是您需要同时指定库和codegen jar,并告诉它要运行哪个类。例如:

代码语言:javascript
复制
# assuming your library is under my-codegen/myLibrary/target

java -cp my-codegen/myLibrary/target/myClientCodegen-swagger-codegen-1.0.0.jar \
    modules/swagger-codegen-cli/target/swagger-codegen-cli.jar \
    io.swagger.codegen.SwaggerCodegen generate \
    -l com.my.company.codegen.MyclientcodegenGenerator \
    -o foo \
    -i http://petstore.swagger.io/v2/swagger.json

这将将码元库添加到类路径、自定义模板库、执行主函数(io.swagger.codegen.SwaggerCodegen)并作为目标传入类。

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36797952

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档