开发者

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

开发者 https://www.devze.com 2023-01-04 20:44 出处:网络
While executing mvn install, I\'m getting following error: Error assembling WAR: webxml attribute is required (or pre-existing

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

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

My web application structure tree is like that:

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

My POM file looks like that:

<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<开发者_如何学Go/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


I strongly recommend to use Maven's standard layout:

  • put the Java sources in src/main/java (and remove the sourceDirectory element)
  • put the Web application sources in src/main/webapp
  • remove the classes and lib directories under WEB-INF

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


My guess is that maven-war-plugin is 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 WebContent to webapp and move it under src/main/ (recommended), you could try adding something like this to your <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>


Take a look at this comment:

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

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


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


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.

<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>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号