Create Spring MVC dynamic web project with Maven and make it support Eclipse IDE

来源:互联网 发布:c语言其实很简单百度云 编辑:程序博客网 时间:2024/05/16 13:43
In this particular blog we will discuss how to create a Spring Web Project in Maven and how to make it support eclipse IDE.

Before we start lets make sure we have all required tools available :
Maven 3.0.5
Eclipse 4.2
JDK 7
Spring 3.2.0 Release
Tomcat 7


Make sure you have a working installation of Maven in your machine, to check this supply following command to your command line: 

C:\Users\nagesh.chauhan>mvn -version



Create Dynamic Web Project in Maven


To create dynamic web project with maven, navigate to the folder where you want to store the project and supply following command:

C:\Users\nagesh.chauhan\maven-project>mvn archetype:generate -DgroupId=com.beingjavaguys.sample -DartifactId=SampleSpringMaven -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false


What is groupId in maven ?

groupId identifies a particular project uniquely across all projects, so we should follow an naming convention. A very simple and commonly used way of doing this is to use reverse of your domain, i.e. com.beingjavaguys.maven. 

A good way of maintaining the integrity of groupId is to use the project structure. In case the project is consists of multiple modules than every module should append an identifier to the parent groupId. i.e. com.beingjavaguys.maven, com.beingjavaguys.spring, com.beingjavaguys.struts .. etc. 


What is artifactId in maven ?

artifactId is the name of war file without version, if you are creating it by yourself you are free to took any name of your choice in lower case and without any strange symbol. But if this is a third party jar than we have to take the name of jar as suggested by it’s distribution. 


What is archetype in maven ?

Archetype is a Maven project templating toolkit which tells the maven the type of project we are going to create. Archetype enables the maven to create a template project of user’s choice so that the user can get the project up and running instantly. 

“archetype:generate”  generates a new project from provided archetype or update the actual project if using a partial archetype. Maven provides a number of predefined archtypes, see more details from Maven Documentation.


What is archetypeArtifactId in maven ?

While creating a new project we provide the archetypeArtifactId that informs maven about what archetype to use to create the initial structure of the project. Maven looks it up from the archetypeCatalog and works accordingly. eg. if we want to create a simple web-app project we specify -DarchetypeArtifactId=maven-archetype-webapp. 


Convert Maven project to support Eclipse IDE


Here we are done with creating a dynamic web project in maven, now lets make this project compatible to Eclipse IDE. To make maven project support eclipse ide navigate to project folder and supply following command : 

C:\Users\nagesh.chauhan\maven-project\SampleSpringMaven>mvn eclipse:eclipse -Dwtpversion=2.0

Now Import the project into eclipse, you will see a folder structure like this : 




Create Spring MVC project in Eclipse using Maven



Now we are done with creating a simple dynamic web project in maven and imported it in eclipse. Lets add some required files to it to make it a Spring MVC project. 

Update pom.xml

SampleSpringMaven\pom.xml 
view plainprint?
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <groupId>com.beingjavaguys.sample</groupId>  
  5.     <artifactId>SampleSpringMaven</artifactId>  
  6.     <packaging>war</packaging>  
  7.     <version>1.0-SNAPSHOT</version>  
  8.     <name>SampleSpringMaven Maven Webapp</name>  
  9.     <url>http://maven.apache.org</url>  
  10.   
  11.     <properties>  
  12.         <spring.version>3.0.5.RELEASE</spring.version>  
  13.         <jdk.version>1.6</jdk.version>  
  14.     </properties>  
  15.   
  16.     <dependencies>  
  17.   
  18.         <dependency>  
  19.             <groupId>org.springframework</groupId>  
  20.             <artifactId>spring-core</artifactId>  
  21.             <version>${spring.version}</version>  
  22.         </dependency>  
  23.   
  24.         <dependency>  
  25.             <groupId>org.springframework</groupId>  
  26.             <artifactId>spring-web</artifactId>  
  27.             <version>${spring.version}</version>  
  28.         </dependency>  
  29.   
  30.         <dependency>  
  31.             <groupId>org.springframework</groupId>  
  32.             <artifactId>spring-webmvc</artifactId>  
  33.             <version>${spring.version}</version>  
  34.         </dependency>  
  35.   
  36.         <dependency>  
  37.             <groupId>junit</groupId>  
  38.             <artifactId>junit</artifactId>  
  39.             <version>3.8.1</version>  
  40.             <scope>test</scope>  
  41.         </dependency>  
  42.     </dependencies>  
  43.   
  44.     <build>  
  45.         <finalName>SampleSpringMaven</finalName>  
  46.         <plugins>  
  47.             <plugin>  
  48.                 <groupId>org.apache.tomcat.maven</groupId>  
  49.                 <artifactId>tomcat7-maven-plugin</artifactId>  
  50.                 <version>2.1</version>  
  51.                 <configuration>  
  52.                     <url>http://localhost:8080/manager/text</url>  
  53.                     <server>my-tomcat</server>  
  54.                     <path>/SampleSpringMaven</path>  
  55.                 </configuration>  
  56.             </plugin>  
  57.             <plugin>  
  58.                 <groupId>org.apache.maven.plugins</groupId>  
  59.                 <artifactId>maven-compiler-plugin</artifactId>  
  60.                 <version>3.0</version>  
  61.                 <configuration>  
  62.                     <source>${jdk.version}</source>  
  63.                     <target>${jdk.version}</target>  
  64.                 </configuration>  
  65.             </plugin>  
  66.         </plugins>  
  67.     </build>  
  68.   
  69. </project>  


