skip to content

Profiles & Activation

Profiles declared in the POM or settings.xml and the triggers that activate them — property, JDK, OS, file presence, or an explicit -P. Interviewers ask because activeByDefault behaves less intuitively than most people assume.

on this pageshow

explore

questions

5

What is a Maven profile, and why would you use one?

level: juniorimportance: must knowfreq 75%

answer

  1. named conditional config slice
  2. pom.xml vs settings.xml
  3. overlay deps/plugins/properties
  4. if-block for the build
  5. env-specific overrides

basics

~20 s

A Maven profile is a named set of build settings (dependencies, plugins, properties) that can be turned on or off. You use it to vary the build for different environments like dev, test, and prod without changing the main configuration.

solid answer

~40 s

A profile is a conditional, named slice of build configuration declared under `<profiles>` in a `pom.xml` (or `settings.xml`). When active it overlays extra or overriding `<dependencies>`, `<plugins>`, `<properties>`, `<build>` settings, and even `<repositories>` onto the effective build. Profiles exist to make one build adapt to varying conditions — environment (dev/staging/prod), OS, JDK version, or presence of a file — without duplicating whole POMs. You activate them explicitly with `mvn -P <id>`, or automatically via `<activation>` triggers. The classic use is swapping a JDBC URL or packaging flag per environment. Best practice: keep profiles small and prefer them for environment-specific overrides, not core logic — over-using profiles makes the effective build hard to reason about.

code

xml · 8 lines
xml
<profiles>
  <profile>
    <id>prod</id>
    <properties>
      <db.url>jdbc:postgresql://prod-host/app</db.url>
    </properties>
  </profile>
</profiles>

go deeper

for a junior

Know it is a named on/off set of build settings used for environments like dev/prod.

for a middle

Know it lives in pom.xml or settings.xml and can override properties, dependencies, plugins, and repositories.

for a senior

Weigh profiles against alternatives; keep them small and prefer them for environment overrides rather than core build logic.

for a principal

Govern profile usage across a multi-module org — naming conventions, where env config belongs, avoiding non-deterministic builds.

## What a profile is Maven builds are normally driven by a single `pom.xml`. A **profile** is a named bundle of build configuration that is only applied when the profile is **active**. Think of it as an `if`-block for your build: "if this profile is on, also use these settings." A profile can contribute almost anything a POM can: `<properties>`, `<dependencies>`, `<dependencyManagement>`, `<build>` (plugins, plugin config, final name), `<repositories>`, `<pluginRepositories>`, `<reporting>`, and module lists (`<modules>`). ## Why profiles exist The core problem they solve is **one source tree, many build conditions**: - Different environments (local dev vs CI vs production) need different property values — e.g. a database URL or a logging level. - Some plugins should only run on certain operating systems or JDK versions. - You want to include extra modules or dependencies only in some situations. Without profiles you'd copy-paste POMs or hack the build with scripts. Profiles keep the variation declarative and inside Maven. ## Where they live - **`pom.xml`** — project-scoped profiles, committed with the code. - **`settings.xml`** (in `~/.m2/` or the Maven install) — user/machine-scoped profiles, good for credentials or machine paths you don't want in version control. ## How they turn on - Explicitly: `mvn -P profileId` (comma-separated for several). - Automatically: `<activation>` triggers (property present, JDK range, OS, a file existing/missing, or `activeByDefault`). ## Example ```xml <profiles> <profile> <id>prod</id> <properties> <db.url>jdbc:postgresql://prod-host/app</db.url> <log.level>WARN</log.level> </properties> </profile> </profiles> ``` Run with `mvn package -P prod` and `${db.url}` resolves to the prod value in filtered resources. ## Caution Profiles add hidden state: the same `mvn package` can produce different artifacts depending on which profiles are active. Keep them few, well-named, and document which are default.

  • Can a profile add a dependency that the base build doesn't have?
    Yes. A profile may contain its own `<dependencies>` block; when the profile is active those dependencies are merged into the effective dependency set.
  • What's a downside of relying heavily on profiles?
    They make the build non-deterministic from the command alone — the same `mvn package` yields different results depending on active profiles, which can surprise teammates and CI.

A profile is like a costume you put on the build for a specific occasion — the actor (project) is the same, but the outfit (settings) changes for the venue.

saying these in an interview costs you the question

  • Saying a profile is a separate pom file (it is a section inside a pom or settings.xml).
  • Claiming profiles can only set properties — they can override dependencies, plugins, repositories and modules too.

context

open as a page

What activation triggers can automatically enable a Maven profile, and how do they behave?

level: middleimportance: must knowfreq 70%

basics

~10 s

Besides activating with -P, a profile's <activation> can turn it on automatically: activeByDefault, a property being set, a JDK version range, the OS, or whether a file exists or is missing.

open as a page

How do you explicitly activate and deactivate profiles from the command line?

level: juniorimportance: should knowfreq 60%

basics

~10 s

Use -P with the profile id to turn it on (mvn package -P prod). Add several with commas. Prefix the id with ! or - to turn a profile off: mvn package -P !slow-tests.

open as a page

What is the difference between declaring a profile in pom.xml versus settings.xml, and when do you choose each?

level: seniorimportance: should knowfreq 55%

basics

~20 s

pom.xml profiles travel with the project and can change almost anything in the build. settings.xml profiles live on the developer's machine (~/.m2), aren't committed, and are limited to properties, repositories, and plugin repositories — good for machine-specific or secret values.

open as a page

How do profiles override dependencies and plugin configuration, and what risks does this introduce for reproducible builds?

level: principalimportance: should knowfreq 40%

basics

~20 s

An active profile merges its dependencies, plugin config, and properties into the effective build, overriding base values. The risk is that the same command produces different artifacts depending on which profiles are active, which can break reproducibility and surprise CI.

open as a page