Tuesday, April 5, 2011

GWT with RubyOnRails backend glued together with Maven/JRuby

let's first look a the different parts involved:
  • GWT (Google Webtoolkit) comes with "generators" which focus on ANT as build system
  • Ruby on Rails has its way of starting a new application and scaffold models and their views
  • JRuby allows to use java side by side with ruby and allows to run a rails application in a java servlet engine
  • Maven is a build tool mainly for java project. but with a set of jruby maven plugins (j)ruby project can be managed almost as well. with the advent of Roo the maven support for GWT drastically improved.
  • Ruby-Maven Gem tries to bridge the ruby and maven world by offering a more concise commandline interface. as gem it is easy to install from ruby point of view.
  • RestyGWT is a GWT generator for REST services and JSON encoded data transfer objects.
walkthrough of an example
first you need ruby-maven installed. when you use it with ruby (MRI) then you also need java >1.5 installed or use jruby. please update the gem if already installed:

jruby -S gem install ruby-maven

or
ruby -S gem install ruby-maven
using MRI will improve startup time drastically.

create a rails application
with ruby-maven there are new commands rmvn, gwt and jetty-run with this you can create a new rails application with GWT setup like this
gwt new my_app com.example
with this you have already a GWT application with EntryPoint and could be started but does not do much. the class layout within the GWT client package:
src/main/java/com/example/client/
  ├── client
  │   ├── ActivityPlaceActivityMapper.java
  │   ├── ActivityPlace.java
  │   ├── managed
  │   │   ├── ActivityFactory.java
  │   │   ├── MyAppModule.java
  │   │   └── MyAppPlaceHistoryMapper.java
  │   └── MyAppEntryPoint.java
  └── MyApp.gwt.xml

scaffold a resource with model, controller, gwt-model, etc
go into the created application (or any existing rails app)
cd my_app
now you can scaffold a model (currently two steps are required for this)
rmvn rails generate scaffold user name:string
this creates a rails like structure following the GWT activity/place/view pattern:
src/main/java/com/example/client/
├── activities
│   └── UserActivity.java
├── models
│   └── User.java
├── places
│   ├── UserPlace.java
│   └── UserPlaceTokenizer.java
├── restservices
│   └── UsersRestService.java
└── views
    ├── UserViewImpl.java
    ├── UserView.java
    └── UserView.ui.xml
finally you can run the application.

start the GWT development shell

before running the application you need to migrate the database
rmvn rake db:migrate
and start the GWT development shell
gwt run
the start url for the new resource is

to run the application with default webrick you need first to compile the GWT part
gwt compile
the compiler will output the GWT app in public/my_app and then start the webrick. the start url is http://localhost:3000/MyApp.html#users
rmvn rails server
or if you prefer the jetty server with http://localhost:8080/MyApp.html#users
rmvn jetty:run
build the war file
rmvn package 
the war file can be found in the target directory

more
you can also use proper maven3 instead of ruby-maven but the command-line-interface of maven is much more clumsy though the functionality is the same.

more up to date infos are here: https://github.com/mkristian/rails-resty-gwt

enjoy !
any comment and/or suggestion is welcome.