Activating/deactivating a Maven profile

A profile can be specified in pom.xml or settings.xml. Each profile may be created for a specific purpose; for instance, to run on a particular platform or to run in an integration environment. All profiles may not need to run in all cases. Maven provides a mechanism to activate and deactivate a profile as required.

Getting ready

Use the project where we created the profile to add a new Maven profile section.

How to do it...

Let's perform the following steps to activate/deactivate a Maven profile:

  1. To deactivate a profile, set the following value in the activeByDefault element:
    <activeByDefault>false</activeByDefault>
  2. Run the Maven command to check if the profile is active:
    mvn help:active-profiles
    

    The output for the preceding command is shown as follows:

    [INFO] --- maven-help-plugin:2.2:active-profiles (default-cli) @ project-with-profile ---
    [INFO]
    Active Profiles for Project 'com.packt.cookbook:project-with-profile:jar:1.0-SNAPSHOT':
    The following profiles are active:
    
  3. To activate the profile, set the following value:
    <activeByDefault>true</activeByDefault>
  4. Confirm that the profile is now active, by executing the following command:
    mvn help:active-profiles
    

    The output for preceding command is shown as follows:

    The following profiles are active:
    - dev (source: com.packt.cookbook:project-with-profile:1.0-SNAPSHOT)
    

How it works...

Profiles can be triggered in one of the following ways:

  • Explicitly: Here, Maven provides a command-line option to invoke a profile, shown in the following command:
    mvn –P dev package
    

    This invokes the dev profile

  • Through settings: A profile can be activated in the settings file by setting the <active> property to true. If activated, when the project is built, the profile is invoked:
      <activeProfiles>
        <activeProfile>dev</activeProfile>
      </activeProfiles>
  • Based on environment variables: The profile can be activated based on any environment variable and the value that it has:
    <profile>
        <activation>
          <property>
            <name>debug</name>
          </property>
        </activation>
        ...
    </profile>

    If the system property debug is defined and has any value, then the profile is activated

  • Based on OS settings: The following profile will only run on Windows:
    <profile>
        <activation>
          <os>
            <family>Windows</family>
          </os>
        </activation>
        ...
      </profile>
  • Present or missing files: The following profile will be activated if the target/site file is missing:
    <profile>
        <activation>
          <file>
            <missing>target/site</missing>
          </file>
        </activation>
      </profile>
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.17.128