Run Spring Maven project in eclipse


Now to make the project run on server and detect required jars in eclipse, run following command in project directory again : 

C:\Users\nagesh.chauhan\maven-project\SampleSpringMaven>mvn eclipse:eclipse -Dwtpversion=2.0

this will add all required jars to eclipse's deployment assembly, as shown in the figure below: 


This is the most critical step in importing maven web app in eclipse process, make sure all jar files are referenced here as shown in above image. If not, run "mvn eclipse:eclipse -Dwtpversion=2.0" command again. 

Here we are all done with importing a working spring mvc maven project in eclipse. Now lets add some source code to our project: 

Update web.xml

src\main\webapp\WEB-INF\web.xml
view plainprint?
  1. <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  3.           http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  4.     version="2.5">  
  5.   
  6.     <display-name>Sample Spring Maven Project</display-name>  
  7.   
  8.     <servlet>  
  9.         <servlet-name>mvc-dispatcher</servlet-name>  
  10.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  11.         <load-on-startup>1</load-on-startup>  
  12.     </servlet>  
  13.   
  14.     <servlet-mapping>  
  15.         <servlet-name>mvc-dispatcher</servlet-name>  
  16.         <url-pattern>*.htm</url-pattern>  
  17.     </servlet-mapping>  
  18.   
  19.     <context-param>  
  20.         <param-name>contextConfigLocation</param-name>  
  21.         <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>  
  22.     </context-param>  
  23.   
  24.     <listener>  
  25.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  26.     </listener>  
  27. </web-app>  


Add mvc-dispatcher-servlet.xml

\src\main\webapp\WEB-INF\mvc-dispatcher-servlet.xml
view plainprint?
  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:context="http://www.springframework.org/schema/context"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="  
  5.         http://www.springframework.org/schema/beans       
  6.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  7.         http://www.springframework.org/schema/context   
  8.         http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
  9.    
  10.     <context:component-scan base-package="com.beingjavaguys.controller" />  
  11.    
  12.     <bean  
  13.         class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  14.         <property name="prefix">  
  15.             <value>/WEB-INF/pages/</value>  
  16.         </property>  
  17.         <property name="suffix">  
  18.             <value>.jsp</value>  
  19.         </property>  
  20.     </bean>  
  21.    
  22. </beans>  


Update index.jsp

\src\main\webapp\index.jsp
view plainprint?
  1. <html>  
  2. <head>  
  3. <script type="text/javascript" src="jquery-1.2.6.min.js"></script>  
  4. <title>Being Java Guys | Hello World</title>  
  5. </head>  
  6. <body>  
  7.       
  8.     <center>  
  9.         <h2>Being Java Guys | Hello World</h2>  
  10.         <h4>  
  11.             <a href="hello.htm">Click Here</a>  
  12.         </h4>  
  13.     </center>  
  14. </body>  
  15. </html>  


Add hello.jsp

