一、創建Maven Web 項目
1. 創建maven project工程
2. 選擇創建簡單工程
3. 填寫maven座標信息和打包方式
4. 點擊完成
5. 在webapp目錄下創建WEB-INF目錄,並在WEB-INF目錄下創建web.xml文件
web.xml 內容:
<?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"
id="WebApp_ID" version="3.0">
</web-app>
二、啓動web項目,並在首頁顯示 “你好,MAVEN”
1. 在pom.xml文件中添加servlet-api依賴,添加編譯插件和tomcat7插件
<dependencies>
<!--servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 添加編譯插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<!-- 添加tomcat7插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>9000</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
- 然後選中項目,右鍵:maven -> Update Project
2. 在webapp目錄下新建index.jsp頁面
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>Maven測試頁面</title>
</head>
<body>
你好,MAVEN
</body>
</html>
3. 運行maven-web項目
選中項目,右鍵:Run As -> Maven Build…
在彈出框的Goals欄輸入命令:tomcat7:run
然後點擊run按鈕,運行maven-web項目
在控制檯下會出現如下信息:
4. 在瀏覽器端訪問maven-web
測試成功!