Skip to content

Workflow integration

Tony Tam edited this page Jun 25, 2014 · 23 revisions

You can add swagger-codegen into your workflow in a number of ways.

Templates

Default codegen templates are included with the swagger-codegen artifact. You can of course override these by copying them into your own deployment, and setting a new template directory. A good starting point is to grab templates from here:

https://github.com/wordnik/swagger-codegen/tree/master/src/main/resources

And modify them as you like. After doing so, you'll need to override the basic codegen template directory. See the petstore client as an example:

https://github.com/wordnik/swagger-codegen/tree/master/samples/client/petstore

  // location of templates
  override def templateDir = "mytemplates/Java"

Build process with Maven

It's easy to make the codegen a part of your build cycle. Take the following pom.xml for example:

<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.wordnik</groupId>
  <artifactId>client-generator</artifactId>
  <packaging>jar</packaging>
  <name>client-generator</name>
  <version>1.0.0-SNAPSHOT</version>
  <prerequisites>
    <maven>2.2.0</maven>
  </prerequisites>

  <build>
    <sourceDirectory>src/main/scala</sourceDirectory>
    <testSourceDirectory>src/test/scala</testSourceDirectory>
    <plugins>
      <plugin>
        <groupId>net.alchim31.maven</groupId>
        <artifactId>scala-maven-plugin</artifactId>
        <version>${maven-plugin-version}</version>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
              <goal>testCompile</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <jvmArgs>
            <jvmArg>-Xms64m</jvmArg>
            <jvmArg>-Xmx1024m</jvmArg>
          </jvmArgs>
          <launchers>
            <launcher>
              <id>java-codegen</id>
              <!-- See note #1 -->
              <mainClass>JavaPetstoreCodegen</mainClass>
              <args>
                <!-- See note #2 -->
                <arg>http://petstore.swagger.wordnik.com/api/api-docs</arg>
                <!-- See note #3 -->
                <arg>special-key</arg>
              </args>
              <jvmArgs>
                <!-- See note #4 -->
                <jvmArg>-DfileMap=src/test/resources/petstore-1.2/api-docs</jvmArg>
              </jvmArgs>
            </launcher>
            <launcher>
              <id>objc-codegen</id>
              <mainClass>com.wordnik.swagger.codegen.BasicObjcGenerator</mainClass>
              <args>
                <arg>http://petstore.swagger.wordnik.com/api/api-docs</arg>
                <arg>special-key</arg>
                <arg></arg>
              </args>
              <jvmArgs>
                <!--jvmArg>-DfileMap=src/test/resources/petstore-1.2/api-docs</jvmArg-->
              </jvmArgs>
            </launcher>
          </launchers>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>copy-dependencies</goal>
            </goals>
            <configuration>
              <outputDirectory>${project.build.directory}/lib</outputDirectory>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  <repositories>
    <!-- See note #5 -->
    <repository>
      <id>sonatype-snapshots</id>
      <url>https://oss.sonatype.org/content/repositories/snapshots</url>
    </repository>
  </repositories>
  <dependencies>
    <dependency>
      <groupId>com.wordnik</groupId>
      <artifactId>swagger-codegen_2.9.1</artifactId>
      <version>${codegen-version}</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.scala-lang</groupId>
      <artifactId>scala-library</artifactId>
      <version>${scala-version}</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>
  <properties>
    <codegen-version>2.0.13</codegen-version>
    <scala-version>2.9.1</scala-version>
    <junit-version>4.8.1</junit-version>
    <maven-plugin-version>3.1.0</maven-plugin-version>
  </properties>
</project>

To generate the objective-c client, run:

mvn scala:run -Dlauncher=objc-codegen

which executes the default objective-c script.

Now to generate the java client, you simply run:

mvn scala:run -Dlauncher=java-codegen

which executes the codegen script under src/main/scala named JavaPetstoreCodegen. Note! To run this, you'll need to copy some files from the codegen project, or run this pom.xml from the codegen source folder.

Some notes:

  1. JavaPetstoreCodegen is the codegen script, should lives under src/main/scala

  2. This is the URL where the service is running

  3. If the api requires a key, put it here.

  4. You can generate from a set of files instead of from a live server by setting the -DfileMap flag. If this is the case, the URL from #2 will be used as the basePath of the service when generating the code.

    Using files for the codegen is a good idea for automating the build process without having to launch a server to provide the json.

  5. If you are using a snapshot, you'll need to add the Sonatype OSS repository, as shown