Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven: Customize web.xml of web-app project

I have a web application Maven project, and I want to customize the web.xml file depending on the Profile that is running. I am using the Maven-War-plugin, which allows me to define a "resources" directory, where the files may be filtered. However, filtering alone is not sufficient for me.

In more detail, I want to include (or exclude) the whole section on security, depending on the profile I an running. This is the part:

.... ....  <security-constraint>      <web-resource-collection>         <web-resource-name>protected</web-resource-name>         <url-pattern>/pages/*.xhtml</url-pattern>         <url-pattern>/pages/*.jsp</url-pattern>     </web-resource-collection>      <auth-constraint>         <role-name>*</role-name>     </auth-constraint>      </security-constraint>         <login-config>         <auth-method>${web.modules.auth.type}</auth-method>         <realm-name>MyRealm</realm-name>     </login-config>  <security-constraint>  .... .... 

If this is not done easily, is there a way to have two web.xml files and select the appropriate one depending on the profile?

like image 476
Markos Fragkakis Avatar asked Jul 21 '10 11:07

Markos Fragkakis


People also ask

How do I add Web XML to POM XML?

I know this question is three years now, but just in case anybody is still wondering how to do it : In eclipse , right click on your project's name then select Java EE Tools-> Generate Deployment Descriptor Stub. And voila, the web. xml file is automatically created.

Where does Web XML go in Maven project?

xml file in the current Maven project, relative to the location of pom. xml . The default is src/main/webapp/WEB-INF/web.

Is Web XML same as POM XML?

6 Answers. Show activity on this post. They're completely compatible. As a matter of fact, they perform completely unrelated tasks.


2 Answers

is there a way to have two web.xml files and select the appropriate one depending on the profile?

Yes, within each profile you can add a configuration of the maven-war-plugin and configure each to point at a different web.xml.

<profiles>     <profile>         <id>profile1</id>         <build>             <plugins>                 <plugin>                     <groupId>org.apache.maven.plugins</groupId>                     <artifactId>maven-war-plugin</artifactId>                     <configuration>                         <webXml>/path/to/webXml1</webXml>                     </configuration>                 </plugin>                  ... 

As an alternative to having to specify the maven-war-plugin configuration in each profile, you can supply a default configuration in the main section of the POM and then just override it for specific profiles.

Or to be even simpler, in the main <build><plugins> of your POM, use a property to refer to the webXml attribute and then just change it's value in different profiles

<properties>     <webXmlPath>path/to/default/webXml</webXmlPath> </properties> <profiles>     <profile>         <id>profile1</id>         <properties>             <webXmlPath>path/to/custom/webXml</webXmlPath>         </properties>     </profile> </profiles> <build>     <plugins>         <plugin>             <groupId>org.apache.maven.plugins</groupId>             <artifactId>maven-war-plugin</artifactId>             <configuration>                 <webXml>${webXmlPath}</webXml>             </configuration>         </plugin>         ... 
like image 164
matt b Avatar answered Oct 13 '22 23:10

matt b


There's a third, compromise option which I implemented in my project. It keeps everything in one web.xml while still making both it and the pom.xml readable. In my case, I had a need to sometimes have security and sometimes have no security, depending on the environment.

So what I did was:

In the pom.xml, define two profiles (or however many you need). Within the profiles, include two properties. When you want security, you leave them empty, like this:

<enable.security.start></enable.security.start> <enable.security.end></enable.security.end> 

When you want to exclude all of the security, you define them as follows:

<enable.security.start>&lt;!--</enable.security.start> <enable.security.end>--&gt;</enable.security.end> 

Then, you have a single web.xml file with the following:

${enable.security.start} <security-constraint>   ...   // all of the XML that you need, in a completely readable format   ... </login-config>   ${enable.security.end} 

The pom.xml maven-war-plugin has to be configured to use filtering. Mine looks like this:

   <configuration>       <webResources>         <resource>           <filtering>true</filtering>           <directory>src/main/webapp</directory>           <includes>             <include>**/web.xml</include>           </includes>         </resource>       </webResources>       <warSourceDirectory>src/main/webapp</warSourceDirectory>       <webXml>src/main/webapp/WEB-INF/web.xml</webXml>       ... 

So, basically, when you select the profile to include security, you get two extra CRLF's in your web.xml. When you select the profile to NOT include security, the XML is all still in the web.xml, but it's commented out so it gets ignored. I like this because you don't have to worry about keeping multiple files in sync, yet the XML is still readable (and it's in the web.xml file where people would naturally look for it).

like image 28
Chris Clark Avatar answered Oct 13 '22 23:10

Chris Clark