Building Maven Projects with Eclipse

  In the previous " Maven Installation Tutorial Detailed ", I explained how to install Maven, but in the development process, most of our developers developed on Eclipse, so let's further explain how to use Eclipse to build Maven projects.

I. Introduction

  Maven, a project management and build automation tool, is being used by more and more developers to manage jar packages in projects. This article only describes how to install, configure and use Maven in Eclipse.

Installation of m2eclipse plugin in Eclipse

 

2. Eclipse Maven plug-in installation

  Now Eclipse has Maven by default. If there is, just ignore this step. If not, you need to add the m2eclipse plug-in to it. The following are the specific operation steps.

1. Plugin installation

        Select the "Help" menu "Instance NewSoftWare" menu item in the Eclipse menu bar in turn, and the following dialog window will pop up:


        Select the "Add.." button, and the following dialog box will pop up:

        This dialog is used to add a plugin address. Enter an identifying name for the operation in the input box corresponding to "Name". Enter the installation address of this plugin in the input box corresponding to "Location". Note: Maven's Eclipse plug-in address is: http://download.eclipse.org/technology/m2e/releases . After input, as shown below:


        After the input is completed, click the "OK" button in the lower right corner, and the following dialog box will pop up:


        Here you need to select the details of the plugin you want to install. Check the box in front of "Maven Integration for Eclipse". As shown below:


        After the selection is complete, click the "Next>" button at the bottom right to install. After the installation is successful, you will be prompted to restart Eclipse for this configuration to take effect. After restarting, this Eclipse can use the m2eclipse plug-in function.

 

3. Configuration

        After the plugin is installed, some configuration is required to start using Maven's features.

1. Set the Maven program associated with the m2eclipse plugin

        Select the "Window" -> "Preferences" menu item in the Eclipse menu bar in turn, and the following dialog box will pop up:


        Expand the "Maven" directory node in the navigation bar on the left side of this dialog and select the "Installations" subnode, as shown in the following figure:


        Here you need to associate the corresponding Maven installer. The specific operation is to select the "Add..." button in the right panel, and the following selection dialog box for selecting the Maven installation directory will pop up:


        Use this dialog to select the specific installation directory for Maven. After selecting, click the "OK" button. As shown below:



2. Set up a custom local warehouse

        Select the "UserSettings" sub-node under the "Maven" node of the menu tree on the left side of the dialog window as shown in the figure above. Its default configuration file is "C:\Documents and Settings\csdn\.m2\settings.xml", which needs to be modified for us to customize The settings.xml file under the warehouse location, the interface after the operation is as follows:


  Click the "OK" button in the lower right corner of the image above to complete all configuration changes.

 

4. Use Maven to build web projects

1. Create a Maven web project

        Some Maven boilerplate projects are provided by default in the m2eclispe plugin, which can be used to create projects that meet our requirements. The specific steps are as follows:

        Select the "File" -> "News" -> "Other" menu item in the eclipse menu bar, the following dialog window will open, and select the "Maven Project" entry under the "Maven" node in the window, as shown in the following figure:


        Click the "Next>" button to enter the following window:


        Select the "Next >" button again to enter the following window:


        In this window, select "maven-archetype-webapp" to create a Maven web project, and then select the "Next>" button to enter the following window:


        Enter the basic package name of the project in "Group Id" in the above window, and enter the project name in "Artifact Id", ignore other input boxes. Click the "Finish" button to complete the entire project creation process.

        After that, Maven will download the corresponding files and jar packages from the central repository according to the configuration of the project. After completion, there is the following project directory structure:


        As shown in the figure above, the "pom.xml" file is the maven configuration file for this project. Open this file and do the following configuration in "<build>":

<?xml version="1.0" encoding="UTF-8"?>
<!-- $Id: pom.xml 642118 2008-03-28 08:04:16Z reinhard $ -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <packaging>war</packaging>

  <name>testweb_mvn</name>
  <groupId>com.bijian</groupId>
  <artifactId>testweb_mvn</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <!-- Property configuration -->  
  <properties>  
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  </properties>
  
  <!-- Dependency configuration-->       
  <dependencies>  
    <!-- Add JUnit -->  
    <dependency>  
      <groupId>junit</groupId>  
      <artifactId>junit</artifactId>  
      <version>3.8.1</version>  
      <scope>test</scope>  
    </dependency>  
      
    <!-- Add Servlet -->  
    <dependency>    
        <groupId>javax.servlet</groupId>    
        <artifactId>servlet-api</artifactId>    
        <version>2.5</version>    
        <scope>provided</scope>    
    </dependency>
  </dependencies>
  
  <build>
    <plugins>
      <plugin>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>maven-jetty-plugin</artifactId>
        <version>6.1.7</version>
        <configuration>
          <connectors>
            <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
              <port>8888</port>
              <maxIdleTime>30000</maxIdleTime>
            </connector>
          </connectors>
          <webAppSourceDirectory>${project.build.directory}/${pom.artifactId}-${pom.version}</webAppSourceDirectory>
          <contextPath>/</contextPath>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

2. Prepare a small web application

  Click Java Resources, right-click, and create a new Source Folder.


        After it is built, it will look like the following.

  And simply got index.jsp and login.jsp from the Internet, the content is as follows:

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"  
    pageEncoding="UTF-8"%>  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
<html>  
    <head>  
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
        <title>Hello Maven</title>  
    </head>  
      
    <body>  
        <p>Hello everyone! </p>  
        <a href="user?action=login_input">去登录</a>  
    </body>
</html>

 login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"  
    pageEncoding="UTF-8"%>  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
<html>  
    <head>  
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
    <title>Login interface</title>  
    </head>  
      
    <body>
        <form action="user?action=login" method="post">  
            Name:<input type="text" name="name" />
            Password:<input type="password" name="password" />  
              
            <input type="submit" value="登录" />  
        </form>  
    </body>  
</html>

        Comment out the configuration content in applicationContext.xml and modify the content of web.xml as follows:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

  <servlet>  
    <servlet-name>UserServlet</servlet-name>
    <servlet-class>com.bijian.test.UserServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>UserServlet</servlet-name>  
    <url-pattern>/user</url-pattern>  
  </servlet-mapping>
</web-app>

   And create a new UserServlet.java as follows:

  The content is as follows:

package com.bijian.test;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UserServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");

		String action = request.getParameter("action");
		if ("login_input".equals(action)) {
			request.getRequestDispatcher("login.jsp")
					.forward(request, response);
		} else if ("login".equals(action)) {
			String name = request.getParameter("name");
			String password = request.getParameter("password");

			System.out.println("name->" + name + ",password->" + password);
		}
	}
}

3. Configure the tomcat service to run

  Configure the Tomcat service in Eclipse and add the testweb_mvn project created above. If you need to configure the Tomcat service, you can further refer to " How to Configure Environment Variables in Tomcat " and " Eclipse Quickly Build a Web Application Based on Tomcat Server ".


  Start the tomcat service, enter in the browser: http://localhost:8080/testweb_mvn/index.jsp, the effect of opening the page is as follows:


   Click the "Go to Login" link to enter the following page for entering your username and password.

   Enter the user name and password information, and click the "Login" button.

   The user's input information is output in the background of the server, as follows:

 

Reference article: http://blog.csdn.net/qjyong/article/details/9098213

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326195998&siteId=291194637