Maven Profile activeByDefault

By Arvind Rai, May 25, 2021
The <activeByDefault> element is used in pom.xml to decide if profile should be activated by default or not. The value of <activeByDefault> can be true or false. To activate profiles using settings.xml, we use <activeProfile> element and assign profile id in it.
The <activeByDefault> can be used in pom.xml as following.
<project>
        ------
	<profiles>
		<profile>
                        ------
			<activation>
				<activeByDefault>true</activeByDefault>
			</activation>
                        ------			
		</profile> 
	</profiles>
</project> 
I am using Maven-3.8.1 in my demo application.

Types of Profile

Profiles can be declared in pom.xml and settings.xml files. Types of profile can be categorized in following way.
1. Per Project: Defined in pom.xml itself.
2. Per User : Defined in user settings.xml located at (%USER_HOME%/.m2/settings.xml).
3. Global : Defined in global settings.xml located at (${maven.home}/conf/settings.xml).

activeByDefault in pom.xml

Find the sample Maven code snippet that is using <activeByDefault> tag.
pom.xml
<project>
        ------
	<profiles>
		<profile>
			<id>installConfig</id>
			<activation>
				<activeByDefault>true</activeByDefault>
			</activation>			
			<build>
				<defaultGoal>install</defaultGoal>
				<directory>C:/java-projects/target</directory>
			</build>
		</profile>
		<profile>
			<id>osConfig</id>
			<activation>
				<activeByDefault>false</activeByDefault>
				<jdk>1.5</jdk>
				<os>
					<name>Windows XP</name>
					<family>Windows</family>
					<arch>x86</arch>
					<version>5.1.2600</version>
				</os>
			</activation>
		</profile>
	</profiles>
</project> 
Find the output of command mvn clean install
Maven Profile activeByDefault
We can see that JAR file is being created in the location defined by the active profile in the above pom.xml.

activeProfiles in settings.xml

Profiles can also be configured and activated in settings.xml file. Create the profile using <profiles> tag and activate them using <activeProfile> tag.
settings.xml
<settings>
  <profiles>
    <profile>
        <id>android-settings</id>
        <properties>
            <android.sdk.path>C:\Users\Arvind\android-sdks</android.sdk.path>
        </properties>
    </profile>
  </profiles>
  <activeProfiles>
        <activeProfile>android-settings</activeProfile>
  </activeProfiles>
</settings> 

Deactivating activeByDefault Profile

As we know that if the value for activeByDefault is true for a profile, then that profile will be activated by default. If we want to deactivate that profile for specific goal run, we can use -P option with (!) with profile id.
mvn clean install -P !installConfig 

Command to Display All Active Profiles

Maven has Help plugin that gives information about a project or the system. To display all active plugins, use following command.
mvn help:active-profiles 

References

Introduction to Build Profiles
Settings Reference
POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us