Eclipse Error: web.xml is missing and failOnMissingWebXml is set to true – Eclipse错误:web.xml丢失,failOnMissingWebXml被设置为true

最后修改: 2019年 3月 10日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.绪论

In this tutorial, we’ll discuss the common Eclipse error, “web.xml is missing and <failOnMissingWebXml> is set to true“, that we get while creating a web application.

在本教程中,我们将讨论常见的Eclipse错误,”web.xml丢失,并且<failOnMissingWebXml>被设置为true“,这是我们在创建Web应用程序时遇到的问题。

2. Eclipse Error

2.Eclipse错误

In Java web applications, web.xml is the standard name of the deployment descriptor.

在Java网络应用中,web.xml是部署描述符的标准名称。

We can create a web application using Maven or a dynamic web project using Eclipse. Eclipse doesn’t create the default deployment descriptor web.xml under the WEB-INF/ directory.

我们可以用Maven创建一个Web应用,也可以用Eclipse创建一个动态Web项目。Eclipse并没有在WEB-INF/目录下创建默认的部署描述符web.xml

Java EE 6+ specifications have attempted to de-emphasize deployment descriptors, as they can be replaced by annotations. However, the lower versions still require it.

Java EE 6+规范已经试图不再强调部署描述符,因为它们可以被注释所取代。然而,低版本仍然需要它。

The failOnMissingWebXml property is one of the properties of the Apache Maven war plugin, org.apache.maven.plugins:maven-war-plugin. The default value of this plugin is true for version < 3.1.0 and false for the later versions.

failOnMissingWebXml属性是Apache Maven战争插件的属性之一,org.apache.maven.plugins:maven-war-plugin。该插件的默认值是true,对于<3.1.0>版本,false

This means that if we’re using maven-war-plugin earlier than version 3.1.0, and the web.xml file is not present, then the goal to package it as a war file will fail.

这意味着,如果我们使用的maven-war-plugin早于3.1.0版,且web.xml文件不存在,那么将其打包为war文件的目标就会失败。

3. Using web.xml

3.使用web.xml

For all the cases where we still need the web.xml deployment descriptor, we can easily generate web.xml in Eclipse:

对于所有我们仍然需要web.xml部署描述符的情况,我们可以在Eclipse中轻松生成web.xml

  • Right click on the web project
  • Hover to Java EE Tools on the menu
  • Select Generate Deployment Descriptor Stub from the sub-menu

 

generatestub

Voila! the web.xml file is generated under the WEB-INF/ directory.

瞧!web.xml文件在WEB-INF/目录下生成。

4. Without web.xml

4.没有web.xml

In most cases, we may not require the web.xml file at all. Instead of keeping a blank web.xml file in our project, we can simply skip creating it altogether. Luckily, there are two simple approaches, depending on which version of the maven-war-plugin we’re using.

在大多数情况下,我们可能根本不需要web.xml文件。与其在项目中保留一个空白的web.xml文件,我们不如直接跳过创建它。幸运的是,有两种简单的方法,取决于我们使用的maven-war-plugin的版本。

4.1. Using maven-war-plugin Before 3.1.0

4.1.使用3.1.0之前的maven-war-plugin

We can configure all the plugins of a Maven project in the <plugins> section of our pom.xml. As we’ve said previously, the default value for failOnMissingWebXml is true before version 3.1.0 of the plugin.

我们可以在pom.xml<plugins>部分配置一个Maven项目的所有插件。正如我们之前所说,在3.1.0版本的插件之前,failOnMissingWebXml的默认值是true

Let’s declare the maven-war-plugin in our pom.xml and explicitly set the property failOnMissingWebXml to false:

让我们在pom.xml中声明maven-war-plugin,并明确将属性failOnMissingWebXml设为false

<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <failOnMissingWebXml>false</failOnMissingWebXml>    
    </configuration>
</plugin>

4.2. Using maven-war-plugin 3.1.0 and Later

4.2.使用maven-war-plugin 3.1.0及以后版本

We can also avoid setting the property explicitly by upgrading the version of maven-war-plugin. The default value of the property failOnMissingWebXml is false for maven-war-plugin version 3.1.0 and later:

我们还可以通过升级maven-war-plugin的版本来避免明确设置该属性。failOnMissingWebXml属性的默认值是false,适用于maven-war-plugin 3.1.0及以后的版本。

<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>3.1.0</version>
</plugin>

5. Conclusion

5.总结

In this article, we saw the reason behind the missing web.xml error and multiple approaches to fix it.

在这篇文章中,我们看到了丢失web.xml错误背后的原因,以及修复它的多种方法。

As usual, our example can be found over on GitHub.

像往常一样,我们的例子可以在GitHub上找到over