peterh
peterh

Reputation: 19225

Using web.xml in Vaadin project

I have a Maven-based Vaadin project. It is based off the Maven vaadin-archetype-application-multimodule archetype (link).

This means I have a structure something like this:

parent
   myapp-ui
   myapp-production
   myapp-widgetset

In my project I had to customize web.xml as what I needed could not be done programmatically or with annotations. Don't like it but it's the way it is.

The problem I have is that it seems I have to maintain web.xml in two places: both in myapp-ui module and then secondly also on myapp-production module. Really??

I would like to maintain web.xml in only once place. How to do that?

For those who doesn't know Vaadin: The only difference between the two versions of web.xml is that the one in the production module must have the following added:

<context-param>
  <param-name>productionMode</param-name>
  <param-value>true</param-value>
</context-param>

Upvotes: 2

Views: 1093

Answers (2)

peterh
peterh

Reputation: 19225

This turned out to be a Maven question much more than a Vaadin question. Still I'll answer the question in the context of Vaadin.

Recap: I want to maintain only one web.xml. That should be the one in my -ui module as all source code generally reside there and really there should be no source code in the -production module.

Problem: The maven-war-plugin's overlay feature doesn't copy nor merge the web.xml from the overlay into the target.

I've solved it in a couple of steps. First I enhanced my web.xml in the -ui module with the following:

 ...
   <context-param>
      <description>Vaadin turn debugging mode on/off</description>
      <param-name>productionMode</param-name>
      <param-value>${vaadin.productionmode}</param-value>
   </context-param>
 ...

Then in the pom.xml for the -ui module I added:

<properties>
     ...
    <vaadin.productionmode>false</vaadin.productionmode>
</properties>

and in the pom.xml for the -production module I added:

 <profiles>
    <profile>
        <id>production</id>
        <properties>
            <vaadin.productionmode>true</vaadin.productionmode>
        </properties>
        ...

Now we have a Maven property, vaadin.productionmode, which by default has a value of false, except if the production profile is active in which case the value is true. We've also prepared our web.xml for Maven filtering. But we also need to activate the filtering (-ui module's POM):

<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <configuration>
       ...
       <!-- Do filtering on web.xml -->    
       <filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>
    </configuration>

(The Maven WAR plugin doesn't do filtering on web.xml file unless you set this config parameter)

You also need to activate web.xml filtering on the -production module's POM:

 <profiles>
    <profile>
        <id>production</id>
        ...
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    ...
                    <configuration>
                        <!-- Do filtering on web.xml -->    
                        <filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>

                        <!-- Use web.xml from UI module -->
                        <webXml>../myapp-ui/src/main/webapp/WEB-INF/web.xml</webXml>

                        <overlays>
                           ...

And finally as you can see in the XML above I've told the -production module that it should use the web.xml from the -ui project. And that's the real trick! You can now remove the web.xml file from the -production module as it's no longer used.

What has been accomplished:

  1. The web.xml's content has become variable using the Maven filtering feature. The variable vaadin.productionmode gets set differently when production profile is active in Maven.
  2. The -production module now no longer has its own web.xml. Instead it copies the file from the -ui module (doing proper filtering after copying).

Upvotes: 2

Jukka Nikki
Jukka Nikki

Reputation: 366

productionMode = false is simply a way to turn debug features off. There's no need to have two web.xml's -- and if you use annotations you can get rid of web.xml completely.

It's possible to use @VaadinServletConfiguration annotation on you're application code, in which case it's not up to web.xml to turn debug mode off.

Please see up to date official docs here: https://vaadin.com/docs/-/part/framework/advanced/advanced-debug.html

Here's example code which shows how to annotate vaadin servlet with @WebServlet and @VaadinServletConfiguration annotations.

https://github.com/vaadin/serverside-elements/blob/master/elements-demo/src/main/java/org/vaadin/elements/demo/Demo.java

It's up to you if you try to get rid of web.xml, but there's never really need for more than one per web application, and if you use recent servlet container there shouldn't be too many things you can't do without web.xml.

Upvotes: 1

Related Questions