\src\main\webapp\WEB-INF\pages\hello.jsp
view plainprint?
  1. <html>  
  2. <head>  
  3. <script type="text/javascript" src="jquery-1.2.6.min.js"></script>  
  4. <title>Being Java Guys | Hello World</title>  
  5. </head>  
  6. <body>  
  7.   
  8.     <center>  
  9.         <h2>Being Java Guys | Hello World</h2>  
  10.         <h4>${message}</h4>  
  11.     </center>  
  12. </body>  
  13. </html>  

<br

Adding a Simple Controller code

\src\main\java\com\beingjavaguys\controller\Home.java
view plainprint?
  1. package com.beingjavaguys.controller;  
  2.   
  3. import org.springframework.stereotype.Controller;  
  4. import org.springframework.web.bind.annotation.RequestMapping;  
  5. import org.springframework.web.servlet.ModelAndView;  
  6.   
  7. @Controller  
  8. public class Home {  
  9.     String message = "Welcome to your 1st Maven Spring project !";  
  10.   
  11.     @RequestMapping("/hello")  
  12.     public ModelAndView showMessage() {  
  13.         System.out.println("from controller");  
  14.         return new ModelAndView("hello""message", message);  
  15.     }  
  16. }  


Here we are all done with adding all required files to our spring project, now the complete project structure will look something like this:


Run the project on server (Right Click > Run As > Run on Server), and you will get screens like : 





Deploying Spring Maven application war to apache tomcat using tomcat-maven plugin.


Till now we have created a spring maven project and imported in eclipse. We also run the project from eclipse. Now lets see how to create a war file uisng maven and deploy it to apache tomcat server directly using tomcat7-maven plugin. Deploying maven created war file from eclipse to external server is a three step process. 

Step 1 : Add tomcat-maven plugin entry to pom.xm

view plainprint?
  1. <build>  
  2.   <finalName>SampleSpringMaven</finalName>  
  3.   <plugins>  
  4.    <plugin>  
  5.                 <groupId>org.apache.tomcat.maven</groupId>  
  6.                 <artifactId>tomcat7-maven-plugin</artifactId>  
  7.                 <version>2.1</version>  
  8.                 <configuration>  
  9.                     <url>http://localhost:8080/manager/text</url>  
  10.                     <server>my-tomcat</server>  
  11.                     <path>/SampleSpringMaven</path>  
  12.                 </configuration>  
  13.             </plugin>  
  14.    <plugin>  
  15.    ....  
  16.    ....  
  17.    </plugin>  
  18.    </plugins>  
  19.    ....  
  20.    ....  
  21.    </build>  
We have already added the plugin in pom.xml showed above. 

Step 2 : Add a user entry to '..\..\apache-tomcat-7.0.41\conf\tomcat-users.xml' file in your server.

view plainprint?
  1. <role rolename="manager-gui"/>  
  2. <role rolename="manager-script"/>  
  3. <user username="managerGui" password="manager" roles="manager-gui"/>  
  4. <user username="manager" password="manager" roles="manager-script"/>   

Step 3 : Add a 'settings.xml' to your '.m2' folder

view plainprint?
  1. <settings>  
  2.     <servers>  
  3.         <server>  
  4.             <id>my-tomcat</id>  
  5.             <username>manager</username>  
  6.             <password>manager</password>  
  7.         </server>  
  8.     </servers>  
  9. </settings>  
Thats it, now lets deploy our application 'war' file directly to external tomcat server. 
1 - Start your targeted tomcat server.
2 - Navigate to your project's parent folder and supply following commands.

C:\Users\nagesh.chauhan\maven-project\SampleSpringMaven>mvn install -Dmaven.test.skip=true

C:\Users\nagesh.chauhan\maven-project\SampleSpringMaven>mvn clean

C:\Users\nagesh.chauhan\maven-project\SampleSpringMaven>mvn package

C:\Users\nagesh.chauhan\maven-project\SampleSpringMaven>mvn tomcat7:deploy


Now open browser and hit 'http://localhost:8080/SampleSpringMaven/' you will get your allpication deployed on server. Every time you make a change in your code apply last three steps 'mvn clean','mvn package' and 'mvn tomcat7:deploy'. You can use 'mvn tomcat7:redeploy' instead of 'mvn tomcat7:deploy' if case of re-deploying same app's war again on server. 

In this particular blog we came across 'how to create a spring web project in maven' and 'how to deploy spring maven project war to tomcat' in upcoming blogs we will see more about 'Maven', 'Spring', 'Java' and other opensource technologies. 






Thanks for reading !
0 0
原创粉丝点击