"webxml attribute is required" error in Maven - maven

I am getting the following error:
Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode)
I have got web.xml in right place which is projectname\src\main\webapp\WEB-INF\web.xml
What could be causing this?

It would be helpful if you can provide a code snippet of your maven-war-plugin.
Looks like the web.xml is at right place, still you can try and give the location explicitly
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webXml>src\main\webapp\WEB-INF\web.xml</webXml>
</configuration>
</plugin>

<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
This solution works for me (I was using 2.2 before). Also, I am using Java Based Configuration for Servlet 3.0 and no need to have web.xml file.

It works perfectly for me too.
<project>
.....
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webXml>WebContent\WEB-INF\web.xml</webXml>
</configuration>
</plugin>
</plugins>
</build>
</project>

This is because you have not included web.xml in your web project and trying to build war using maven. To resolve this error, you need to set the failOnMissingWebXml to false in pom.xml file.
For example:
<properties>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
Please see the blog for more details: https://ankurjain26.blogspot.in/2017/05/error-assembling-war-webxml-attribute.html

If you are migrating from XML-based to Java-based configuration and you have removed the need for web.xml by implementing WebApplicationInitializer, simply remove the requirement for the web.xml file to be present.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
...
</configuration>

The value of my webXml tag needed to look like this in order to work:
<webXml>${project.basedir}\src\main\webapp\WEB-INF\web.xml</webXml>

I had the exact same problem and i solved it like this :
Make a new folder named WEB-INF under src/main/webbapp then
Right Click on your Project -> Java EE Tools -> Generate Deployment Descriptor Stub
This should generate your web.xml
I hope this helps by solving your problem :D

It does look like you have web.xml in the right location, but even so, this error is often caused by the directory structure not matching what Maven expects to see. For example, if you start out with an Eclipse webapp that you are trying to build with Maven.
If that is the issue, a quick fix is to create a
src/main/java and a
src/main/webapp directory (and other directories if you need them) and just move your files.
Here is an overview of the maven directory layout:
http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html

🔴 failOnMissingWebXml since 2020
All other answers about might be obsolete because the default value used by the maven-war-plugin changed:
Starting with 3.0.1, this property defaults to false if the project depends on the Servlet 3.0 API or newer.
So the ONLY thing you have to do is to add
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
Example:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>foo</groupId>
<artifactId>bar</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

As per the documentation, it says : Whether or not to fail the build if the web.xml file is missing. Set to false if you want you WAR built without a web.xml file. This may be useful if you are building an overlay that has no web.xml file.
Default value is: true.
User property is: failOnMissingWebXml.
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<extensions>false</extensions>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
Hope it makes more clear

It worked for me too.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webXml>WebContent\WEB-INF\web.xml</webXml>
</configuration>
</plugin>

This is an old question, and there are many answers, most of which will be more or less helpful; however, there is one, very important and still relevant point, which none of the answers touch (providing, instead, different hacks to make build possible), and which, I think, in no way has a less importance.. on the contrary.
According to your log message, you are using Maven, which is a Project Management tool, firmly following the conventions, over configuration principle.
When Maven builds the project:
it expects your project to have a particular directory structure, so that it knows where to expect what. This is called a Maven's Standard Directory Layout;
during the build, it creates also proper directory structure and places files into corresponding locations/directories, and this, in compliance with the Sun Microsystems Directory Structure Standard for Java EE [web] applications.
You may incorporate many things, including maven plugins, changing/reconfiguring project root directory, etc., but better and easier is to follow the default conventions over configuration, according to which, (now is the answer to your problem) there is one simple step that can make your project work: Just place your web.xml under src\main\webapp\WEB-INF\ and try to build the project with mvn package.

If you change the default project path, you must specify the location of the web.xml file, for example:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.5</version>
<configuration>
<webXml>src\main\web\WEB-INF\web.xml</webXml>
</configuration>
</plugin>

mvn-war-plugin 2.3 fixes this:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
</plugin>
...

I have had the same error on the test server but not in local. After a few minutes, I discovered that the IDE wasn't synchronized with the pom.xml. Here is how I solve it:
Re-Generate the deployment descriptor with Eclipse
Right click on your project folder
In the contextual menu, choose "Java EE Tools" then "Generate Deployment Descriptor Stub"
It will create the web.xml.
Re-Generate the deployment descriptor with IntelliJ
Right click on your project folder
In the contextual menu, choose "Open Module Settings", then click on the + to add the web deployment descriptor.
Then your can change the path to your descriptor or the Web Ressource Directoriesand the on the right side.
Then you will get something like:

Make sure pom.xml is placed properly in Project folder. and not inside target folder or any where else.
Looks like pom.xml is not relatively aligned.

