Search code examples
javagwtjavascript

Compile a GWT library into a JavaScript library


I want to know what is the best approach to take to build a GWT library into a JavaScript library. When this GWT library to be compiled into Java script does not have any GWT server side component in it.

Just plain front-end components. Including only things like AJAX calls, etc.

In Java the GWT library is used like this:

TheGWTLibrary api = new TheGWTLibrary();
api.setServer("http://www.somewhere/api");
api.post(stuff, new Callback(){
 void success(){
 }
 void fail(){
 }
});
api.get(new Callback(){
 void success(){
 }
 void fail(){
 }
});

Such that the GWT library project would be usable in any HTML project without making it a GWT app.

If this is possible where will the dependencies of this GWT library be compiled to? Will it be included in a one single JavaScript (js) file?


Solution

  • [EDIT 2016]

    The way to go with new GWT (2.8.0) is using JsInterop, read the documentation API. Note that it's still in 2.8.0-SNAPSHOT but will be released in few weeks. Also, JsInterop in 2.7.0 has been deprecated and removed in 2.8, so don't use it.

    [END EDIT]

    Apart from writing your JSI method, there are two easy ways to deal with this.

    1.- GwtExporter

    You might be interested on reading this article I wrote some years ago:

    https://code.google.com/p/gwtchismes/wiki/Tutorial_ExportingGwtLibrariesToJavascript_en

    It uses gwt-exporter, and you can take a look to a couple of projects using this approach:

    2.- JsInterop

    If you want to play with new stuff, you might read about JsInterop a beta feature in GWT-2.7 which will be fully functional in GWT-3.0.

    It allows you to export java classes to JS. You have to use the -XjsInteropMode JS and some annotations.

    There is no so much documentation right now, but there is a document explaining the API, and an interesting presentation.

    https://docs.google.com/document/d/1tir74SB-ZWrs-gQ8w-lOEV3oMY6u6lF2MmNivDEihZ4/edit

    http://gokdogan.appspot.com/gwtcreate2013/#1