Parametrize anything using maven Resource filtering.

Was trying to Parametrize persistence.xml in src/main/resources/META-INF .  Came accross concept of  Resource Filtering by maven Resources plugin.

1. Replace values with variables in whichever file you want.

<property name=”hibernate.connection.driver_class” value=”${my.driverClassName}” />
<property name=”hibernate.connection.url” value=”${my.url}” />
<property name=”hibernate.connection.username” value=”${my.user}” />
<property name=”hibernate.connection.password” value=”${my.password}” />
<property name=”hibernate.dialect” value=”${my.dialect}”/>

2. Create profile in pom xml

<profiles>
<profile>
<id>myprofile</id>
<properties>
<my.driverClassName>oracle.jdbc.driver.OracleDriver</my.driverClassName>
<my.url>jdbc:oracle:thin:@localhost:1521:orcl</my.url>
<my.user>myuser</my.user>
<my.password>mypass</my.password>
<my.dialect>org.hibernate.dialect.Oracle9iDialect</my.dialect>
</properties>
</profile>
</profiles>

3. Activate resource filtering

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>

You need to do this, by default filtering is disabled. Even if your files are within subfolders of src/main/resources it will filter them. For example in this case  persistence.xml  is in src/main/resources/META-INF and it works ok.

4. Run maven like this:-

mvn clean install -Pmyprofile

 

http://www.sonatype.com/books/mvnref-book/reference/resource-filtering-sect-description.html

http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html