webxml attribute is required with Servlet 3.0

48,730

Solution 1

By default maven-war-plugin will fail if it can't find web.xml, see here. If you are creating Maven project using latest archetype, you will get this parameter set to false:

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <configuration>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
    </plugins>
  </build>

If you want to use web.xml instead of annotations, just create WEB-INF/web.xml and define servlet there, see Book of Vaadin for detailed instruction.

Solution 2

I landed here with a similar error but using Eclise Luna (4.4) with Maven 3.0

I ended up doing this:

  1. move the WebContent\WEB-INF\web.xml to src\main\webapp\WEB-INF\web.xml

  2. Use the <webXml> to specify the location of web.xml

<build>
      <plugins>
          <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-compiler-plugin</artifactId>
              <version>3.0</version>
              <configuration>
                  <webXml>src\main\webapp\WEB-INF\web.xml</webXml>
              </configuration>
          </plugin>
      </plugins>
      <!-- we dont want the version to be part of the generated war file name -->
      <finalName>${project.artifactId}</finalName>
  </build>
  1. Run mvn package and see a BUILD SUCCESS (for WAR)

Before this I tried using the original path of the web.xml file

<webXml>WebContent\WEB-INF\web.xml</webXml>

But maven did not pickit up and is best to follow the maven folder structure

http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html

Solution 3

Since version 3.0.0 the maven-war-plugin works out of the box with no web.xml.

Up to at least Maven version 3.3.9 the packaging war binds to a older version of the maven-war-plugin, so you need to explicitly set the version in your pom.xml:

<project>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.0.0</version>
            </plugin>
        </plugins>
    </build>
</project>

Earlier versions of the maven-war-plugin had the parameter failOnMissingWebXml set to true by default, what causes your problem. See Sergey Makarovs answer on how to manually set the parameter to false if you need to use a older version of the plugin.

Share:
48,730
Roger
Author by

Roger

I'm a Java developer, mostly working on enterprise applications/systems. I have a background in PL1, FormScape and .NET (C#) although that is history now. =)

Updated on July 09, 2022

Comments

  • Roger
    Roger over 1 year

    I get this error when trying to compile a Vaadin WAR:

    Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.1.1:war (default-war) on project testvaadin-web: Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode) -> [Help 1]
    

    I know this error means that maven cannot find my web.xml, but in the "Book of Vaadin" it says that web.xml is not needed when using Servlet API 3.0 and the Annotation @WebServlet in your UI class.

    I am compiling my widgetsets in a separate profile (according to this guide) and it compiles fine when I rnu this profile. However, when I compile only the web-project, I get above mentioned error.

    What gives?

    Do I override the maven behaviour somehow? Vaadin didn't even create a WEB-INF directory. I guess I could create WEB-INF folder and keep a "ghost" web.xml in there to keep maven happy, but that doesn't seem right.

    Am I missing something?

    Does anyone know a good solution to this?

  • Roger
    Roger over 10 years
    I just found it. =) I actually had failOnMissingWebXml in the widget compilation. Thanks
  • Jon
    Jon almost 10 years
    I needed to add the <version>2.4</version> to the plugin to get rid of all my warnings.
  • Roger
    Roger almost 9 years
    I guess this solution may be really nice if you actually do need to use web.xml in some other part of the same project.
  • Dan Passaro
    Dan Passaro almost 9 years
    Note that you can do this a little more succinctly just by adding <failOnMissingWebXml>false</failOnMissingWebXml> to your <properties> section
  • Ajay
    Ajay over 7 years
    just included code snippet in web.xml from the above code snippet as suggested by @Sergey Makarov, it solved the problem. Thanks a lot. Saved 20 minutes there. <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin>