Maven创建web项目具体过程
最后更新于:2022-04-01 09:46:10
## 使用eclipse插件创建一个web project
首先创建一个Maven的Project如下图
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-03-01_56d50e97ef636.png)
我们勾选上Create a simple project (不使用骨架)
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-03-01_56d50e98092c0.png)
这里的Packing 选择 war的形式
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-03-01_56d50e981c030.png)
由于packing是war包,那么下面也就多出了webapp的目录
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-03-01_56d50e982c195.png)
由于我们的项目要使用eclipse发布到tomcat下面,这里我们需要先把项目转成dynamic web project
在我们的项目上点击右键,选择properties 并找到 Project Facets ,并点击Convert to faceted form… 如下图:
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-03-01_56d50e9839988.png)
然后勾选Dynamic Web Module 并点击ok 如下图:(3.0只有tomcat7才支持)
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-03-01_56d50e984c295.png)
接下来观察我们的项目结构,多了一个web content目录
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-03-01_56d50e9862614.png)
虽然此时我们可以发布到tomcat中,但这不符合maven的结构,我们还要做如下修改
把上图WebContent下面两个目录 META-INF ,WEB-INF 直接剪切到src/main/webapp目录下,并删掉WebContent目录,那么现在的项目结构如下图:
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-03-01_56d50e9872a0c.png)
然后我们要修改发布规则,右键点击项目, 选择 Deployment Assembly
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-03-01_56d50e98830df.png)
选择WebContent,把它remove掉,测试类我们也不需要发布,test的两个目录页可以remove
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-03-01_56d50e9891bfd.png)
接着重新指定一个web的路径,点击add,选择Folder -- 〉 next
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-03-01_56d50e989ebc9.png)
在src下找到webapp目录,然后finish
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-03-01_56d50e98b48bd.png)
最后一步,我们要把当前的build path 指向 Maven Dependency, 直接点击add,选择Java Build Path Entries 然后next
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-03-01_56d50e98c6ee6.png)
然后再点击finish完成
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-03-01_56d50e98d84de.png)
完成后如下图:
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-03-01_56d50e98e8c37.png)
至此一个基于maven的webapp就建立好了,并可以直接从eclipse中发布到tomcat中
补充:我们需要在src/main/webapp/WEB-INF下面创建一个web.xml
## 导入我们的Spring mvc依赖jar包
~~~
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.0.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.0.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.0.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.7.1</version>
</dependency>
</dependencies>
~~~
直接保存,maven就会自动为我们下载所需jar文件