Activate Maven Profile by Property

By Arvind Rai, June 12, 2021
On this page we will learn to activate Maven profile by property. Maven provides <property> element that is used within <activation> element. Property name and value can be specified using <name> and <value> elements.
<activation>
  <property>
	<name></name>
	<value></value>
  </property>
</activation> 

System Property debug

1. Here we will activate a profile for system property 'debug'. If the system property 'debug' is defined, the below profile will be activated.
pom.xml
<project>
 ------  
 <profiles>
  <profile>
	<id>debug_profile</id>
	<activation>
	  <property>
		<name>debug</name>
	  </property>
	</activation>
	<build>
	  <resources>
		<resource>
		  <directory>src/main/resources/debug-files</directory>
		</resource>
	  </resources>
	</build>
  </profile>
  ------
 </profiles>
</project> 
The profile id 'debug_profile' will be activated for any value of 'debug' system property. The profile is changing default resource location to below location.
src/main/resources/debug-files 
2. If we want to activate a profile when system property 'debug' is not defined at all, use property name as '!debug' .
<activation>
  <property>
	<name>!debug</name>
  </property>
</activation> 
3. Here we are specifying a value for our system property.
<activation>
  <property>
	<name>debug</name>
	<value>!true</value>
  </property>
</activation> 
The profile will be activated in the case either system property 'debug' is not defined or its value is not 'true' .

To activate profile by property we run Maven command as following.
mvn clean install -Ddebug=false 

System Property environment

Find the code to use system property 'environment' .
pom.xml
<project>
 ------  
 <profiles>
  <profile>
	<id>env_profile</id>
	<activation>
	  <property>
		<name>environment</name>
		<value>prod</value>
	  </property>
	</activation>
	<build>
	  <resources>
		<resource>
		  <directory>src/main/resources/prod-files</directory>
		</resource>
	  </resources>
	</build>
  </profile>
  ------
 </profiles>
</project> 
To activate above profile, we can run Maven command as following.
mvn clean install -Denvironment=prod 

Reference

Introduction to Build Profiles
POSTED BY
ARVIND RAI
ARVIND RAI







©2024 concretepage.com | Privacy Policy | Contact Us