Customized context.xml per environment using maven

8.5k views Asked by At

I have two context.xml files in my Java web project:

context.xml.development context.xml.production

and I use maven war plugin to build it.

When I build the project, I'd like maven to copy the proper context.xml to the META-INF directory.

How could I do it? I'm already using profiles in my pom.xml

2

There are 2 answers

2
BenjaminLinus On

A combination of maven build profiles and the maven-war-plugin using file filtering to include or exclude the correct files should do the trick.

For example, something like this:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<groupId>com.mycompany</groupId>
<artifactId>myproject</artifactId>
<profiles>
    <profile>
        <id>custom-profile</id>
        <activation>
            <property>
                <name>environment</name>
                <value>production</value>
            </property>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.1.1</version>
                    <configuration>
                        <webResources>
                            <resource>
                                <directory>resources</directory>
                                <excludes>
                                    <exclude>context.xml.development</exclude>
                                </excludes>
                            </resource>
                        </webResources>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
    <profile>
        <id>custom-profile2</id>
        <activation>
            <property>
                <name>environment</name>
                <value>development</value>
            </property>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.1.1</version>
                    <configuration>
                        <webResources>
                            <resource>
                                <directory>resources</directory>
                                <excludes>
                                    <exclude>context.xml.production</exclude>
                                </excludes>
                            </resource>
                        </webResources>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

Then when you run maven be sure to sent the "environment" property appropriately:

mvn clean install -Denvironment=production

3
Tom Chamberlain On

Another approach (in case you didn't consider it) is to use one context.xml file with place holders. For example:

<Context>
<Resource name="jdbc/syncDB" auth="Container" type="javax.sql.DataSource"
           maxTotal="100" maxIdle="30" maxWaitMillis="10000"
           username="${database.username}" password="${database.password}" driverClassName="oracle.jdbc.OracleDriver"
           url="${database.url}"/>

</Context>

Then, add the war plugin to your maven pom.xml file that has the META-INF folder as a resource that filters:

    <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <webResources>
                    <resource>
                        <directory>src/main/webapp/META-INF</directory>
                        <filtering>true</filtering>
                        <targetPath>META-INF</targetPath>
                    </resource>
                </webResources>
            </configuration>
    </plugin>

That way, you can define these placeholder values as defaults that can be overriden for specific profiles:

<properties>
    <database.password>default_user</database.password>
    <database.username>default_password</database.username>
    <database.url>jdbc:oracle:thin:@oracle.host:1521:defaultsid</database.url>
</properties>

<profiles>  
        <profile>   
            <id>dev</id>
            <properties>
                <database.url>jdbc:oracle:thin:@oracle.host:1521:DEVELOPMENTsid</database.url> 
            </properties>
        </profile>
</profiles>