Create a project using Maven in Eclipse (GO)

Source: Internet
Author: User
Tags java web

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.
    • To install maven:
  1. 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;
  2. 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;
  3. 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;
  4. 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 :
    1. 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/;
    2. 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,;
    3. 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 :
  1. Create Maven WebApp:file>new>maven project>next archetype select maven-archetype-webapp Next, fill in the project information , Finish,;
  2. 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;
  3. 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:
    01 <plugins>
    02    <plugin>
    03     <artifactId>maven-compiler-plugin</artifactId>
    04     <version>3.0</version>
    05     <configuration>
    06      <source>1.6</source>
    07      <target>1.6</target>
    08     </configuration>
    09    </plugin>
    10 </plugins>
    Right-click Projectmaven>update ProjectUpdate the project, and thenProperties>project Facets, you willDynamic Web ModuleSwitch3.0JavaSwitch1.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 publicclass HelloMavenWebappServlet extendsHttpServlet{
03  privatestatic final longserialVersionUID = 8963265462953694987L;
04  @Override
05  publicvoiddoGet(HttpServletRequest req, HttpServletResponse resp)
06    throwsServletException, IOException {
07   resp.getWriter().println("Get:/helloMavenWebapp");
08  }
09  
10  @Override
11  publicvoiddoPost(HttpServletRequest req, HttpServletResponse resp)
12    throwsServletException, IOException {
13   resp.getWriter().println("Post:/helloMavenWebapp");
14  }
15 }
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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.