MODULE 2 · TOPIC 7 · VISUAL MAVEN LAB

Maven Profiles

Maven profiles customize the effective build model for particular environments or build conditions. They can change properties, dependencies, plugins and resource configuration when activated. Profiles are build-time mechanisms, not automatic runtime switches. The experiments show how activation, merging and filtering change the build and how runtime settings remain independent.

Browser-based Maven simulations: change the inputs and trace commands, POM configuration and build results. No real Maven commands, downloads or file changes are executed.

1. Environment profiles — dev, test, prod

A profile is a named set of conditional POM configuration. When active, it contributes its settings to the effective build model. Profiles can represent development, testing or production build needs without maintaining separate copies of the entire POM.

Why this example? Select dev, test or prod and inspect the resulting property. This shows how the same project can produce different build configurations while keeping a common project definition.

Before you run: Profiles change the effective build model. Compare properties, plugin configuration and generated resources, rather than assuming a profile is an application runtime switch.
Maven command / configuration
Build / simulation outputConsole
Data operationsCompact blocks
CurrentResultError / rejected
Why this step happens
See all steps in this scenario

    Change the input and step through the example. Commands and XML fragments are illustrative; use the real Maven installation and the specified project files to reproduce them.

    What this animation explains

    Profiles merge build configuration when active. They are not Java runtime profiles; the example uses a property to represent an environment.

    Try and observe

    Change dev to prod and inspect the merged property.

    2. Profile activation rules

    Profiles can activate explicitly through -P or implicitly through conditions such as user properties. An activeByDefault profile in a POM is suppressed when another profile in that same POM becomes active. More than one profile may be active at a time.

    Why this example? Compare explicit, property and default activation scenarios. Observe the selected profile set to understand why a default configuration can disappear without manually disabling it.

    Before you run: Activation mechanisms may combine. Use the new two-input activation lab to see default suppression and multiple active profiles.
    Maven command / configuration
    Build / simulation outputConsole
    Data operationsCompact blocks
    CurrentResultError / rejected
    Why this step happens
    See all steps in this scenario

      Change the input and step through the example. Commands and XML fragments are illustrative; use the real Maven installation and the specified project files to reproduce them.

      What this animation explains

      A default-active profile in a POM is deactivated when another profile from that POM becomes active. Property activation depends on the specified property. Multiple explicit profiles may be active.

      Try and observe

      Compare Default only with Another profile active.

      3. Resource filtering with profiles

      Resource filtering replaces placeholders in configured text resources with effective Maven property values during the build. The selected profile can supply those values. Filtering is optional, and credentials should not be embedded in packaged resources.

      Why this example? Choose a profile and follow the placeholder to the generated app.properties content. This demonstrates how profile configuration affects an actual file in the build output rather than just a label in the POM.

      Before you run: Filtering substitutes properties in configured text resources during processing. Keep secrets outside build artifacts and inspect generated output before packaging.
      Maven command / configuration
      Build / simulation outputConsole
      Data operationsCompact blocks
      CurrentResultError / rejected
      Why this step happens
      See all steps in this scenario

        Change the input and step through the example. Commands and XML fragments are illustrative; use the real Maven installation and the specified project files to reproduce them.

        What this animation explains

        Resource filtering substitutes configured Maven properties into text resources. Enable filtering explicitly and avoid filtering binary files. Never embed production credentials in a packaged resource.

        Try and observe

        Switch profile and observe the generated app.properties.

        4. Multiple profile activation — default, CLI and property

        Explicit activation and property-based activation can occur together. Profiles are not mutually exclusive by default, so Maven can merge contributions from multiple active profiles. Default activation has a special suppression rule within the same POM.

        Why this example? Toggle explicit QA activation and env=prod independently. Inspect the active set, including both QA and prod together, to understand why specifying one profile does not necessarily disable every other profile.

        Before you run: This example contains three profiles in the same POM. Activation across parent POMs and settings has additional model-building rules.
        Maven command / configuration
        Build / simulation outputConsole
        Data operationsCompact blocks
        CurrentResultError / rejected
        Why this step happens
        See all steps in this scenario

          Change the input and step through the example. Commands and XML fragments are illustrative; use the real Maven installation and the specified project files to reproduce them.

          What this animation explains

          Profiles are not exclusive by default. Explicit and implicit activations can combine; an activeByDefault profile in the same POM is suppressed when another activates.

          Try and observe

          Choose Explicit QA plus env=prod to see both profiles active, then remove both to restore dev.

          5. Profile conflicts — which property wins?

          When multiple active profiles in one POM define the same scalar property, the later declaration can override the earlier one. This is distinct from the order in which profile names appear on the command line. Other model elements can have more complex merge rules.

          Why this example? Activate both dev and prod, reverse their declaration order and compare the effective app.env. Then select only one profile to see how avoiding overlapping definitions removes the ambiguity.

          Before you run: The simulation only models same-POM profile property merging. Use help:effective-pom to diagnose more complex inheritance or plugin merges.
          Maven command / configuration
          Build / simulation outputConsole
          Data operationsCompact blocks
          CurrentResultError / rejected
          Why this step happens
          See all steps in this scenario

            Change the input and step through the example. Commands and XML fragments are illustrative; use the real Maven installation and the specified project files to reproduce them.

            What this animation explains

            If active profiles in the same POM define the same scalar property, the later-declared profile takes precedence. This is distinct from CLI activation order.

            Try and observe

            Activate both, reverse the declaration order, and inspect app.env. Use one profile only to eliminate the conflict.

            6. Build-time versus runtime configuration

            Build-time settings are resolved while Maven produces an artifact. Runtime settings are read by the application when it runs, if the application is written or configured to do so. A Maven profile does not automatically set a running Java application’s environment.

            Why this example? Build with dev or prod and independently choose a runtime APP_ENV override. Follow the packaged default and the runtime value to understand how one artifact can support different deployment environments.

            Before you run: The example models explicit application logic, not automatic Java or Spring behavior. Prefer runtime configuration for environment-specific secrets.
            Maven command / configuration
            Build / simulation outputConsole
            Data operationsCompact blocks
            CurrentResultError / rejected
            Why this step happens
            See all steps in this scenario

              Change the input and step through the example. Commands and XML fragments are illustrative; use the real Maven installation and the specified project files to reproduce them.

              What this animation explains

              Maven profiles affect build configuration. Runtime environment variables affect application behavior only if the application reads them.

              Try and observe

              Build with dev, set runtime prod, and inspect which value is used. Remove the override to fall back to the packaged default.

              Command and configuration reference

              Use these commands in the terminal of a real Maven project. Each serves a different purpose; the simulations above show the corresponding logic.

              CommandWhy and when to use it
              mvn help:active-profilesSee active profiles
              mvn -Pdev help:effective-pomInspect effective model for dev
              mvn -Denv=prod help:active-profilesActivate property-based profile if configured
              mvn -Pdev process-resourcesProcess configured filtered resources
              mvn -Pprod clean verifyBuild with prod configuration
              mvn -Pdev,prod help:effective-pomInspect overlapping profile contributions

              Worked practical examples

              Follow the steps in order using a real JDK and Maven installation. XML snippets marked as fragments belong inside the relevant POM element.

              1. Create profile-specific resources

              Procedure

              1. Use the demo POM. Add the following profiles and merge the resources block into the existing build element (do not create duplicate build elements).
              2. Create src/main/resources/app.properties containing app.env=${app.env}.
              3. Run mvn -Pdev clean process-resources and inspect target/classes/app.properties. Repeat with -Pprod.
              4. Run help:active-profiles and help:effective-pom to confirm activation and resolved property.
              Configuration / commands
              <profiles>
               <profile><id>dev</id><activation><activeByDefault>true</activeByDefault></activation><properties><app.env>dev</app.env></properties></profile>
               <profile><id>prod</id><properties><app.env>prod</app.env></properties></profile>
              </profiles>
              <!-- Add inside existing build element: -->
              <resources><resource><directory>src/main/resources</directory><filtering>true</filtering></resource></resources>
              Expected result: The generated text changes from app.env=dev to app.env=prod. Existing source template remains unchanged.

              2. Investigate overlapping profile properties

              Procedure

              1. Use the two profiles above. Run with both profiles explicitly active.
              2. Inspect the effective app.env property; then reverse their declaration order in the POM.
              3. Run the same command again.
              4. Avoid accidental conflicts by selecting only the intended profile or using distinct property names.
              Configuration / commands
              mvn -Pdev,prod help:active-profiles
              mvn -Pdev,prod help:effective-pom
              Expected result: Both profiles can be active; the later-defined same-POM property wins in this scalar example.

              Predict the outcome before you run

              Try answering these questions, then expand each answer and repeat the appropriate simulation.

              1. Does -Pprod make a running Java application use production mode automatically?

              No. Maven profiles configure a build; runtime behavior depends on the application and its configuration.

              2. What happens to a default-active profile when another profile in the same POM is active?

              The default-active profile is suppressed.

              3. Should a database password be put into filtered app.properties in a JAR?

              No. Use runtime secrets/configuration, not packaged credentials.

              Hands-on practice and assessment

              Programming challenge

              Create dev and prod profiles with different non-secret properties. Run mvn -Pdev help:active-profiles and mvn -Pprod help:effective-pom, then compare filtered resources.

              Edit the scratchpad below. Copy the appropriate command or XML fragment into your real Maven project to test it.

              Quick assessment

              Key points and real Maven notes

              Profiles are build-time configuration. Use runtime environment variables or secret management for sensitive values. The simulated URLs use illustrative endpoints.