Was your folder structure altered so the file is no-longer at /src/main/webapp/WEB-INF/web.xml ?
To resolve this issue, I gave my web folder the name webapp and placed it inside the src/main. Maven seems to look for web.xml by default at /src/main/webapp/WEB-INF/web.xml. If you do this then you don't need to explicitly tell maven where web.xml is. And if you've altered your folder structure and your build recently stopped working, this could be why.
Note: This is an old post but the posted solutions don't point out why a working build would suddenly stop working.

This error occurs because you tell to Maven to pakage files to war.
<packaging>war</packaging>
Do you really need war? If not, put jar there.
Here is full code:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<version>1.0-SNAPSHOT</version>
<groupId>com.your.groupid</groupId>
<artifactId>artifactid</artifactId>
<packaging>jar</packaging>

I encountered this message while trying to package a Spring Boot project as a WAR file and deploying that WAR file in a standalone Tomcat instance.
I used Spring Intializr to generate the initial pom.xml and related artifacts.
The problem was that in that pom.xml (Spring version 2.6.+), this dependency was in place.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
I had to replace it with this one:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
And also added this dependency with scope of "provided"
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
Also, of course, changed the packaging
<packaging>war</packaging>

Make sure to run mvn install before you compile your war.

There are two things to note regarding packaging.
Packaging to a war file will require you define the web.xml file in the appropriate folder as described by Catalin Ciolocoiu.
But changing the package type to jar would not need that structure and should work straight away.

Related

includeSystemScope Parameter in pom.xml not working

I'm tring to include a custom jar in my Spingboot application. In my case the additional jar contains a custom font for jasper Report.
This is my "system" decendency
<dependency>
<groupId>jasperFontOverrides</groupId>
<artifactId>jasperFontOverrides</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${basedir}/lib/MyriadPro.jar</systemPath>
</dependency>
...
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
</build>
The system package is visible while I debug my application in my IDE but when I'm done and I what to generate the package for production deploy
mvn install -DskipTests
My system package is not included the final jar.
Is there anything missing in my maven configuration?
If this is a multi-module project, you probably need to define the configuration section in the parent pom.
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
I have spent almost a day debugging this and it seems the configuration set in the child pom is not enough to get this to work. My guess is that the dependencies are calculated early in the build and the includeSystemScope option set in the child module is processed too late to be applied.

How to create a complete `.war` file with pom.xml

I have a web service project (SOAP) implemented in Java, as a Maven project. I want to create a .war to deploy into a Tomcat.
To create the .war, I always used the Eclipse Juno interface (Right click -> Export -> WAR File). The Eclipse is configured with Tomcat 7 and Axis 1.5.6. Using this method I deploy the .war file created and everything is fine! The main page of the application looks like below:
[![Web Service main page][1]][1]
In this page, I can explore the services, methods and WSDL files. This is what I call complete .war file.
Now, I migrated my web service to Maven, in order to use it with Jenkins, to automate the deploys, tests, etc.
I tried for two full days create a pom.xml which will create the same .war file that was created with Eclipse, but I was not successful. When I deploy the .war created using the pom.xml on a Tomcat and access the localhost:8080/SOAPExample/, it shows Error 404. The Maven build of the .war and the Tomcat deploy was successful. I understood that the service page was not created, but the web service was deployed successfuly.
I used some of the tutorials available in the internet, like the below, but in all cases the .war created by Maven is incomplete:
http://crunchify.com/how-to-create-a-war-file-from-eclipse-using-maven-plugin-apache-maven-war-plugin-usage/
https://www.mkyong.com/maven/how-to-deploy-maven-based-war-file-to-tomcat/
It looks like I'm missing some dependency configuration into the pom.xml, or something like that. Here is my pom.xml. I also think it is missing some axis2 configuration.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>SOAPExample</artifactId>
<version>0.0.1</version>
<packaging>war</packaging>
<name>SOAPExample</name>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
Here is my project sctructure:
[[![Project Folder Structure]][2][2]]
There is no tutorial in the internet explaining how to configure a pom.xml to create a complete .war file. Could some good soul help me?

Why would a maven-war-plugin generate a JAR instead of a WAR?

I am following this Contract first using CXF tutorial and while the resulting pom.xml generates sources and even completes build successfully, it fails to create a WAR file.
Instead, it creates a JAR file.
My understanding is that the part in the pom.xml that's responsible for creating the WAR is:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<outputDirectory>D:/path/to/profile/autodeploy</outputDirectory>
</configuration>
</plugin>
I don't see any <goal> or <execution> element there (unlike in the build-helper-maven-plugin one), but I also understand that with this plugin this is implied as even the official usage page demonstrates:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<webappDirectory>/sample/servlet/container/deploy/directory</webappDirectory>
</configuration>
</plugin>
</plugins>
</build>
...
</project>
So... what am I missing?
What could possibly explain a maven-war-plugin that behaves in unexpected way like this and produces a JAR instead of a WAR by default?
Is there a way to force it to produce a WAR?
packaging should be as below.
<packaging>war</packaging>
if it won't help then try binding your plug-in configuration with a lifecycle phase.
in your project definition , please check if packaging is missing or not , it should be some thing like this .
<groupId>some.groupid</groupId>
<artifactId>My Web Application</artifactId>
<version>0.0.1</version>
<packaging>war</packaging>
<description>My First Web Application</description>
By default maven war plugin binds to package phase of the lifecycle ,so its important that we should mention the type of packaging we want once the build is done.
I would like to suggest to have a look at the Maven specs for war plugin.

