[TOC]

Maven Web项目之Hello-World

Step 1.Eclipse新建Maven项目 PATH:File > New (ALT + Shift + N) -> Maven Project
Step 2.选择AarchType 为 Web工程: Maven-archetype-webapp 1.0 , 其参数Artifact Id 为 hello-world 然后点击完成即可

WeiyiGeek.Maven-archetype-webapp

WeiyiGeek.Maven-archetype-webapp

Step 3.创建后项目会报,所以我们需要配置jdk环境右键工程- build path - configration build path设置当前环境的jdk版本;

Step 4.打开 pom.xml 文件进行如下修改,添加编译环境和servlet接口包, 如果此时报错信息没有消失 右键工程 -> Maven -> update project;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<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>
<groupId>com.weiyigeek.main</groupId>
<artifactId>hello-world</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>hello-world Maven Webapp</name>
<url>http://maven.apache.org</url>

<!-- 系统依赖包 -->
<dependencies>
<!-- 单元测试依赖包 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

<!-- servlet接口包-运行环境包 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>

</dependencies>
<build>
<plugins>
<!-- 配置java8 编译环境 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>

<!-- 添加Tomcat插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<!-- Config: contextPath and Port (Default - /HelloSpringMVC : 8080) -->
<configuration>
<path>/</path>
<port>80</port>
<uriEncoding>UTF-8</uriEncoding><!-- 非必需项 -->
</configuration>
</plugin>
</plugins>

<finalName>hello-world</finalName>
</build>
</project>

Step 5. 修改 /hello-world/src/main/webapp/index.jsp 内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<%@ page language="java" import="java.util.*" Encoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv='Content-Type' content='Type=text/html; charset=utf-8'>
<meta name="author" content="WeiyiGeek" />
<meta http-equiv="content-language" content="zh-CN"/>

<title>WeiyiGeek Blog | 为了能到远方,脚下的每一步都不能少。</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="keywords" content="WeiyiGeek,网络安全,系统运维,安全测试,物联网与应用开发,网络技术,学习心得分享,唯一极客,DIY极客">
<meta name="Description" content="WeiyiGeek Blog-关注于网络安全_物联网安全开发_网络运维,分享技术学习心得与入坑,提升自我网络安全技术与能力,立志维护大众网络安全为己任--中国梦,我的梦">
<meta name="generator" content="Hexo 4.2.1"><link rel="alternate" href="/atom.xml" title="WeiyiGeek Blog" type="application/atom+xml">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
<h1>Maven - Hello World</h1>
<P>访问时间: <span id="time"></span></P>
<%
String SERVER_NAME = request.getServerName();
String SERVER_ADDR = request.getLocalAddr();
String SERVER_SOFTWARE = getServletContext().getServerInfo();
String SCRIPT_NAME = request.getServletPath();
String DOCUMENT_ROOT = request.getRealPath("/");
String REMOTE_HOST = request.getRemoteHost();
String REMOTE_ADDR = request.getRemoteAddr();
String HTTP_USER_AGENT = request.getHeader("User-Agent");
%>

<p> Server : <%=SERVER_SOFTWARE%> | <%=SERVER_ADDR%> </p>
<p> Client : <%=REMOTE_HOST%> | <%=REMOTE_ADDR %></p>
<p> Document_Root : <%= DOCUMENT_ROOT %> <br/><br/> URL : <%=SERVER_NAME %><%= SCRIPT_NAME %> </p>

<script>
var d = new Date();
document.getElementById("time").innerHTML=d;
</script>
</body>
</html>

Step 6.工程项目->Run AS -> Maven Build -> 在弹出的goals框中输入 tomcat7:run 点击运行如下图所示

WeiyiGeek.tomcat7:run

WeiyiGeek.tomcat7:run

PS : 在运行Maven Goal的时候一定要注意把 tomcat:run 改成 tomcat7:run,否则还是会运行默认的 Tomcat 6。否则将出现 org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException错误

Step 7.Maven 项目运行示例:

WeiyiGeek.Maven-HelloWorld

WeiyiGeek.Maven-HelloWorld

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 <repositories>
<repository>
<id>nexus</id>
<url>http://maven.weiyigeek.top:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>

<!-- 设定插件仓库 -->
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<url>http://maven.weiyigeek.top:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>

https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api

有些时候打包的时候需要指定Maven settings.xml(譬如从内部Nexus 私服获取依赖),如下:

mvn clean package -U –settings F:\Maven\settings.xml -Dmaven.test.skip=true
Maven 实用小技巧

Maven 打包跳过测试 通过 使用参数-DskipTests和-Dmaven.test.skip=true 指定,如下:

mvn clean package -Dmaven.test.skip=true

或者:

mvn clean package -DskipTests

这两个参数的主要区别是:

-DskipTests,不执行测试用例,但编译测试用例类生成相应的class文件至target/test-classes下。
-Dmaven.test.skip=true,不编译测试用例类,也不执行测试用例

作者:FX_SKY
链接:https://www.jianshu.com/p/ac4c69525e6c
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。