Wednesday, November 16, 2011

How to mavenize your GWT eclipse projects

Maven and gwt eclipse projects haven`t been playing nicely with each other for quite some time now. There are always some little gotchas that might get you. For me I found a way that works nicely and so I decided to share. This is based on Google Plugin for Eclipse 3.6 Version 2.4.2.relr36v201110112027 and maven 3.0.3.

Update 1:
Users running this on windows reported problems with the linked war file. Changing the path for the linked file using an eclipse variable solves this. I updated the post to reflect this.

Update 2:
As Thorsten pointed out running mvn war:exploded is much faster than running mvn war:war . Updated the post.

The first thing I like to have is one single point of configuration and not a plit up configuration in many different places. To me this is what the pom.xml is for. So I wanted to have a way to be able to set up everything from the pom and let maven write the files that I need in eclipse. (Like .project and .classpath)  First thing for me was to declare the gwt version as a property like this:
<properties>
   <gwtversion>2.4.0</gwtversion>
</properties>
Next up you simply declare gwt as a dependency:
<dependency>
  <groupId>com.google.gwt</groupId>
  <artifactId>gwt-user</artifactId>
  <version>${gwtversion}</version>
</dependency>
<dependency>
  <groupId>com.google.gwt</groupId>
  <artifactId>gwt-servlet</artifactId>
  <version>${gwtversion}</version>
</dependency>
<dependency>
  <groupId>javax.validation</groupId>
  <artifactId>validation-api</artifactId>
  <version>1.0.0.GA</version>
  </dependency>
<dependency>
   <groupId>javax.validation</groupId>
   <artifactId>validation-api</artifactId>
   <version>1.0.0.GA</version>
   <classifier>sources</classifier>
</dependency>
So far nothing fancy, but right now maven would not be able to compile your gwt sources to javascript. For that we are using the gwt-maven-plugin:
<plugin>
   <groupId>org.codehaus.mojo</groupId>
   <artifactId>gwt-maven-plugin</artifactId>
   <version>${gwtversion}</version>
   <executions>
      <execution>
         <goals>
            <goal>compile</goal>
            <goal>test</goal>
         </goals>
      </execution>
   </executions>
</plugin>
Note: If for some reason you want to use a different version of the gwt-maven-plugin (using a newer version of the plugin with gwt trunk), you can set this version manually. Because the gwt-maven-plugin takes care of adding gwt-dev to the classpath for compilation only, we avoid having nasty dependency conflicts. So up to know you can simply run mvn package on the console and you will get a nice artifat compiled with gwt, but there is no integration with eclipse so far. For that we are going to use the maven-eclipse-plugin with some nice tricks. First the whole plugin configuration:
<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-eclipse-plugin</artifactId>
   <version>2.8</version>

   <configuration>
      <downloadSources>true</downloadSources>
      <downloadJavadocs>true</downloadJavadocs>
      <buildOutputDirectory>war/WEB-INF/classes</buildOutputDirectory>
      <projectnatures>
         <projectnature>org.eclipse.jdt.core.javanature</projectnature
         <projectnature>com.google.gdt.eclipse.core.webAppNature</projectnature>
         <nature>com.google.gwt.eclipse.core.gwtNature</nature>
      </projectnatures>
     
      <buildcommands>
         <buildcommand>org.eclipse.jdt.core.javabuilder</buildcommand>
         <buildcommand>com.google.gdt.eclipse.core.webAppProjectValidator</buildcommand>
         <buildcommand>com.google.appengine.eclipse.core.projectValidator</buildcommand>
         <buildcommand>com.google.gwt.eclipse.core.gwtProjectValidator</buildcommand>
      </buildcommands>

      <classpathContainers>
         <classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer>
         <classpathContainer>com.google.gwt.eclipse.core.GWT_CONTAINER</classpathContainer>
      </classpathContainers>
     
      <excludes>
         <exclude>com.google.gwt:gwt-servlet</exclude>
         <exclude>com.google.gwt:gwt-user</exclude>
         <exclude>com.google.gwt:gwt-dev</exclude>
         <exclude>javax.validation:validation-api</exclude>
      </excludes>

      <linkedResources>
         <linkedResource>
            <name>war
            <type>2
            <location>PROJECT_LOC/target/${project.artifactId}-${project.version}</location>
  </linkedResource>
      </linkedResources>

   </configuration>
</plugin>


Lets discuss the different parts in detail:
<linkedResources>
   <linkedResource>
   <name>war
   <type>2
   <location>PROJECT_LOC/target/${project.artifactId}-${project.version}</location>
   </linkedResource>
</linkedResources>
This adds a linked directory to your eclipse project which actually points to the compile directory of maven. So your build from eclipse just use the same output directory. This is quite useful because the google eclipse plugin won't complain about the compile target of the project, but still will deploy to a temporary directory (and not src/main/webapp)

With this part you tell eclipse to complile into the linked directory:
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
<buildOutputDirectory>war/WEB-INF/classes</buildOutputDirectory>


We also have to set the right project natures so that our project is regonzied as java, gwt and a wepp app project:
<projectnatures>
   <projectnature>org.eclipse.jdt.core.javanature</projectnature
   <projectnature>com.google.gdt.eclipse.core.webAppNature</projectnature>
   <nature>com.google.gwt.eclipse.core.gwtNature</nature>
</projectnatures>


And we have to configure the right builders for our project:
<buildcommands>
   <buildcommand>org.eclipse.jdt.core.javabuilder</buildcommand>
   <buildcommand>com.google.gdt.eclipse.core.webAppProjectValidator</buildcommand>
   <buildcommand>com.google.appengine.eclipse.core.projectValidator</buildcommand>
   <buildcommand>com.google.gwt.eclipse.core.gwtProjectValidator</buildcommand>
</buildcommands>


Also we want to have the GWT SDK and the Java SDK on our buildpath:
<classpathContainers>
   <classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer>
   <classpathContainer>com.google.gwt.eclipse.core.GWT_CONTAINER</classpathContainer>
</classpathContainers>


And we have to make sure that gwt maven dependencies are not on the classpath, because the google eclipse plugin needs the local SDK jars to work properly. So for eclipse project we exclude them (google eclipse plugin will provide them).
<excludes>
   <exclude>com.google.gwt:gwt-servlet</exclude>
   <exclude>com.google.gwt:gwt-user</exclude>
   <exclude>com.google.gwt:gwt-dev</exclude>
   <exclude>javax.validation:validation-api</exclude>
</excludes>


If you set up your project to include this in your pom, you can now simply to:

mvn eclipse:eclipse 
mvn war:exploded 

 and import the project into eclipse:

File -> Import -> General -> Existing Projects into Workspace


The one thing I was not able to figure out was how to tell the google eclipse plugin that gwt-servlet and gwt-user are already inside war/WEB-INF/lib, so you still get a warning about that. Something like : 

"The GWT SDK JAR gwt-servlet.jar is missing in the WEB-INF/lib directory"

But you can simply use the quick fix:

> Synchronize /WEB-INF/lib with SDK libaries. 


The google eclipse plugin will copy the gwt-servlet.jar into your WEB-INF/lib folder and you are good to go. I will try to talk about this last issue with some developers from google at the google developer weekend to get this working all the way. For me this is the best way right now to work with maven and gwt in eclipse.


If you did something to your project which needs to rewrite the eclipse project files you can do:


mvn eclipse:clean
mvn eclipse:eclipse


If you are making changes to src/main/webapp that need to be reflected in your war directory, simply run:


mvn war:exploded


For those interested here is a complete pom example from the mgwt showcase:
<?xml version="1.0" encoding="UTF-8"?>
<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.googlecode.mgwt</groupId>
 <artifactId>mgwt-showcase</artifactId>
 <version>1.0.0-SNAPSHOT</version>
 <packaging>war</packaging>
 <name>Mobile GWT Showcase</name>

 <properties>
  <gwtversion>2.4.0</gwtversion>
 </properties>

 <build>
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
    <configuration>
     <source>1.6</source>
     <target>1.6</target>
    </configuration>
   </plugin>

   <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>gwt-maven-plugin</artifactId>
    <version>2.4.0</version>
    <executions>
     <execution>
      <goals>
       <goal>compile</goal>
       <goal>test</goal>
      </goals>
     </execution>
    </executions>
   </plugin>

   

   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-eclipse-plugin</artifactId>
    <version>2.8</version>

    <configuration>
     <downloadSources>true</downloadSources>
     <downloadJavadocs>false</downloadJavadocs>
     <buildOutputDirectory>war/WEB-INF/classes</buildOutputDirectory>
     <projectnatures>
      <projectnature>org.eclipse.jdt.core.javanature</projectnature>
      <projectnature>com.google.gdt.eclipse.core.webAppNature</projectnature>

      <nature>com.google.gwt.eclipse.core.gwtNature</nature>
     </projectnatures>
     <buildcommands>
      <buildcommand>org.eclipse.jdt.core.javabuilder</buildcommand>
      <buildcommand>com.google.gdt.eclipse.core.webAppProjectValidator</buildcommand>

      <buildcommand>com.google.appengine.eclipse.core.projectValidator</buildcommand>
      <buildcommand>com.google.gwt.eclipse.core.gwtProjectValidator</buildcommand>
     </buildcommands>
     <classpathContainers>
      <classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer>

      <classpathContainer>com.google.gwt.eclipse.core.GWT_CONTAINER</classpathContainer>
     </classpathContainers>
     <excludes>
      <exclude>com.google.gwt:gwt-servlet</exclude>
      <exclude>com.google.gwt:gwt-user</exclude>
      <exclude>com.google.gwt:gwt-dev</exclude>
      <exclude>javax.validation:validation-api</exclude>
     </excludes>
     <linkedResources>
      <linkedResource>
       <name>war</name>
       <type>2</type>
       <location>PROJECT_LOC/target/${project.artifactId}-${project.version}</location>
      </linkedResource>
     </linkedResources>

    </configuration>
   </plugin>
   <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
     <execution>
      <id>assemble</id>
      <phase>package</phase>
      <goals>
       <goal>single</goal>
      </goals>
     </execution>
    </executions>
    <configuration>
     <descriptors>
      <descriptor>src/main/assembly/clientcode.xml</descriptor>
     </descriptors>
    </configuration>
   </plugin>
  </plugins>
  <resources>
   <resource>
    <directory>src/main/java</directory>
    <includes>
     <include>**/client/**</include>
     <include>**/*.gwt.xml</include>
    </includes>
   </resource>
  </resources>
 </build>

 <dependencies>
  <dependency>
   <groupId>com.googlecode.mgwt</groupId>
   <artifactId>mgwt</artifactId>
   <version>1.0.1-SNAPSHOT</version>

  </dependency>

  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>4.7</version>
   <type>jar</type>
   <scope>test</scope>
  </dependency>
  <dependency>
   <groupId>com.google.gwt</groupId>
   <artifactId>gwt-user</artifactId>
   <version>${gwtversion}</version>
  </dependency>
  <dependency>
   <groupId>com.google.gwt</groupId>
   <artifactId>gwt-servlet</artifactId>
   <version>${gwtversion}</version>
  </dependency>
  <dependency>
   <groupId>javax.validation</groupId>
   <artifactId>validation-api</artifactId>
   <version>1.0.0.GA</version>
  </dependency>
  <dependency>
   <groupId>javax.validation</groupId>
   <artifactId>validation-api</artifactId>
   <version>1.0.0.GA</version>
   <classifier>sources</classifier>
  </dependency>
 </dependencies>



</project>

Monday, November 14, 2011

mgwt 1.0 released

After more than 1,5 years I am very happy to announce the first non-beta release of mgwt. During the last three months I was able to put a tremendous amount of time into this project. For a quick overview checkout the Showcase with a webkit browser (e.g. Chrome, Safari, ...) or your phone / tablet.
This release supports:
  • iPhone
  • iPhone Retina Displays
  • iPad
  • Android Phones
  • Android Tablets
  • blackberry phones
With this release you get:
  • Great looking fast animations integrated with GWT MVP
  • Nice set of good looking widgets
  • Performance because we leverage CSS3 as much as possible (no images, animations in css and many more details)
  • Great looking styles for each platform
  • UiBinder support for all widgets
  • Editor support for all widgets
  • Deferred Binding for all major diffs
We love working with GWT and this is why mgwt does not try to replace GWT. Instead it uses best practices you already know from GWT and extends it where needed. If you want to know why we decided to go with GWT in the first place to do mobile development, this wiki page sums it up nicely. We know that the current documentation needs improvement and we will start to improve it very soon. To get started with developing great mobile apps checkout Getting started wiki page and also take a look at m-gwt.com for a brief overview. This is just the start and we are very excited about the future of mgwt. If you have any questions or suggestions as always don't hesitate to ask. mgwt is available from central:
<dependency>
      <groupId>com.googlecode.mgwt</groupId>
      <artifactId>mgwt</artifactId>
      <version>1.0.0</version>
</dependency>

Saturday, November 12, 2011

GWT-Phonegap 1.2 released

In preparation for the google developer day I was very anxious to support the latest version of phonegap.

To get a good overview of the current state of gwt-phonegap you can take a look at the features page of the wiki. As always if you have any feedback or problems feel free to use the usergroup.

This release is also available from maven central:
<dependency>
      <groupId>com.googlecode.gwtphonegap</groupId>
      <artifactId>gwtphonegap</artifactId>
      <version>1.2.0.0</version>
</dependency>

Wednesday, October 12, 2011

gwt-phonegap in maven central repository

Using gwt-phonegap has become much easier since the project is now available from the maven central repository. You can use your favorite build tool to add the dependency. It is deployed with the groupId: com.googlecode.gwtphonegap and the artifactId: gwt-phonegap and version 1.0.0.0 For maven this would look like:
<dependency>
      <groupId>com.googlecode.gwtphonegap</groupId>
      <artifactId>gwtphonegap</artifactId>
      <version>1.0.0.0</version>
</dependency>
I hope that saves you a lot of local deployment time when using gwtphonegap. In the next couple of weeks I will also start an archetype for a standard gwtphonegap project, so getting stated will be much easier.

Monday, October 10, 2011

GWT-Phonegap version 1.0 released

Today I was finally able to release gwt-phonegap as 1.0. There has been a tremendous amount of work put into this and I want to thank all the people that contributed in any way.

GWT-Phonegap now supports the full phonegap API and provides a mocked functionality in gwt dev mode. In the next days I will concentrate on improving the documentation, so that getting to work with gwt-phonegap will become much more fun.

To see the full supported features you can checkout the feature tab.

In the next couple of weeks, I will also make the first 1.0 release for mgwt, so that the whole toolchain for creating great mobile apps with gwt will be in a supported released state.

As always if you have any feedback or problems feel free to use the usergroup.

For those who are wondering why I renamed all the packages to com.googlecode.gwtphonegap: This needed to be done to comply with the rules for maven central. I will try to get the project into maven central very soon, so that including it into your build environment will be much easier.

Wednesday, September 28, 2011

GWT-Phonegap nearing 1.0

In the last couple of weeks I was able to invest a substantial amount of time into gwt-phonegap and mgwt. The work on gwt-phonegap is almost complete and it supports all of the phonegap api.

If you are running in dev mode, gwt-phonegap will emulate the phonegap apis, so that you will be able to develop your phonegap app in your safari / chrome on your desktop machine and later compile it down to javascript. This is done with gwt deferred binding so there are no runtime costs at all.

Also the documentation has had some major improvements, so that it will be much easier to get started with gwt-phonegap.

One major thing that happened was to change the package structure from de.kurka to com.googlecode.gwtphonegap to meet the requirements for artifacts to be uploaded the the central maven repository.

I will be releasing the first beta of gwt-phonegap 1.0.0.0 very soon and if all goes well, we are going to have an release in the central repo very soon.

In the next couple of weeks I will be focusing to get mgwt to 1.0 as well

Tuesday, May 3, 2011

gwt-dispatch and Custom Header or Basic Authentification

gwt-dispatch is an open source implementation of the command pattern, which is very well suited for use with GWT RPC.

In order to set Custom Headers in GWT you need to get the RequestBuilder instance which will perform the request under the covers for you.

There are two ways to do this:
  1. Let your Async Interface return RequestBuilder instead of void
  2. You can provide an instance of RPCRequestBuilder to your service, which will construct the RequestBuilderInstance

Using option one has the disadvantages that you have to send your request manually, thus changing the DispachAsync interface would break a lot of current applications (and introduce a lot of redundant code). So I was looking for another options.


You can downcast your service to ServiceDefTarget and set an instance of RPCRequestBuilder like this:

service = GWT.create(Dispatch.class);
((ServiceDefTarget) service).setRpcRequestBuilder(this.authenticatingRequestBuilder);


With and RPCRequestBuilder looking like this:

public class ACustomRequestBuilder extends RpcRequestBuilder {

@Override
protected RequestBuilder doCreate(String serviceEntryPoint) {
RequestBuilder requestBuilder = super.doCreate(serviceEntryPoint);
requestBuilder.setHeader("your header", "your value");
return requestBuilder;
}
}




Now you can easily control all aspects of your request.

Thanks to Robert Muntenau this is now also in the gwt-dispatch docs: http://code.google.com/docreader/#p=gwt-dispatch&s=gwt-dispatch&t=RequestBuilder