Eclipse中创建Maven webapp项目
2017年11月27日 12:14:54
阅读数:120
1、打开eclipse,点击右键new,如下图找到maven project
- 1
2、选择maven project,点击Next> 显示创建maven项目的窗口,勾选Create a simple project,如图所示 点击Next>
- 1
3、输入maven项目的基本信息,如图所示,填写完毕, 点击Finish
- 1
4、完成maven项目的创建,生成相应的maven项目,如下所示,创建好后有部分结构是项目不需要的,我们需要删掉:
- 1
5、选择项目,右键选择Properties,进入属性页面,找到Maven菜单,如下图所示:
- 1
6、选择java版本为1.7以上,这里我选择1.8,并去掉其他一项,如下图:
- 1
7、点击ok之后,回到项目,此时项目符合我们想要创建的maven项目
- 1
8、此时webapp下的结果还没有显示出来,再次进去Properties配置,如下图:
- 1
9、点击Further configuration available...,如下:
- 1
10、配置src/main/webapp,并勾选下面选项生成web.xml的选项,如图所示:
- 1
11、确定并应用之后,如下所示
- 1
12、返回到maven菜单下去掉Dynamic Web Module的勾选,点击ok,
- 1
13、此时还需要配置,src/main/webapp为 “/” 根目录,如图所示:
- 1
14、完成如上配置后,最后完成maven webapp项目结构如下图所示:
- 1
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_38283270/article/details/78643378
环境jre1.8 ,首先New-Maven-Project创建webapp项目,创建成功之后,右击点击properties,java-build-path 修改选择你的jre环境(我的是1.8),进入java-compiler修改为1.8,在进入project-facets,把java修改为1.8,Dynamic为3.1(tomcat为8.5),出现jsp出错,在pom.xml文件加入
javax.servlet.jsp-api和javax.servlet-api包
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
提示信息应该能看懂。也就是缺少了web.xml文件,<failOnMissingWebXml>被设置成true了。
搜索了一下,Stack Overflow上的答案解决了问题,分享一下。
目前被顶次数最多的回答原文如下:
This is a maven error. It says that it is expecting a web.xml file in your project because it is a web application, as indicated by <packaging>war</packaging>
. However, for recent web applications a web.xml file is totally optional. Maven needs to catch up to this convention. Add this to your maven pom.xml
to let maven catch up and you don't need to add a useless web.xml
to your project:
大意是说这是一个Maven错误,在最近的web应用开发中web.xml文件已经变得可有可无了。不过Maven还没有跟上这一变化,我们只要在pom.xml文件中手动添加如下配置:
-
<build>
-
<plugins>
-
<plugin>
-
<groupId>org.apache.maven.plugins</groupId>
-
<artifactId>maven-war-plugin</artifactId>
-
<version>2.6</version>
-
<configuration>
-
<failOnMissingWebXml>false</failOnMissingWebXml>
-
</configuration>
-
</plugin>
-
</plugins>
-
</build>
“告知”maven即可。这样做的好处是我们不必在项目生中成一个无用的web.xml文件。在更新版本(具体从哪一个版本开始我也不知道~~)的maven中已经不存在web.xml文件缺失的问题,我们只需要处理<failOnMissingWebXml>被设置成tue的问题即可。也就是在pom.xml中添加如下配置即可。
-
<properties>
-
<failOnMissingWebXml>false</failOnMissingWebXml>
-
</properties>
其他方案:(生成web.xml——>部署运行时文件路径)
you can do it also like this:
- Right click on Deployment Descriptor in Project Explorer.
- Select Generate Deployment Descriptor Stub.
It will generate WEB-INF folder in src/main/webapp and an web.xml in it.
这是被采纳方案,也就是说在项目浏览器中右键单击Deloyment Descriptor,选择生成部署描述符存根:Generate Deployment Descriptor Stub。
操作后会在项目的src/main/webapp/WEB-INF目录下生成web.xml文件。我按照这个方法生成了web.xml文件,但是仍然报错。如果你也是的话可以接着往下看。
Here are the steps you can follow:
- You can check this by right clicking your project, and open its Properties dialogue, and then "Deployment Assembly", where you can add Folder /src/main/webapp. Save the setting, and then,
- Go to Eclipse menu Project -> Clean... and clean the project, and the error should go away.
此时需要对src/main/webapp文件进行运行时的路径部署,以保证服务器正常运行。
也就是说需要部署一下src/main/webapp(刚生成的web.xml就在该文件夹下)文件夹,Deployment的作用是会在运行时将相应的resource复制到web服务器的相应目录下(通过Deploy Path指定),保证web应用正常运行。
经过这两步,问题解决。