Some time ago I wrote about how to implement your Restful Web API using Spring MVC. Read my previous post to know about it.
In that post it was developed a simple Rest example. For testing the application, file was copied into a web server (Tomcat for example), and then accessing to http://localhost:8080/RestServer/characters/1 information of character 1 was returned.
In current post I am going to explain how to transform that application to a Google App Engine and be deployed into Google's infrastructure using Maven. Of course in this case we are going to deploy a Rest Spring MVC application, but same approach can be used for migrating a Spring MVC web application (or any other application developed with other web framework) to GAE.
First of all, obviously you should create a Google Account and register a new application (remember the name because will be used in next step). After that you can start the migration.
Three changes are required, create appengine-web.xml defining application name; add server tag to settings.xml with Google account information, and modify pom.xml for adding GAE plugin and its dependencies.
Let's start with appengine-web.xml. This file is used by GAE to configure application and is created into WEB-INF directory (at same level of web.xml).
The most important field is application tag. This tag contains the name of our application (defined when you register a new Google Application).
Other tags are version, system properties and environment variables, and misc configuration like if you want a precompilation to enhance performance or if your application requires sessions.
And your project should not be modified anymore, now only Maven files will be touched.
In settings.xml, account information should be added:
See that it is as easy as registering any other server in Maven.
And finally the most tedious part, modifying pom.xml.
First thing is adding new properties:
At first line we are defining Appengine Java SDK location. If you have already installed then insert location in this tag, if not, copy same location of this pom and simply change maven repository directory, in my case /media/share/maven_repo, to yours. Typically your Maven repository location will be /home/user/.m2/repositories. Maven will download SDK for you at deploy time.
Next step is adding Maven GAE repository.
Because our project is dummy project, Datanucleus are not used. In case of more complex projects, that database access is required using, for example JDO, next dependencies should be added:
And in case you are using Datanucleus, maven-datanucleus-plugin should be registered. Take care to configure it properly depending on your project.
Now Google App Engine dependencies are added.
Then if you want to test GAE functionalities (not used in our dummy project), next GAE libraries are added:
Next change is a modification on maven-war-plugin including appengine-web.xml into generated package:
And finally adding maven-gae-plugin and configuring it to upload application to appspot.
See that <serviceId> tag contains the server name defined previously in settings.xml file.
Also if you are using maven-release-plugin you can upload application to the appspot automatically, during release:perform goal:
Now run gae:deploy goal. If you have already installed Appengine Java SDK, then your application will be uploaded to your GAE site. But if it is the first time you run the plugin, you will receive an error. Do not panic, this error occurs because Maven plugin does not find Appengine SDK into directory you specified in <gae.home> tag. But if you have configured gae.home location into your local Maven repository, simply run gae:unpack goal, and SDK will be installed correctly so when you rerun gae:deploy your application will be uploaded into Google infrastructure.
In post example you can go to http://alexsotoblog.appspot.com/characters/1 and character information in JSON format is displayed into your browser.
As I have noted at the beginning of the post, the same process can be used for any web application, not only for Spring Rest MVC.
Because of teaching purpose all modifications have been made into application pom. My advice is that you create a parent pom with GAE related tags, so each project that must be uploaded into Google App Engine extends from same pom file.
I wish you have found this post useful.
This week I am at devoxx, meet me there ;) I will be speaking on Thursday 17 at 13:00 about Speeding Up Javascript & CSS Download Times With Aggregation and Minification.
Full pom file:
Download Code.
3 comentarios:
Fantastic write up Alex, I've been looking for a good SpringMVC tutorial for a while now for a hobby project I'm planning.
Keep up the good work, I'm looking forward to trying this at the weekend!
Muchas gracias!
Hi James thank you very much for reading my blog. For me the most important thing is writing posts that are useful for developers, so I am so happy you have found it interesting.
Muchas gracias a ti.
Publicar un comentario