Search code examples
javamavenprofiles

How to use Maven Profile Name as a variable


I need to build a war file that can call a JDBC connection depending on it's profile name.

When I build with the dev profile, I need it to call the dev.properties file. And when I build with the prod profile, I need it to read from the prod.properties file.

The idea that I came up with is to use the profile name as a variable somehow, but I can't figure out how to do it.

POM.XML

<profile>
    <id>dev</id>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>
    <properties>
        <profile-id>dev</profile-id>
    </properties>
</profile>
<profile>
    <id>prod</id>
    <properties>
        <profile-id>prod</profile-id>
    </properties>
</profile>

servlet-context.xml

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>classpath:${profile-id}.properties</value>
    </property>
</bean>
<property name="dataSourceProperties">
    <props>                 
        <prop key="url" >${jdbc}</prop>
        <prop key="user">user</prop>
        <prop key="password">password</prop>    
        <prop key="implicitCachingEnabled">true</prop>          
    </props>
</property>

Solution

  • You must use the filtering concept of maven. See http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html

    Add to your pom something like:

      <resource>
        <directory>directory.to.servlet/servlet-context.xml</directory>
        <filtering>true</filtering>
      </resource>
    

    By replacing directory.to.servlet with the real directory