creating war file using maven-ear-plugin and defining it in one pom.xml

I am at the starter level of the maven usage. I hope I can explain my problem clearly, I want to create an ear file which contains war file inside it. And I planned to use to create a war file from the start. Also I want to do it in my pom.xml at my project and there is only one pom.xml, here is the problem;
Can I create ear file and which contains this war that I created at the same time in one pom.xml file?
when I try to create war file in webmodule tag, here is the problem that I encounter " Artifact[war:denem.denem:denem] is not a dependency of the project." I understood so that's why I added dependency for this file in the same pom.xml but this time I encountered that problem
(By the way my command to build this pom is "mvn clean package" )
"1 required artifact is missing.
for artifact:
com.denem.denem:com.denem.de2:ear:v0.1"
It tries to find this war file but I want to create it not to find it. Here the code in my pom.xml file;
<parent>
<groupId>denem.denem</groupId>
<artifactId>com.denem.denem</artifactId>
<version>v0.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>denem.denem</groupId>
<artifactId>com.denem.de2</artifactId>
<version>v0.1</version>
<packaging>ear</packaging>
<properties>
<cxf.version>2.2.5</cxf.version>
</properties>
<dependencies>
<dependency>
<groupId>denem.denem</groupId>
<artifactId>denem</artifactId>
<version>v0.1</version>
<type>war</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<version>2.5</version>
<configuration>
<finalName>edu</finalName>
<defaultLibBundleDir>lib</defaultLibBundleDir>
<modules>
<webModule>
<groupId>denem.denem</groupId>
<artifactId>denem</artifactId>
<contextRoot>/WebContent</contextRoot>
</webModule>
</modules>
</configuration>
</plugin>
</plugins>
</build>
I guess I am doing lots of things wrong. But If you can help me I will be glad. Thank you anyway.
You need to create a modular project.
Create:
a parent project of type "pom";
a child project of type "war";
if needed, child projects of type "ejb";
if needed, child projects of type "jar" (common libraries);
one project of type "ear", that has all of the above as dependencies.
In the latter you need to configure the ear plugin putting all the modules that you need.

sonar findbug missing class exception

while I am using sonar findbug for code review, it throws exception, claiming some classes are missing.
After some research, it's clear that the jars needed to build the project, should be included in the pom.xml for sonar's use.
to do that, sonar official web site suggesting, add dependencies,
com.opensymphony
xwork-core
2.1.6
system
${basedir}/.../xwork-core.jar
however, for me, it's always not working.
I am using windows, would anyone please enlighten me, how to configure, especially the systempath?
Here is the pom.xml, for one module of the project.
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>DB</groupId>
<artifactId>Project1</artifactId>
<name>project1</name>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>com.opensymphony</groupId>
<artifactId>xwork-core</artifactId>
<version>2.1.6</version><!--installed into .m2 folder, local repository, then refer it-->
</dependency>
<dependency>
<groupId>dep</groupId>
<artifactId>dep1</artifactId>
<version>1.0</version> <scope>system</scope>
<systemPath>${basedir}/lib//asn.jar</systemPath>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<outputDirectory>build-to/classes</outputDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
<excludes>
<exclude>**/*.*</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<sonar.dynamicAnalysis>false</sonar.dynamicAnalysis>
</properties>
</project>
Thanks,
Jackie
Is your project already a maven project, or are you compiling things with ant or some other tool and then trying to get sonar to run against it? Sonar Light mode can be tricky with very complex projects. If you've got a multi-module project, i'd get the simplest project working under a local copy of sonar and then work towards adding more modules.
Additionally, I would try copying the files locally to a different directory that does not step up from ${basedir}. See if that helps. So ${basedir}/lib/xwork-core.jar something like that.
Perhaps for Sonar analysis, you can have a separate task copy the needed libraries to a temp folder that can be accessed appropriately, and then remove them once the analysis is complete.
i found out the problem. The official documentation of sonar is actually wrong.
"mvn sonar:sonar" would invoke the maven sonar plugin 2.0 beta, which won't enable the dependency of library jars work. Instead, call "mvn sonar3:sonar", invoke the maven sonar plugin 2.4.1, the latest version, works.

Resources