Transferred from: http://www.gogogogo.me/development/eclipse-maven-webapp.html Apache maven is an excellent project building and management tool, and many open source projects are built using MAVEN. Since MAVEN is being used in recent work, it is noted here that the process of creating a Java Web project based on Servlet3.0 with the Maven plugin in Eclipse is welcome.
- download maven: Download Maven3.1.1 on the official Apache website http://maven.apache.org/download.cgi, and place it in the e:/maven directory after decompression e:\ maven\apache-maven-3.1.1;
- Configure environment Variables : On my Computer > Properties > Advanced > Environment Variables > System Variables > NEW: variable name :m2_home , variable value :F:\maven\apache-maven-3.0.3, then add the trailing value at the end of the path environment variable:;%m2_home%\bin;
- Check the configuration : Run >cmd, enter at the command line: the mvn-v command checks the configuration, MAVEN needs to use the JDK at runtime, so installing the JDK is a prerequisite. If no error message is prompted, MAVEN installation is complete;
- Modify Warehouse location : Maven default warehouse Location the . M2/repository directory for the current user directory, configuring the warehouse location to E:/maven under/repository for ease of administration directory, open the settings.xml under E:\maven\apache-maven-3.1.1\conf with a text editor, and add the location of the local repository under the <settings> node <localRepository>E:/maven/repository</localRepository> ,;
Here, MAVEN is installed and you can use MAVEN at the command line, and the next thing to do is integrate Maven into eclipse.
- Eclipse Installation m2e plugin :
- Install M2E plugin: Eclipse Kepler later versions are integrated M2E plug-ins, if no integration can be downloaded to the M2E official website installation: http://www.eclipse.org/m2e/;
- Configure the M2E plugin: After installing the plug-in, remove the m2e built-in Maven in window>preferences>maven>installations , click the Add button on the right, select the top installed apache-maven-3.1.1 , then tick, apply,;
- Configure user settings: Copy the settings.xml file under E:\maven\apache-maven-3.1.1\conf to the E:/maven directory and rename it to User_ Settings.xml , Select the file in Window>preferences>maven>user settings , of course, the file only configures the warehouse location, you can also configure other parameters, This configuration file is a user profile , and/conf settings.xml is the global profile of Maven;
By integrating Maven into eclipse, you can use MAVEN with the visual interface of Eclipse.
- to create a MAVEN WebApp project that supports Java Servlet3.0 :
- Create Maven WebApp:file>new>maven project>next archetype select maven-archetype-webapp Next, fill in the project information , Finish,;
- Refine the project SRC directory structure: Because of the standard MAVEN WebApp project src/(Main (Java, resources, WebApp), (Test (Java, Resources)), The created project is missing the Java directory under Main, and there is no test directory, which is created separately;
- Modify the version of WebApp: Version 2.3 of WebApp created by default, which needs to be modified to support Servlet3.0 webapp3.0. Find the Hellomaven project in workspace, edit/hellomaven/.settings/org.eclipse.wst.common.project.facet.core.xmlWill<installed facet= "java" version= "1.5"/>, <installed facet= "Jst.web" version= " 2.3"/>Switch<installed facet= "java" version= "1.6"/>, <installed facet= "Jst.web" version= " 3.0"/>。 Then eclipse opens the project'sPom.xmlFile, add the following configuration under the <build> node:
03 |
< artifactId >maven-compiler-plugin</ artifactId > |
04 |
< version >3.0</ version > |
Right-click Projectmaven>update ProjectUpdate the project, and thenProperties>project Facets, you willDynamic Web ModuleSwitch3.0、JavaSwitch1.6RightruntimesChooseTomcat7.0(Servlet3.0 need tomcat7.0+),.
Finally, delete
Src/main/webapp/web-inf/web.xmlAgain
maven>update ProjectUpdate the project. 4. Create a servlet test: Create a Servlet3.0-based hellomavenwebappservlet with the code below to deploy the project to TOMCAT7 launch and access Http://localhost:8080/HelloMaven /hellomavenwebapp, page output
Get:/hellomavenwebapp。 View Source print?
01 |
@WebServlet ( "/helloMavenWebapp" ) |
02 |
public class HelloMavenWebappServlet extends HttpServlet{ |
03 |
private static final long serialVersionUID = 8963265462953694987L; |
05 |
public void doGet(HttpServletRequest req, HttpServletResponse resp) |
06 |
throws ServletException, IOException { |
07 |
resp.getWriter().println( "Get:/helloMavenWebapp" ); |
11 |
public void doPost(HttpServletRequest req, HttpServletResponse resp) |
12 |
throws ServletException, IOException { |
13 |
resp.getWriter().println( "Post:/helloMavenWebapp" ); |
Finally, the creation of a Java Web project built with Maven is successful, and more about MAVEN configuration and use is being learned, so please correct me!
Create a project using Maven in Eclipse (GO)