MidgarZolom
MidgarZolom

Reputation: 305

How to call swagger codegen programmatically?

I am generating a restful java jax-rs api with swagger-codgen-cli.jar.
Right now I call java -jar with some command line options to do this.

java -jar swagger-codegen-cli.jar generate -i api.yaml -l jaxrs -o ./outputdir

Which works fine.

But I would like to make this call from of a Java program i.e. including the codegen.jar into my classpath and then call the corresponding method with similar parameters.

So is there a public API from the swagger-codegen module which I can call?

Upvotes: 3

Views: 6081

Answers (1)

abarisone
abarisone

Reputation: 3783

If I correctly understand what you need, you would like to dinamically generate your stub classes. So why don't use swagger-codegen-maven-plugin to generate your stub classes?

As reported in the usage section, simply add to your build->plugins section (default phase is generate-sources phase)

<plugin>
    <groupId>com.garethevans.plugin</groupId>
    <artifactId>swagger-codegen-maven-plugin</artifactId>
    <version>${project.version}</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>src/main/resources/api.yaml</inputSpec>
                <language>java</language>
            </configuration>
        </execution>
    </executions>
</plugin>

If you would like to execute the command from a program you may use Runtime.getRuntime().exec() or Runtime.getRuntime().exec() alternatives

Upvotes: 4

Related Questions