All,

I wanted to give something back to the community. For all of you, who would like to know how to structure a web application in a way that it can be build for different environments, here is the solution:

I combined several previous posts and my ideas, this application uses different resources for different environments (profiles) as well as diffenent filters.

I filter the context.xml for Tomcat and web.xml, but use totally different log4j.xml's for the different environments. In addition I have resources that go unfiltered to all environments (Hibernate configuration files).

Create a directory layout similar to this:

app
|
+- src
    |
    +- main
        |
        +- filter
        |   |
        |   +- dev (filter properties for your local development)
        |   |
        |   +- prod (filter props for production)
        |
        +- java
        |
        +- resources
        |   |
        |   +- all (resources for all environments)
        |   |
        |   +- dev
        |   |
        |   +- prod
        |
        +- webapp
        |
        +- webapp-filtered
            |
            +- META-INF (e.g. context.xml)
            |
            +- WEB-INF (e.g. web.xml)

Your pom should look something like this then:

...

  <build>
    <finalName>app</finalName>
    <resources>
       <!-- Resources for all builds. -->
       <resource>
         <directory>src/main/resources/all</directory>
       </resource>
    </resources>
   </build>

  <profiles>
    <!-- Local development environment. -->
    <profile>
      <id>dev</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <build>
        <resources>
          <resource>
            <directory>src/main/resources/dev</directory>
          </resource>
          <resource>
            <directory>src/main/webapp-filtered</directory>
            <targetPath>../app</targetPath>     <!-- has to be finalName! -->
            <filtering>true</filtering>
          </resource>
        </resources>
        <filters>
          <filter>src/main/filter/dev/context.properties</filter>
          <filter>src/main/filter/dev/web.properties</filter>
        </filters>
      </build>
    </profile>

    <!-- Production. -->
    <profile>
      <id>prod</id>
      <build>
        <resources>
          <resource>
            <directory>src/main/resources/prod</directory>
          </resource>
          <resource>
            <directory>src/main/webapp-filtered</directory>
            <targetPath>../app</targetPath>     <!-- has to be finalName! -->
            <filtering>true</filtering>
          </resource>
        </resources>
        <filters>
          <filter>src/main/filter/prod/context.properties</filter>
          <filter>src/main/filter/prod/web.properties</filter>
        </filters>
      </build>
    </profile>

  </profiles>

...

Would be great if we could use ${pom.build.finalName}! Maybe this is an idea for the Maven developers.

Using this you can build your dev app using:
mvn package (since it is the default) or mvn -Pdev package

and your production application using:
mvn -Pprod package

This can be extended. Just add another directory and profile and you're done!

I found this to be a good way of attacking the problem. Feedback is welcome!


Janek



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to