Tuesday 4 April 2017

"Error assembling WAR. webxml attribute is required"


Today, when I was trying to generate a WAR file with maven, I got an error saying "Error assembling WAR. webxml attribute is required"


Complete error message:

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

This  has something to do with the failOnMissingWebXml property of the maven WAR plugin.
Servlet 3.0 onwards we need not have a web.xml.
When we specify the packing of the application as war (<packaging>war</packaging>) , maven WAR plugin comes in the picture to generate a WAR file for us even if we have not explicitly configured it. The default value of failOnMissingWebXml is false as specified here. But still maven starts expecting a web.xml since we have mentioned the packaging as war.


Solution: set the property  failOnMissingWebXml to false. There are two ways to achieve this.


1. Set the property  failOnMissingWebXml to false in the property section of your pom file.
    <properties>
        ....
        <failOnMissingWebXml >false</failOnMissingWebXml>
        ....
    </properties>
2. Configure the maven WAR plugin with failOnMissingWebXml set to false.
    <build>
        <plugins>
             ...
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>

        <plugins>
     </build>

Apart from these two solutions there exists another tweak to get rid of this error. You could add an empty web.xml within WEB-INF. But this is not a clean solution. A file should not exist in your project just because a build tool needs it. Having an empty file will always create doubts on the purpose of this file in anyone new to the project or maven world. 

It is also possible that you are getting this error because web.xml is not added within proper directory layout.
Refer this link to learn more about standard directory layout for maven.