Showing posts with label Spring. Show all posts
Showing posts with label Spring. Show all posts

Sunday, October 17, 2010

Maven: Add Spring dependencies and Spring MVC your project

To enable Spring and Spring MVC modules in your maven project, follow the steps below

#1: Add Spring related dependencies. The dependencies below will get all the rest of the transitive dependencies.


  
    org.springframework
    spring-context
    ${org.springframework.version}

    
      
      
        commons-logging
        commons-logging
      
    

    
      
      org.springframework
      spring-webmvc
      ${org.springframework.version}
    


The org.springframework.version property is set to 3.0.3.RELEASE:

3.0.3.RELEASE

  

#2. Setup Dispatcher Servlet in your web.xml:

The Spring MVC requires setting the dispatcher servlet in web.xml. Below we are loading the /WEB-INF/sprin/app-config.xml (the main Spring application configuration file for the project) while instantiating the Dispatcher Servlet at application start up time i.e. when the server is starting up.

NOTE: You can name your application config with any name of your liking. Just make sure it is correctly referenced in Dispatcher Servlet setting in web.xml.


    springmvcdemo
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation/WEB-INF/spring/app-config.xml
      
    1
  

  
    springmvcdemo
    /app/*
  


#3. The context config location points to the spring application config file for the application (app-config.xml). There are 2 things of note here:

a. The com.mayabansi.webappdemo will be scanned for classes annotated with @Controller, @Resources. @Component, @Service etc.

b. The mvc-config.xml, that holds the MVC configuration settings is imported.


    

    
    

#4. The MVC config file has Spring MVC related configuration settings:


    

    
    

    
    
        
        
    

Maven and Spring Enterprise Bundle Repository (in settings.xml)

There is this very informative article about Maven and adding Spring dependencies to your project here.

But I was not able to add the repositories below to my pom.xml and be able to connect with Enterprise Bundle Repository (EBR). I had to add them to my /home/rhasija/.m2/settings.xml file:

  
    SpringSource Enterprise Bundle Repository – External Bundle Milestones
    http://repository.springsource.com/maven/bundles/milestone
  

  
    SpringSource Enterprise Bundle Repository – SpringSource Bundle Releases
    http://repository.springsource.com/maven/bundles/release
  

  
    SpringSource Enterprise Bundle Repository – External Bundle Releases
    http://repository.springsource.com/maven/bundles/external
  


I am sure I must have done something wrong, but I prefer them to be in settings.xml anyway, instead of adding the above mentioned repositories to every project of mine.

Maven, Spring MVC, Hibernate and Mockito

In this 8 part series, I will cover how to create a simple Maven Web project, and then add Hibernate, Spring MVC and Mockito dependencies. I am assuming you have Maven installed. If not you can learn how to install Maven here.

First we will start with a brand new Maven project:

Step #1: Generate a Maven Webapp project:

mvn archetype:generate

The command above provides us a lot of archetype options. For this demo, we are going to choose #83 that is: maven-archetype-webapp (An archetype which contains a sample Maven Webapp project.).

Below (in bold) were the options I chose:

Choose a number: 80: 83
Choose version:
1: 1.0
2: 1.0-alpha-1
3: 1.0-alpha-2
4: 1.0-alpha-3
5: 1.0-alpha-4
Choose a number: : 5
Define value for property 'groupId': : com.mayabansi.webappdemo
Define value for property 'artifactId': : MavenSpringHibernateMockitoDemo
Define value for property 'version': 1.0-SNAPSHOT:
Define value for property 'package': com.mayabansi.webappdemo:

(NOTE: I have left out the rest of standard maven output for brevity)

Above command created the project structure below:

rhasija@rhasija-desktop ~/workspace/blog_projects/MavenSpringHibernateMockitoDemo $ find . 
.
./src
./src/main
./src/main/webapp
./src/main/webapp/index.jsp
./src/main/webapp/WEB-INF
./src/main/webapp/WEB-INF/web.xml
./src/main/resources
./pom.xml

rhasija@rhasija-desktop ~/workspace/blog_projects/MavenSpringHibernateMockitoDemo $ 

Step #2: Add basic project to source code control.

Step #3: Make project ready for IntelliJ Idea/Eclipse.

Step #4: Add JBoss Remote Repository as alternate remote repository for Hibernate artifacts.

Step #5: Add Hibernate related dependencies.

Step #6: Add Enterprise Bundle repository to settings.xml

Step #7: Add Spring MVC related dependencies.

Step #8: Add Mockito dependency.

NOTE: The example is complete. I will keep adding to it as I learn new things so as to make the example project richer.

The entire project can be downloaded from GitHub or you can use Git to checkout the project:

git clone git@github.com:RaviH/MavenSpringHibernateMockito.git