首页 文章

构建WAR包时出现Maven错误(web.xml丢失..?)

提问于
浏览
9

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

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

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

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

我的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>

如何正确解决这个问题?

问候

5 回答

  • 0

    strongly 建议使用Maven's standard layout

    • 将Java源代码放在 src/main/java 中(并删除 sourceDirectory 元素)

    • 将Web应用程序源放在 src/main/webapp

    • 删除 WEB-INF 下的 classeslib 目录

    当然,你可以自定义布局,但这是IMO更多的麻烦和无用的努力而不是好处 . 只需按照惯例 .

  • 1

    我的猜测是 maven-war-plugin 正在寻找 src/main/webapp/WEB-INF/web.xml ,但可以't find it, and wants you to specify the (non-maven-standard) location explicitly. If you can' t将 WebContent 重命名为 webapp 并将其移至 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>
    
  • 0

    看一下这个评论:

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

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

  • 13

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

    例:

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

    会工作得很好 . 但,

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

    将不会

  • 19

    如果您正在使用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>
    

相关问题