Why Profiles are Our Friend
Maven allows for configurable properties to be set for a build so why would one need the notion of a build profile? Well there are subtle differences between these two features of Maven. A profile is able to represent a unique build environment more intuitively than using a set of configurable properties.
Common Profile Usage Scenarios
Different Databases for Development and Test EnvironmentsIt is very easy to combine property filtering with profiles to keep your build portable to different environments (i.e. development, test, quality assurance).
First you can define your separate profiles in your settings.xml file. Notice in the below example that each of the two profiles define their own properties for the JDBC configuration.
<settings>
<localRepository>/home/buildadm/maven/team/atc/repository</localRepository>
<profiles>
<profile>
<id>env-dev</id>
<activation>
<property>
<name>env</name>
<value>dev</value>
</property>
</activation>
<properties>
<jdbc.driverClassName>com.mysql.jdbc.Driver</jdbc.driverClassName>
<jdbc.url>jdbc:mysql://localhost:3306/local_db</jdbc.url>
<jdbc.username>localuser</jdbc.username>
<jdbc.password>localpass</jdbc.password>
</properties>
</profile>
<profile>
<id>env-qual</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<jdbc.driverClassName>com.mysql.jdbc.Driver</jdbc.driverClassName>
<jdbc.url>jdbc:mysql://qual-db.mycompany.com:3306/my_qual_db</jdbc.url>
<jdbc.username>qualuser</jdbc.username>
<jdbc.password>qualpass</jdbc.password>
</properties>
</profile>
</profiles>
</settings>Next we want to enable our project to filter it's configuration using the properties defined in the profile.
We can do this by instructing our project's pom.xml file to filter resources. In this example we are only interested in filtering a properties file so our DAO tests can connect to the database. Therefore we specify the directory as
~src/test/resources~.
<project>
…
<build>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
</build>
…
</project>Now when the build is run the jdbc.properties file in src/test/resources will be transformed from…
jdbc.driverClassName=${jdbc.driverClassName}
jdbc.url=${jdbc.url}
jdbc.username=${jdbc.username}
jdbc.password=${jdbc.password}to...
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://qual-db.mycompany.com:3306/my_qual_db
jdbc.username=qualuser
jdbc.password=qualpass