millimoose
millimoose

Reputation: 39990

Switch between web.xml files in a legacy Java webapp

I'm in the process of inheriting a legacy codebase where nobody really ever introduced a sane way of handling different environments beyond "copy files around into your source tree." I'm trying to reintroduce some sanity without breaking everything.

The web.xml of the front-end webapp (using Spring MVC 3.x on Tomcat 6.x) contains a bunch of servlets that proxy to a different URL:

<servlet>
    <servlet-name>FooProxy</servlet-name>
    <servlet-class>org.mortbay.servlet.ProxyServlet$Transparent</servlet-class>
    <init-param>
        <param-name>ProxyTo</param-name>
        <param-value>http://example.com/foo/</param-value>
    </init-param>
</servlet>

For local development, this has to be replaced with a servlet that proxies to a dev server of some sort:

<servlet>
    <servlet-name>FooProxy</servlet-name>
    <servlet-class>org.mortbay.servlet.ProxyServlet$Transparent</servlet-class>
    <init-param>
        <param-name>ProxyTo</param-name>
        <param-value>http://localhost:8080/foo/</param-value>
    </init-param>
</servlet>

What's the best way to achieve this, so that:


My current thinking was to put the versions of web.xml into nonstandard source folders, like:

`--envs
   |
   |--prod
   |  |
   |  `--webapp
   |     |
   |     `--web.xml // the original ("production") configuration
   |
   `--dev
      |
      `--webapp
         |
         `--web.xml // the development configuration

That is: each "environment" folder contains an alternative version of what's usually in src/main/webapp. Then I'd plug in those folders using Maven profiles, with the production one being active by default.

What I don't know is how one even configures the location of that folder, because the Maven documentation is terrible sometimes. All I know is that src/main/webapp is the convention which is defined somewhere. I also don't know if the tomcat6 plugin will pick up on there being a different webapp folder, and if it's possible to configure more of them and whether that will work correctly. (Ideally I would like to keep the many files under webapp that remain the same between environments in the default folder, and just have it merged with the environment folder.)

Upvotes: 1

Views: 187

Answers (1)

Yogi
Yogi

Reputation: 1895

Better way to achieve your requirements is to use maven profile.

You can have profile for dev and production , which will define this ProxyURL.

You can use profile depending upon environments

For further readings:

  1. http://maven.apache.org/guides/introduction/introduction-to-profiles.html
  2. https://maven.apache.org/guides/mini/guide-building-for-different-environments.html

Upvotes: 1

Related Questions