Setting up file-based code gen with maven/pom.xml

279 views
Skip to first unread message

duane

unread,
Sep 9, 2013, 3:03:05 PM9/9/13
to swagger-sw...@googlegroups.com
So I now have a working pom.xml for the Petstore example. Next, I'm working toward an automated code generation process that uses local json files to create a ruby API client.

Prior to my using maven as a built step, I could generate ruby code from the Canvas app's api-docs.json like this:

JAVA_OPTS=-DfileMap=/Users/duane/Instructure/canvas-lms/public/doc/api/api-docs.json ./bin/runscala.sh com.wordnik.swagger.codegen.BasicRubyGenerator http://canvas.instructure.com

The key arguments are:
1. the java VM "fileMap" argument, and
2. the "http://canvas.instructure.com" dummy (?) argument

If I put these arguments in pom.xml, like so (in //project/build/plugins/plugin/configuration/launchers):

<launcher>
<id>ruby-codegen</id>
<mainClass>RubyCanvasCodegen</mainClass>
<args>
</args>
<jvmArgs>
<jvmArg>/Users/duane/Instructure/canvas-lms/public/doc/api/api-docs.json</jvmArg>
</jvmArgs>
</launcher>

I get the following error:

[INFO] launcher 'ruby-codegen' selected => RubyCanvasCodegen
Exception in thread "main" java.lang.NoClassDefFoundError: /Users/duane/Instructure/canvas-lms/public/doc/api/api-docs/json
Caused by: java.lang.ClassNotFoundException: .Users.duane.Instructure.canvas-lms.public.doc.api.api-docs.json
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

Based on the wiki documentation [1], I've tried the following configuration:

<launcher>
<id>ruby-codegen</id>
<mainClass>RubyCanvasCodegen</mainClass>
<args>
</args>
<jvmArgs>
<jvmArg>/Users/duane/Instructure/canvas-lms/public/doc/api/</jvmArg>
</jvmArgs>
</launcher>

But I get the following error:

[INFO] launcher 'ruby-codegen' selected => RubyCanvasCodegen
Exception in thread "main" java.lang.NoClassDefFoundError: /Users/duane/Instructure/canvas-lms/public/doc/api/
Caused by: java.lang.ClassNotFoundException: .Users.duane.Instructure.canvas-lms.public.doc.api.
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

I've played around with various alternative args and jvmArgs but nothing has succeeded so far. Any guidance?

Thanks,
Duane

tony tam

unread,
Sep 9, 2013, 3:13:13 PM9/9/13
to swagger-sw...@googlegroups.com
Hi Duane, you need to add a flag in that jvm setting:

              <jvmArgs>
                <!-- See note #4 -->
                <jvmArg>-DfileMap=/Users/duane/Instructure/canvas-lms/public/doc/api/api-docs.json</jvmArg>
</jvmArgs>

Duane Johnson

unread,
Sep 10, 2013, 11:43:40 AM9/10/13
to swagger-sw...@googlegroups.com
For others: this was among several issues that Tony and I uncovered. To wit:

1. Update to the latest snapshot if you are using file-based code generation (as opposed to generating from a URL):

In the pom.xml //project/properties tag:

  <properties>
    <codegen-version>2.0.9-SNAPSHOT</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>

(note the 2.0.9-SNAPSHOT)

In previous versions, the swagger-codegen code would assume it could tack on the api filenames to the end of the resource api filename, which never works (although that strategy could work for URLs).

2. Upgrade to Java SE 1.7. I had a very persistent OutOfMemoryError that happened on Java 1.6:

Caused by: java.lang.OutOfMemoryError: Java heap space
at scala.collection.mutable.HashMap.get(HashMap.scala:65)
at scala.collection.mutable.MapLike$class.getOrElseUpdate(MapLike.scala:186)
at scala.collection.mutable.HashMap.getOrElseUpdate(HashMap.scala:43)
at scala.tools.nsc.io.ZipArchive.scala$tools$nsc$io$ZipArchive$$ensureDir(ZipArchive.scala:99)
at scala.tools.nsc.io.ZipArchive.getDir(ZipArchive.scala:108)
at scala.tools.nsc.io.FileZipArchive.iterator(ZipArchive.scala:121)

Upgrading made it go away.

-- Duane

Beth Tran

unread,
Apr 1, 2015, 4:34:57 AM4/1/15
to swagger-sw...@googlegroups.com
I followed  https://github.com/wordnik/swagger-codegen/wiki/Workflow-Integration
and have these two in pom.xml

      <arg>http://petstore.swagger.io/v2/swagger.json</arg>
      <jvmArg>-DfileMap=src/test/resources/petstore-1.2/petstore.json</jvmArg>

But got this error
    java.lang.Exception: unable to read from http://petstore.swagger.io/v2/swagger.json

When I type the URL, I got the json - and I save it in petstore.json; however for some reason, the mvn scala:run -Dlauncher=java-codegen
complains that it's unable to read from the URL

Am I missing something?

tony tam

unread,
Apr 2, 2015, 12:27:45 AM4/2/15
to swagger-sw...@googlegroups.com
Hi, As of 2.1.x, this workflow integration method is not accurate.  Please take a look here:


There is now an executable to run, making the maven integration less necessary.  Please try that out and let me know how it goes.

Beth Tran

unread,
Apr 2, 2015, 11:57:29 AM4/2/15
to swagger-sw...@googlegroups.com
Thank you Tony, my test with petstore is successful using the given URL.

Beth Tran

unread,
Apr 9, 2015, 8:25:24 PM4/9/15
to swagger-sw...@googlegroups.com
How do I use the generated code to post XML in the payload?
For example: the generated PetApi.java,
                      Pet thePet = new Pet();
                      thePet.setId(...); thePet.set...()...
                      new PetApi.addPet(thePet);
        I believe the payload is json by default and post to petstore server.
Is there a way to tell PetApi to post the the payload in XML?

tony tam

unread,
Apr 10, 2015, 10:05:00 AM4/10/15
to swagger-sw...@googlegroups.com
The templates currently are only supporting JSON.  It's pretty easy to add support for XML, it just varies by language.  You would need to modify the ApiInvoker.mustache class to do so.  See this line:


and this one:


Two methods to implement!  To / from XML and you're set.

Beth Tran

unread,
Apr 10, 2015, 11:34:02 AM4/10/15
to swagger-sw...@googlegroups.com
Tony
   Thank you so much.  I'll try this, and let you know.
Cheers,
Beth
Reply all
Reply to author
Forward
0 new messages