How to add a web fragment to all applications deployed on WildFly

In this article we will learn how to create a Web fragment XML file (web-fragment.xml) and how to make it default for all applications deployed on WildFly by using a the deployment-overlay feature.

Firsr off, we need some definitions:

Web fragments are xml configuration files that contain parts of a standard web.xml configuration file. There can be or or many web fragments, and when the application is deployed, the application server will combine all the fragments and will treat it like a single web.xml.

Web fragments allow a logical partitioning of the web application in such a way that the frameworks being used within the web application can define all the artifacts without requiring you to edit or add information in the web. xml . For example, in the following project, a Web fragment can host the definition of a Servlet filter:

└── main
    ├── java
    │   └── org
    │       └── jboss
    │           └── web
    │               └── filter
    │                   └──
    └── resources
        └── META-INF
            ├── MANIFEST.MF
            └── web-fragment.xml

Here is the META-INF/web-fragment.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-fragment xmlns:xsi=""
	xmlns="" xmlns:webfragment=""
	id="MyCommonWeb" version="3.0">




Combining Web fragments with Deployment Overlays

In this paragraph I’m going to introduce the Deployment overlay which is a feature available on WildFly / JBoss EAP. A Deployment overlay is a way to include content into an existing deployment without physically modifying the contents of the deployment archive.

By combining Web fragments with Deployment Overlays, you have a very powerful feature: that is, you can define bits of your application configuration in a Web fragment (which is common to all your applications, for example) and then create a deployment overlay which can link the Web fragment to some or all your applications.

Let’s see an example.

Supposing you have a security-constraints section which is common to all your Web applications.

Firstly, create META-INF/web-fragment.xml as follows:

<?xml version="1.0" encoding="UTF-8"?>
<web-fragment xmlns=""

                        <description>application security constraints</description>
                <realm-name>UserRoles simple realm</realm-name>

Secondly, pack the folder META-INF/web-fragment.xml in a JAR file:

$ jar cvf mysecurity.jar META-INF/web-fragment.xml

Finally, from WildFly CLI, create a deployment-overlay specifying the deployment paths you want them to overlay.add the generated web-fragment-lib.jar to all deployed war:

deployment-overlay add --name=web-fragment-jar --content=WEB-INF/lib/mysecurity.jar=/path/to/mysecurity.jar --deployments=*.war --redeploy-affected

By running the above command, all Web applications (*.war) will include in the WEB-INF/lib folder the file mysecurity.jar (adjust the path to the location of your filesystem where the jar file can be found).

Other examples of Web fragments include, for example, to define a standard way to track HTTP Sessions. By including a session-config element in a Web fragment, you can define the tracking mode, which can be for example COOKIE:

<web-fragment xmlns=""

Pack the above META-INF/web-fragment.xml in a JAR file and define a deployment-overlay, to make the session-config by using COOKIE a standard for all your Web applications.

In conclusion, we have covered what Web fragments are and how to use deployment overlays to make them default for all (or some) of your Web applications.