Search code examples
javamavenswaggerswagger-codegen

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. I put my swagger.json in the src/main/resources folder and ran mvn clean install. Here is my 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. It has source code in it, but no pom.xml file. There is a pom.xml file in the target/generated-sources/swagger folder but it has the groupId and artifactId:

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

The README.md file in the target/generated-sources/swaggersays to include the following in your pom.xml to use the generated 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}}. 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?


Solution

  • It's not particularly well documented, but it appears you are able to override the artifactId, groupId etc from within the configOptions:

    <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.