"Maven" Maven downloads and configures, modifies its local repository, and hangs on to eclipse for Javeee to build a Servlet3.0 HelloWorld

Source: Internet
Author: User

Let's start by saying what Maven is. Often listen to the MAVEN project looks like a tall, in fact maven is just a jar download tool. MAVEN configuration of the project, in the program Ape's development machine can be networked, you can download the program ape the jar package, save the program ape himself on the various foreign official website, endure the speed of the card explosion, find jar bag of egg pain. Because MAVEN is an Apache project, Apache has a service site around the world, so it's nice to have a jar package through Maven. At the same time, some Struts2 and other foreign projects are also beginning to be proud, on the official website all only provide their own Maven dependency, let the program ape themselves through this maven rely on Maven download, so that they can update their jar in time. Well, Javaweb's entry threshold is high again, and you have to figure out what Maven is. Actually, I can't.

One, maven download

Here first of all, this thing is configured in fact with the Java JDK configuration, basically is the same. You've got a Java jdk and you're going to be maven. JDK1.7 can be configured to refer to the download, installation, and deployment of the Java JDK (click the Open link). In fact, as with eclipse directly with the JDK, Eclipse comes with the Maven plugin directly, but you'd better have the latest Maven for Eclipse, or there will always be such a problem.

First go to Maven's official website, find the bin version of the zip package, Linux is tar.gz, download maven. Http://maven.apache.org/download.cgi such as:

It is: Http://apache.dataguru.cn/maven/maven-3/3.3.3/binaries/apache-maven-3.3.3-bin.zip. If, your network like Me egg ache, can throw directly to download the tool to download.

Ii. Configuration of Maven

1, after the download, decompression, select the path to the main, this decompression directory is your MAVEN working directory, do not find a location. Also note the case of folder sets folder. That is, after the decompression, first hit the Kaixie after the folder, see if it is like under the Bin,boot,conf,jar,lib folder, while creating a new jar package, Open the Setting.xml in conf and change MAVEN's local repository path, which is where Maven downloads will be downloaded.

In the Conf setting.xml find the position of the 55th line, in the inside of the non-annotated place, add:

<localRepository> the path where you want maven to download the jar, which is the path to the MAVEN local repository </localRepository>

For example, here is the Jar folder I just created:

2, after right-click computer/My Computer, Management/properties, advanced system settings (WinXP not), locate the Advanced tab, environment variables.

For example, create a new one after opening:

Variable name: maven_home

The variable value is the path of the maven you just unzipped, here is E:\Program files\apache-maven-3.3.3

Identify and then add a%maven_home%\bin to the path variable, such as when you are configuring Java:

Afterwards various to determine.

3, run cmd Open the command line, enter the following command, see the following results prove that MAVEN is configured successfully under Windows!


Third, configure MAVEN to eclipse for Java EE

1. Open Eclipse for Javeee,windows->perference->maven->installations->add. ->directory ... Select just the MAVEN path and click OK

2. Check the maven you added and remove the hook from Eclipse's own maven.

3. Then modify the MAVEN configuration file in Eclipse in user settings. For example, find the MAVEN installation path that we just modified/conf/setting.xml. If you don't change it, maven in eclipse will follow your own configuration. Download the jar package, or not go to the jar directory we just created. then click OK. At this point, Maven successfully hangs in eclipse for Java EE.

Iv. creating a Servlet3.0 maven project and its Hello World

1, for example, a new MAVEN project

2, work space according to the default can

3, after, for example, select a Maven-archetype-quickstart filter, the snapshot level of the MAVEN project can be. There is nothing that we do ourselves, clothed.

4, then enter the group ID and artiface ID. The MAVEN project requires all Java packages to conform to the Java Engineering specification, which is just a prefix for all package names in your project. It's no big deal. Then click OK, according to the different speed, small card for a while. The MAVEN project is officially established. The jar package is not automatically updated in the future as long as the Pom.xml,maven under the project are not modified.

5, after the new project do not do anything, first right-click the project properties, we also want to configure this MAVEN project some things. In deployment assembly set up the Java project to automatically include Maven-dependent packages, such as:

6. Then, in Java build path, change the compiled Java to a JDK1.7 that is already in eclipse.

7. Then add the Dynamic Web page module in project facets and add the running environment of the already configured Tomcat in the running environment. Indicates that this is a Java EE project. Well, at this point, we can finally make sure.

8. After opening the Pom.xml file, in the Pom.xml view, delete the dependency of the JUnit test private package that is not, and delete the test-specific Java files, such as:

Modify the Pom.xml file as follows, add the Servlet3.0 dependency, save the exit, and eclipse will automatically download the corresponding Servlet3.0 package Javax.servlet-api-3.0.1.jar and add it to this project.

<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/xsd/maven-4.0.0.xsd" >< modelversion>4.0.0</modelversion><groupid>com</groupid><artifactid>test</ artifactid><version>0.0.1-snapshot</version><packaging>jar</packaging>< Properties><project.build.sourceencoding>utf-8</project.build.sourceencoding></properties ><dependencies><!--ask maven to download the Servlet3.0 jar package--><dependency><groupid>javax.servlet </groupId><artifactId>javax.servlet-api</artifactId><version>3.0.1</version>< /dependency></dependencies></project>
After that, is Servlet3.0, simple can not be simpler content, in WebContent in the Web-inf in the new Web. Xml as follows:

<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee/http Java.sun.com/xml/ns/javaee/web-app_3_0.xsd "version=" 3.0 "></web-app>

Create a new index.jsp as follows:

<%@ 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" >
Then create a new Hello.java in Src/main/java as follows:

Package Com.test;import Java.io.*;import Javax.servlet.*;import javax.servlet.http.*;import javax.servlet.annotation.*; @WebServlet (name= "Hello", urlpatterns={"/hello"}) public class Hello extends HttpServlet { Private static final Long serialversionuid = 1l;protected void Service (HttpServletRequest request,httpservletresponse Response) throws Servletexception, IOException {printstream out = new PrintStream (Response.getoutputstream ()); Response.setcontenttype ("Text/html;charset=utf-8"); Out.print ("Hello World");}}
The final directory structure is as follows:

Then mount the project to Tomcat and run the results as follows:

"Maven" Maven downloads and configures, modifies its local repository, and hangs on to eclipse for Javeee to build a Servlet3.0 HelloWorld

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.