本教程将教您如何在Eclipse中创建 Archetype为 maven-archetype-webapp的Maven项目,也就是web工程。

创建Maven工程

第一步,启动Eclipse,依次打开菜单【File】【New】【Other】

Maven Web 工程_IT

找到目录Maven,选择Maven Project,点击【Next】

Maven Web 工程_IT_02

点击【Next】

Maven Web 工程_IT_03

选择一个Archetype。这里创建Web工程,选择maven-archetype-webapp

点击【Next】

Maven Web 工程_IT_04

最后点击【Finish】,将生成一个maven项目。项目的图标左上角有一个m字母。

随后将创建工程。工程结构如下:

Maven Web 工程_IT_05

解决问题


创建后,报几个问题。

这样解决,打开pom.xml,添加以下东东。

1、添加插件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.6</version>
    <configuration>
        <failOnMissingWebXml>false</failOnMissingWebXml>
    </configuration>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
    </configuration>
</plugin>

2、添加serlvet依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
</dependency>
<dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>javax.servlet.jsp-api</artifactId>
    <version>2.3.1</version>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>

完整的pom.xml

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
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.liyongzhen</groupId>
    <artifactId>myweb</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>myweb Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.1</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>myweb</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

3、更新这个工程

Maven Web 工程_IT_06

右击工程名,在弹出的菜单中选择【Maven】【Upadte Project】

Maven Web 工程_IT_07

点击【OK】

到此项目创建完成,可以运行。

Maven Web 工程_IT_08

右击工程名,在弹出的菜单中选择【Run As】【Run on Server】

Maven Web 工程_IT_09

点击【Next】

Maven Web 工程_IT_10

最后点击【Finish】

运行效果

Maven Web 工程_IT_11


调整Servlet版本

当前主流Servlet开发或者说主流Java Web开发,Servlet的版本是3.1。(Servlet 4.0还未被大量采用)。

我们在添加依赖时,Servlet版本也是3.1,而maven工程创建时Servlet是2.3

Maven Web 工程_IT_12

 

右击工程名,在弹出的菜单中选择【Properties】

Maven Web 工程_IT_13

弹出对话框,选择【Project Facets】

Maven Web 工程_IT_14

找到 Dynamice Web Module,去掉前面勾选

Maven Web 工程_IT_15

将Serlvet版本更改为3.1,再点击【Apply】

Maven Web 工程_IT_16

再将Dynamice Web Module勾选上去。

Maven Web 工程_IT_17

最后点击【Apply and Close】

再看,更新过来。

Maven Web 工程_IT_18


如果有web.xml,可能是这样的

1
2
3
4
5
6
7
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 
<web-app>
  <display-name>Archetype Created Web Application</display-name>
</web-app>

这是Servlet2.3。需要换成Servlet3.1命名空间。

下面是Servlet3.1命名空间。将web.xml改成下面一样的命名空间。

1
2
3
4
5
6
7
8
9
10
<?xml version="1.0" encoding="UTF-8"?>
id="WebApp_ID" version="3.1">
  <display-name>myweb</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>