简体   繁体   English

如何使用swagger-codegen-maven-plugin生成maven存储库就绪库?

[英]How can I generate a maven repository-ready library with swagger-codegen-maven-plugin?

I'm using the Maven plugin swagger-codegen-maven-plugin to generate a Java client jar. 我正在使用Maven插件swagger-codegen-maven-plugin来生成Java客户端jar。 I put my swagger.json in the src/main/resources folder and ran mvn clean install . 我将swagger.json放在src/main/resources文件夹中并运行mvn clean install Here is my pom.xml. 这是我的pom.xml。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>my-java-client</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>my-java-client</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <swagger.version>1.5.13</swagger.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.0</version>
        </dependency>
        <dependency>
            <groupId>com.squareup.okhttp</groupId>
            <artifactId>okhttp</artifactId>
            <version>2.7.5</version>
        </dependency>
        <dependency>
            <groupId>com.squareup.okhttp</groupId>
            <artifactId>logging-interceptor</artifactId>
            <version>2.7.5</version>
        </dependency>
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-codegen</artifactId>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupId>com.squareup.okio</groupId>
            <artifactId>okio</artifactId>
            <version>1.12.0</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>io.swagger</groupId>
                <artifactId>swagger-codegen-maven-plugin</artifactId>
                <version>2.2.2</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>src/main/resources/swagger.json</inputSpec>
                            <modelPackage>mypackage.model</modelPackage>
                            <apiPackage>mypackage</apiPackage>
                            <invokerPackage>mypackage.invoker</invokerPackage>
                            <language>java</language>
                            <configOptions>
                                <interfaceOnly>true</interfaceOnly>
                                <dateLibrary>java8</dateLibrary>
                                <java8>true</java8>
                            </configOptions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

When I run mvn clean install , a jar file my-java-client-1.0-SNAPSHOT.jar is made in the target folder. 当我运行mvn clean install ,会在目标文件夹中创建一个jar文件my-java-client-1.0-SNAPSHOT.jar It has source code in it, but no pom.xml file. 它包含源代码,但没有pom.xml文件。 There is a pom.xml file in the target/generated-sources/swagger folder but it has the groupId and artifactId : target/generated-sources/swagger文件夹中有一个pom.xml文件,但它有groupIdartifactId

  <groupId>io.swagger</groupId>
  <artifactId>swagger-java-client</artifactId>

The README.md file in the target/generated-sources/swagger says to include the following in your pom.xml to use the generated jar: target/generated-sources/swagger的README.md文件说在pom.xml包含以下内容以使用生成的jar:

<dependency>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-java-client</artifactId>
    <version>1.0.0</version>
    <scope>compile</scope>
</dependency>

I'm guessing these are just defaults because if I generated multiple swagger clients they would conflict over the name, and I can see in the swagger-codegen source code that these fields are generated from placeholders like {{artifactId}} . 我猜这些只是默认值,因为如果我生成多个swagger客户端,它们会与名称冲突,我可以在swagger-codegen源代码中看到这些字段是从{{artifactId}}等占位符生成的。 I haven't been able to find where I can set these placeholders. 我无法找到可以设置这些占位符的位置。

How can I get the jar to include a pom.xml with an artifactId and groupId of my choosing, so I can upload it to a Maven repository like Artifactory, and use it in my Maven dependencies? 我怎样才能让jar包含一个带有我选择的artifactIdgroupIdpom.xml ,所以我可以将它上传到像Artifactory这样的Maven存储库,并在我的Maven依赖项中使用它?

It's not particularly well documented, but it appears you are able to override the artifactId, groupId etc from within the configOptions: 它没有特别好的文档记录,但看起来你可以从configOptions中覆盖artifactId,groupId等:

<configOptions>
    <groupId>com.your.group.id</groupId>
    <artifactId>your-artifact-id</artifactId>
    <artifactVersion>0.0.1-SNAPSHOT</artifactVersion>
</configOptions>

Many of the properties inside CodegenConstants.java can be set in the same way. CodegenConstants.java中的许多属性可以以相同的方式设置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 springdoc-openapi-maven-plugin 和 swagger-codegen-maven-plugin 生成客户端代码? - How to generate client code using springdoc-openapi-maven-plugin and swagger-codegen-maven-plugin? 带有 springdoc-openapi-ui 的 swagger-codegen-maven-plugin - swagger-codegen-maven-plugin with springdoc-openapi-ui swagger-codegen-maven-plugin中使用什么invokerPackage? - What invokerPackage is used for in swagger-codegen-maven-plugin? 删除 swagger-codegen-maven-plugin api class 中的 ResponseEntity - Remove ResponseEntity in swagger-codegen-maven-plugin api class swagger-codegen-maven-plugin 在生成 API class 时忽略我为 ZonedDateTime 设置的导入映射 - swagger-codegen-maven-plugin ignores the importmapping i have set for ZonedDateTime when generating an API class 如何使用maven-swagger-codegen-plugin生成文档? - How generate documentation with maven-swagger-codegen-plugin? 如何使用 swagger-codegen-plugin (maven) 生成客户端代码? - How to generate client code using with swagger-codegen-plugin (maven)? 如何使用 swagger-codegen-maven 插件在 boolean 上生成 getter - how to generate getter on boolean using swagger-codegen-maven plugin “swagger-codegen-maven-plugin:3.0.0 或其依赖项之一无法解析” - "swagger-codegen-maven-plugin:3.0.0 or one of its dependencies could not be resolved" 您如何让Maven swagger Codegen插件使用继承生成Java类? - How do you have the maven swagger codegen plugin generate Java classes using inheritance?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM