Java 构建 WAR 包时出现 Maven 错误(缺少 web.xml ..?)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3136177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:45:26  来源:igfitidea点击:

Maven error while building WAR package (web.xml missing..?)

javamaven-2

提问by jwaliszko

While executing mvn install, I'm getting following error:

执行时mvn install,我收到以下错误:

Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode)

组装 WAR 时出错:需要 webxml 属性(如果在更新模式下执行,则需要预先存在的 WEB-INF/web.xml)

My web application structure tree is like that:

我的 Web 应用程序结构树是这样的:

my-app
|-- pom.xml
|-- src
    |-- ...
    |-- WebContent
        |-- ...
        |-- META-INF
        |-- WEB-INF
            |-- classes
                |-- ...
            |-- lib
            |-- **web.xml**

My POM file looks like that:

我的 POM 文件如下所示:

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>masters.traffic</groupId>
  <artifactId>traffic_web</artifactId>
  <packaging>war</packaging>
  <name>traffic_web</name>
  <version>0.1.0</version>
  <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>                    
                </configuration>
            </plugin>
        </plugins>
  </build>

    ...
</project>

How to properly fix that issue ?

如何正确解决该问题?

Regards

问候

采纳答案by Pascal Thivent

I stronglyrecommend to use Maven's standard layout:

强烈建议使用Maven 的标准布局

  • put the Java sources in src/main/java(and remove the sourceDirectoryelement)
  • put the Web application sources in src/main/webapp
  • remove the classesand libdirectories under WEB-INF
  • 放入 Java 源代码src/main/java(并删除sourceDirectory元素)
  • 将 Web 应用程序源放入 src/main/webapp
  • 删除下的classeslib目录WEB-INF

Sure, you can customize the layout but this is IMO more troubles and useless efforts than benefits. Just follow the conventions.

当然,您可以自定义布局,但这对 IMO 来说是麻烦和无用的努力,而不是好处。只需遵守约定即可。

回答by Ben J

Take a look at this comment:

看看这个评论:

Maven: Including a META-INF folder in the classes folder

Maven:在类文件夹中包含一个 META-INF 文件夹

I believe Maven expects a folder called "webapp", not "WebContent".

我相信 Maven 需要一个名为“webapp”的文件夹,而不是“WebContent”。

回答by Lauri Lehtinen

My guess is that maven-war-pluginis looking for src/main/webapp/WEB-INF/web.xml, but can't find it, and wants you to specify the (non-maven-standard) location explicitly. If you can't rename WebContentto webappand move it under src/main/(recommended), you could try adding something like this to your <build>

我的猜测是maven-war-plugin正在寻找src/main/webapp/WEB-INF/web.xml,但找不到它,并希望您明确指定(非 Maven 标准)位置。如果您无法重命名并将WebContentwebapp移动到src/main/(推荐)下,您可以尝试将这样的内容添加到您的<build>

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-war-plugin</artifactId>
  <configuration>
    <webXml>${basedir}/src/WebContent/WEB-INF/web.xml</webXml>
    <warSourceDirectory>${basedir}/src/WebContent</warSourceDirectory>
  </configuration>
</plugin>

回答by Ben Siver

I ran into this issue recently and the problem was that my resource class was not specifying an initial path at the top of the class. Each method specifed a path, but there was nothing specifying the initial path.

我最近遇到了这个问题,问题是我的资源类没有在类的顶部指定初始路径。每个方法都指定了一个路径,但没有指定初始路径。

Example:

例子:

@Path("/")
public class MyResource {

@GET
@Produces("text/html")
public String foo() {
    return "foo";
}

@GET
@Path("pt")
@Produces("text/html")
public String bar() {
    return "bar";
}

Will work just fine. But,

会工作得很好。但,

public class MyResource {

@GET
@Produces("text/html")
public String foo() {
    return "foo";
}

@GET
@Path("pt")
@Produces("text/html")
public String bar() {
    return "bar";
}

will not

将不会

回答by Ben Siver

If you're using eclispe, make sure to right click the project and create a Maven "project." You can compile as a batch from command line with updates using mvn generate:archetype -B -cpu but you might have to manually structure a few things. With this it will run as a web application or a client application. Add this code to pom.xml.

如果您使用的是 eclispe,请确保右键单击该项目并创建一个 Maven“项目”。您可以使用 mvn generate:archetype -B -cpu 从命令行编译为批处理,但您可能需要手动构建一些内容。有了它,它将作为 Web 应用程序或客户端应用程序运行。将此代码添加到 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>