maven项目搭建教程
https://blog.csdn.net/u010947191/article/details/51824647
https://www.cnblogs.com/aflyun/p/6421441.html
1 maven创建后无法部署到tomcat里面
1.
选中项目右键,点击project Facets选中图框中的选项 这样就可以添加了
2 pom.xml出现web.xml is missing and <failOnMissingWebXml> is set to true解决方案
提示信息应该能看懂。也就是缺少了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,当时不知道Deloyment Descriptor在哪,找了半天才发现,截图下次好找
操作后会在项目的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应用正常运行。
经过这两步,问题解决。
3 Maven项目build时出现No compiler is provided in this environment的处理
错误代码节选:
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.436 s
[INFO] Finished at: 2017-06-28T11:16:07+08:00
[INFO] Final Memory: 10M/151M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project manage: Compilation failure
[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
[ERROR] -> [Help 1]
从上图中可以看出, java编译环境未jre1.7.0_17, 也就是说并没有配置成jdk目录, 然后看Eclipse-->Window-->preferences-->Java-->Installed JREs
为了演示出效果, 在测试之前, 我已经将系统java环境配置成如上图所示路径, 并只保留该配置, 由下图可以看出, 该路径是我所安装的两个JDK版本中的一个JDK自带的jre运行环境. 使用该环境编译普通项目没有问题, 但为什么会在编译Maven项目时出错呢?
我们看看Maven的环境是如何配置的:先找到Eclipse-->Window-->preferences-->Maven-->Installations
在Maven配置中, 我并没有使用Eclipse自带的Maven插件, 而是重新配置的Maven环境, 然后再看Eclipse-->Window-->preferences-->Maven-->User Settings
Maven设置使用的是Maven中conf文件夹下的settings.xml, 点击"open file" 在Eclipse中查看具体配置信息, 仅摘录与本错误信息相关的部分
-
<profiles>
-
<!-- profile
-
| Specifies a set of introductions to the build process, to be activated using one or more of the
-
| mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/>
-
| or the command line, profiles have to have an ID that is unique.
-
|
-
| An encouraged best practice for profile identification is to use a consistent naming convention
-
| for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.
-
| This will make it more intuitive to understand what the set of introduced profiles is attempting
-
| to accomplish, particularly when you only have a list of profile id's for debug.
-
|
-
| This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.-->
-
<profile>
-
<id>jdk-1.7</id>
-
<activation>
-
<activeByDefault>true</activeByDefault>
-
<jdk>1.7</jdk>
-
</activation>
-
<properties>
-
<maven.compiler.source>1.7</maven.compiler.source>
-
<maven.compiler.target>1.7</maven.compiler.target>
-
<maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion>
-
</properties>
-
</profile>
-
</profiles>
中间具体信息的理解, 可以参见 冰河winner 的博客. 也就是说, 根据上面的配置, 我们需要指定一个符合配置的JDK环境, 这是我们之前在Eclipse-->Window-->preferences-->Java-->Installed JREs下的配置就不行了, 而需要指定一个JDK目录, 例如我的JDK安装目录下的jdk1.7.0_17, 这也是这个错误出现的罪魁祸首. 不过对于Java开发者来说, Installed JREs中使用jdk目录而不适用jre目录也是最好的选择.
步骤:
然后再编译运行项目即可.