Thursday, October 3, 2013

Create a Google App Engine project that uses Spring + using Maven to create + Eclipse as IDE


I did not find any tutorial to create a maven project that uses Google App Engine and Spring and use Eclipse IDE.  So, I thought I would document what I had done recently to create a Spring MVC project using Maven and deploys to Google App Engine; using Eclipse as IDE.

I am assuming you have Eclipse plugin for Maven (m2e) installed to Eclipse.

Creation of project:
We need to use maven plugin to create a new Google App Engine project.  I used instructions to create using command line from here.  I am sure we can use Eclipse maven project creation wizard as well.

Adding Spring dependencies:
To add Spring dependencies, add dependencies.


  <!-- Spring Dependencies -->  
           <dependency>   
                <groupId>org.springframework</groupId>   
                <artifactId>spring-core</artifactId>   
                <version>${spring.version}</version>   
           </dependency>   
           <dependency>   
                <groupId>org.springframework</groupId>   
                <artifactId>spring-context</artifactId>   
                <version>${spring.version}</version>   
           </dependency>   
           <dependency>   
                <groupId>org.springframework</groupId>   
                <artifactId>spring-beans</artifactId>   
                <version>${spring.version}</version>   
           </dependency>   
           <!-- Spring MVC framework -->   
           <dependency>   
                <groupId>org.springframework</groupId>   
                <artifactId>spring-webmvc</artifactId>   
                <version>${spring.version}</version>   
           </dependency>   
           <dependency>   
                <groupId>org.springframework</groupId>   
                <artifactId>spring-web</artifactId>   
                <version>${spring.version}</version>   
           </dependency>   


Tips:

  • Till Spring 2.5.6, the framework used to provide a single all-encompassing dependency.  From Spring 3.0.x on wards, that is not the case.  So, we will have to include every dependency in pom.xml file.
  • It is important to have same version for all Spring dependencies.  If Spring dependencies don't have the same version, it gives out an error:

 [INFO] java.lang.NoSuchFieldError: APPLICATION_CONTEXT_ID_PREFIX  
 [INFO] at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:430)  
 [INFO] at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:458)  
 [INFO] at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:339)  

  • Dependencies can be checked using this command: mvn dependency:tree
  • When I was iterating through these steps multiple times, I found some old dependencies lingering around. If that happens, this command would clear things up: mvn clean


Create web.xml:
If web.xml has not been created already, create it under ${project}/src/main/webapp/WEB-INF

Add DispatcherServlet to web.xml:
This is a standard Spring stuff you would do in any Spring MVC project.  (I created MVC project, so I did this.  It may or may not done in other cases)  Also, depending on the name given to DispatcherServlet in web.xml, create <dispatcher-servlet-name>-servlet.xml xml file.

Deploying this application now will not work.  Here are some steps I took to get rid of the errors:

  1. First, we need to clean up the maven project.  Right click project -> Run As -> maven clean.
  2. Right click project -> Properties -> Project Facet -> Select "Dynamic Web Project".
  3. Again, Right click project -> Properties -> Deployment Assembly -> Add -> In popup, "Java Build Path Entries" -> Maven Dependencies -> Finish.  All this does is to copy dependency jar to .../WEB-INF/lib.
Hopefully, this helps someone create a project without any errors.
Thanks to these links:

http://stackoverflow.com/a/12600686/1700184
http://stackoverflow.com/a/6083776/1700184
https://developers.google.com/appengine/docs/java/tools/maven#creating_a_new_maven_app_engine_project_using_skeleton-archetype

No comments:

Post a Comment