Search code examples
javatomcatjndi

Preventing context XML from being override in Tomcat 6


I am using JNDI to read database configuration from my application's context.xml. The way I currently have this setup is to have [appname].xml in conf/Catalina/localhost. However, when I redeploy the app, this file gets overridden with an empty context file, and I have to copy the custom one back to the conf/Catalina/localhost directory. I have different database settings, etc. for my test and production servers, and so don't want to put the context file in META-INF in the WAR file, but would like to just keep it in the conf/Catalina/localhost directory. Is this possible?

Is there somewhere better to put the database configuration?

I'd also like to avoid putting the configuration in the server.xml file, although I know this is possible.

Thanks!


Solution

  • I would say look into using maven profiles (one for prod, one for test), and having different resource definitions for each profile. You can keep your common files in src/main/resources and then have a folder for each profile type to keep specific config files in:

    • src/test/resources
    • src/prod/resources

    Then you can amend your pom to define each profile and its associated resources:

    <project>
      <profiles>
        <profile>
          <id>prod</id>
          <build>
            <resources>
              <resource>
                <directory>src/main/resources</directory>
                <targetPath>${project.build.outputDirectory}</targetPath>
              </resource>
              <resource>
                <directory>src/prod/resources</directory>
                <targetPath>${project.build.outputDirectory}</targetPath>
              </resource>
            </resources>
          </build>
        </profile>
    
        <profile>
          <id>test</id>
          <build>
            <resources>
              <resource>
                <directory>src/main/resources</directory>
                <targetPath>${project.build.outputDirectory}</targetPath>
              </resource>
              <resource>
                <directory>src/test/resources</directory>
                <targetPath>${project.build.outputDirectory}</targetPath>
              </resource>
            </resources>
          </build>
        </profile>
      </profiles>
    </project>
    

    finally you can build the war using the -Pprod or -Ptest profile argument to mvn

    mvn -Pprod package