Wrap your favorite library!

With DukeScript it’s easy to wrap any JavaScript Library using @JavaScriptRessource and @JavaScriptBody Annotations. This is great if you want quick access to some functionality from an existing library. But preparing a full blown API for the whole functionality is a lot of work.

That’s why we created the maven-typings-plugin. It makes use of the type information that is available for more than 1500 JavaScript Libraries and automatically builds a Java API for them.

The process is really simple. You clone our repository of type definitions and follow a simple process, first you copy the pom.xml from the jquery project and replace all the mentions of jquery with the name of your library. For this demo I decided to use ace. That’s a code editor library, which might come handy for some future projects:

$ cp jquery/pom.xml ace/pom.xml
$ cd ace
$ open pom.xml
# replace references to jquery with ace

I first had a look into the “ace.d.ts” file. That’s the file which holds the actual type definitions. On the top it has a comment section with information about the library. I followed the project link, found a download link for ace.js and added it to the pom. So if you want to try it out, this is what the changed section of the pom should look like after your adjustments:

<artifactId>net.java.html.lib.ace</artifactId>
    <name>Java Typings for ace.js</name>
    <properties>
        <!-- the typings file is expected in ${typings}.d.ts -->
        <typings>ace</typings>
        <!-- the javascript library to download that contains the actual code -->
        <typings.url>https://raw.githubusercontent.com/ajaxorg/ace-builds/master/src-min/ace.js</typings.url>
    </properties>

Now all you have to do is a maven install, and as a result you’ll get a wonderful new DukeScript library.

It’s best to write some tests, while the library is still warm from the compiler. Again the JQuery project can provide some clues. So copy the “src” folder from the jquery project, rename the package “jquery” to “ace”, and open class “JQueryTest.java” in the editor.