2

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>
peterh
  • 18,404
  • 12
  • 87
  • 115

2 Answers2

2

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).
peterh
  • 18,404
  • 12
  • 87
  • 115
  • Maven is poweful, Maven Is tricky. You can further configure you're solution using maven profiles. When you activate specific profile using -P switch you can turn production mode on and off pretty easily. http://maven.apache.org/guides/introduction/introduction-to-profiles.html – Jukka Nikki May 26 '16 at 19:27
1

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.

Jukka Nikki
  • 366
  • 3
  • 7
  • Can't use annotations as annotations doesn't go as far as supporting *any* web.xml feature and certainly not the one I want. Also I want naturally the `productionMode=true` to be there when I build for production. – peterh May 26 '16 at 16:31
  • IMO: Rating answer down when someone is helping you is to me unthoughtful and not very constructive. For web.xml and annotations there's already questions which help you forward http://stackoverflow.com/questions/17236743/how-to-use-annotations-instead-of-web-xml-in-the-servlet-to-specify-url – Jukka Nikki May 26 '16 at 18:12
  • I **didn't** rate you down ! (can't understand why someone has done that) – peterh May 26 '16 at 18:19
  • 1
    "if you use annotations you can get rid of web.xml completely". Not so. There are things that can't be done via annotations. Like the [form based auth](http://stackoverflow.com/a/18157086/1504556) or `` cannot be done via annotations. – peterh May 26 '16 at 18:27
  • I gave you an upvote to counter the person who gave you an unfair downvote. Thanks for your input. Appreciated. – peterh May 26 '16 at 18:35
  • :D Well done. I did assume that it's you, sorry for making quick false assumtions. It's pretty strange that someone gives negative votes without any comment, but surely this person felt having good reason for it. – Jukka Nikki May 26 '16 at 19:21