Maven profiles

We all work in different environments, development, test (maybe integration) and production servers or, maybe a set of servers that run the same application with different configurations.

Maven helps us build our jar and war components for different environments configuring profiles.

We are going to see here how we can use Maven profiles to build and package Java jar files configured for specific environments.

Environment dependencies

Let’s say we are working with JPA and we have a persistence.xml file. This file has specific information about the database to connect to set in properties like:

hibernate.connection.url
hibernate.connection.username
hibernate.connection.password:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
	<persistence-unit name="specificmedia" transaction-type="RESOURCE_LOCAL">
		<class>com.persistence.Account</class>
		<class>com.persistence.User</class>
		<class>com.persistence.Client</class>
		<class>com.persistence.Product</class>
	<properties>
		<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
		<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
		<property name="hibernate.connection.username" value="${db.username}"/>
		<property name="hibernate.connection.password" value="${db.password}"/>
		<property name="hibernate.connection.url" value="${db.connectionURL}/database"/>
		........................................................................
	</properties>
	</persistence-unit>
</persistence>

You can assign placeholders to every property that is bound to change depending on the environment you are deploying this artifact to.
These placeholders will be replaced with the correct value for each environment when you package your artifact to deploy it.

For instance, if you are deploying it to the test environment the placeholder will get replaced with the connection details to your test database.

Configuring the pom.xml

There are two things that you need to do in the pom.xml

Define the Profiles

Define the values for the placeholders in every environment using the profiles tag:

<profiles>
	<profile>
		<id>development</id>
		<activation>
			<activeByDefault>true</activeByDefault>
		</activation>
 
		<properties>
			<db.connectionURL>jdbc:mysql://localhost:3306</db.connectionURL>
			<db.username>root</db.username>
			<db.password>your_password</db.password>
		</properties>
	</profile>
 
	<profile>
		<id>integration</id>
		<activation>
			<property>
				<name>env</name>
				<value>eu-integration</value>
			</property>
		</activation>
 
		<properties>
			<db.connectionURL>jdbc:mysql://integration.server.ip:3306</db.connectionURL>
			<db.username>root</db.username>
			<db.password></db.password>
		</properties>
	</profile>
</profiles>

Every profile has to be identified by an unique id: integration. This id is required to indicate the profile when you build your project using the -P option:

mvn -P integration clean package

Resource Filtering

Activate maven filtering for the resources containing placeholders.

We are indicating where are the resources containing placeholders. So Maven knows where are the files with placeholders to be replaced (at built time) by the information defined in the tag in the pom.xml

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

Maven Profiles Activation

Just to explain a bit the activation section in the pom.xml. Maven activation is a very powerful tool that lets you activate a profile only under certain circumstances.

Here, you can see activeByDefault=true, that is used to indicate the default profile (the one that gets triggered by default when no other conditions are set).

<id>development</id>
<activation>
	<activeByDefault>true</activeByDefault>
</activation>

So you these two will get the environment ‘development‘ properties:

mvn clean package
mvn -P development clean package

I have also added an activation property (env) that, when set to a given value, chooses the profile:

<id>eu-integration</id>
<activation>
	<property>
		<name>env</name>
		<value>eu-integration</value>
	</property>
</activation>

Therefore, the following two statements are equivalents:

mvn -Denv=integration clean package
mvn -P integration clean package

Resources

Popularity: 12% [?]

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
You can leave a response, or trackback from your own site.

Leave a Reply