Find the Best Cosmetic Hospitals

Explore trusted cosmetic hospitals and make a confident choice for your transformation.

“Invest in yourself — your confidence is always worth it.”

Explore Cosmetic Hospitals

Start your journey today — compare options in one place.

Maven Profiles in Practice: Definition, Activation, and Reliable Multi-Environment Builds

Maven profiles let you keep one pom.xml while adapting builds for different environments such as local development, CI, testing, or production. You will learn where profiles can be defined, how activation works, and how to use profiles without hurting build portability.

Originally published on 2010-04-14; fully revised and updated.

Reference basis: Apache Maven User Guide concepts for profiles, updated for practical DevOps usage. Original post: Profiles in Maven – How to Build Maven Profile?. Maven project site: Apache Maven.

Profiles in Maven

What Maven profiles are and when to use them

A profile is a conditional block of Maven configuration. When active, it can add or override parts of the build (properties, dependencies, plugins, repositories, and more) for a specific context.

  • Use profiles when builds differ by environment (for example, local vs CI).
  • Use profiles to avoid maintaining multiple POM files with -f.
  • Prefer profiles for complementary configuration, not for creating unrelated builds from one project.

Where Maven profiles are defined

Maven supports project, user, and global profile locations. Older Maven-era material also mentions a separate profiles.xml descriptor.

Profile typeDefined inScopeTypical use
Per-projectpom.xmlShared in source controlProject-specific dependencies, plugins, and properties
Per-user${user.home}/.m2/settings.xmlSingle developer or CI agentLocal paths, credentials references, machine-specific defaults
Global${maven.home}/conf/settings.xmlAll users of one Maven installationOrganization-wide defaults on managed hosts
Legacy descriptorprofiles.xml in project base directoryHistorical patternSeen in older Maven 2 documentation; verify current support

How profile activation works

Profiles can be activated explicitly from the command line or automatically through activation rules (JDK, OS, property, file presence/missing, or active-by-default behavior).

flowchart TD
    A[Run mvn command] --> B[Load project profiles]
    B --> C[Load user and global settings profiles]
    C --> D{Any -P flags}
    D -- Yes --> E[Apply explicit activate or deactivate]
    D -- No --> F[Evaluate activation rules]
    F --> G[Check jdk os property file conditions]
    G --> H[Apply activeByDefault when applicable]
    E --> I[Merge into effective pom]
    H --> I
    I --> J[Execute build lifecycle]

Important behavior: a profile with activeByDefault is typically deactivated when another profile in the same POM becomes active.

Activation methodHow to set itExampleBest use
Explicit CLI-P option-Pdev,testTemporary run-time choice
Settings default<activeProfiles> in settings.xml<activeProfile>corp</activeProfile>User or machine defaults
System property<activation><property>-Denvironment=testCI-driven environment selection
JDK<activation><jdk>17 or rangeJDK-specific build behavior
OS<activation><os><family>Windows</family>Platform-specific tooling
File exists/missing<activation><file><missing>target/...</missing>Generated-artifact or workspace checks

Example profile configuration in pom.xml

<profiles>
  <profile>
    <id>dev</id>
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>
    <properties>
      <app.env>dev</app.env>
    </properties>
  </profile>

  <profile>
    <id>test</id>
    <activation>
      <property>
        <name>environment</name>
        <value>test</value>
      </property>
    </activation>
    <properties>
      <app.env>test</app.env>
    </properties>
  </profile>

  <profile>
    <id>windows-build</id>
    <activation>
      <os>
        <family>Windows</family>
      </os>
    </activation>
  </profile>

  <profile>
    <id>generate-wsdl</id>
    <activation>
      <file>
        <missing>target/generated-sources/wsdl.marker</missing>
      </file>
    </activation>
  </profile>
</profiles>Code language: HTML, XML (xml)

For environment variables, Maven exposes them as properties prefixed with env. (for example, env.FOO).

Command-line usage: activate, deactivate, inspect

# Activate one or more profiles
mvn clean package -Pdev
mvn clean verify -Pdev,test

# Trigger property-based activation
mvn clean verify -Denvironment=test

# Deactivate profiles (quote ! in shells that treat it specially)
mvn clean package -P '!dev'
mvn clean package -P '!dev,!windows-build'

# Inspect profile resolution
mvn help:active-profiles
mvn help:all-profilesCode language: PHP (php)

Using settings.xml for user or machine-specific profiles

<settings>
  <activeProfiles>
    <activeProfile>corp-network</activeProfile>
  </activeProfiles>

  <profiles>
    <profile>
      <id>corp-network</id>
      <properties>
        <repo.mirror.enabled>true</repo.mirror.enabled>
      </properties>
    </profile>
  </profiles>
</settings>Code language: HTML, XML (xml)

Keep secrets and machine-only values in settings files, not in project pom.xml.

Best practices for reliable team builds

  • Prefer profile-driven properties over hardcoded absolute filesystem paths.
  • Use clear profile IDs such as dev, test, ci, prod.
  • Avoid unnecessary changes to artifact coordinates across profiles.
  • Document which profiles CI uses, and verify with mvn help:active-profiles.
  • Keep team-shared behavior in pom.xml; keep user/machine behavior in settings.xml.
  • Treat legacy profiles.xml usage as technical debt and confirm support before relying on it.

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
I'm Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms. I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.

Related Posts

Maven Learning Resources: A Practical Study Plan with the Better Builds with Maven PDF

The original post shared one Maven book link. This revised guide preserves that resource and adds a practical learning workflow, comparison table, and hands-on Maven commands.

Read More

Maven CLI Options Reference: Practical Flags, Comparisons, and Command Examples

A structured Maven command-line reference for CI/CD and local builds, including option comparisons, workflow guidance, and ready-to-use mvn commands. Based on the original 2013 cheatsheet, updated for readability and operational use.

Read More

Maven2 Web Application Setup: Legacy Step-by-Step Guide with Build, Jetty, and Site Reporting

A practical Maven2-era walkthrough for creating a Java web app, running core build goals, and generating docs and reports from pom.xml. This revision keeps the original workflow but adds structure, comparisons, and implementation-ready snippets.

Read More

Maven Clover for Distributed Java Applications: Instrument, Collect, and Aggregate Coverage

Learn a practical Clover + Maven workflow for distributed test environments. This walkthrough covers instrumentation, remote execution, coverage file collection, and final report generation.

Read More

Maven Reporting Plugins: Configure Site Reports in pom.xml (All, Selective, and reportSets)

Maven reporting plugins generate project documentation during the site lifecycle and are configured in the reporting section of pom.xml. This guide shows how to run all reports, select specific ones, and structure reportSets cleanly.

Read More

Maven Properties Reference: project.*, settings.*, env.*, and Custom POM Values

Maven properties are the backbone of reusable, environment-aware builds. This revised reference explains major property sources, interpolation flow, and practical commands for inspecting real values.

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
0
Would love your thoughts, please comment.x
()
x