YouTip LogoYouTip

Maven Build Profiles

# Maven Build Profiles Build profiles are a set of configuration values that can be used to set or override Maven build defaults. By using build profiles, you can customize the build process for different environments, such as Production and Development. Profiles are specified in the `pom.xml` file using the `activeProfiles` or `profiles` elements and can be triggered in various ways. Profiles modify the POM during the build and are used to set different target environments for parameters (for example, the address of a database server in Development, Testing, and Production environments). * * * ## Types of Build Profiles Build profiles are generally of three types: | Type | Where Defined | | --- | --- | | Per Project | Defined in the project's POM file, `pom.xml` | | Per User | Defined in Maven's settings XML file (`%USER_HOME%/.m2/settings.xml`) | | Global | Defined in Maven's global settings XML file (`%M2_HOME%/conf/settings.xml`) | * * * ## Profile Activation Maven build profiles can be activated in several ways. * Explicit activation via command-line input. * Through Maven settings. * Based on environment variables (user or system variables). * Operating system settings (e.g., Windows family). * Presence or absence of a file. ### Profile Activation Example Assume the project structure is as follows: !(#) Where there are three test files in the `src/main/resources` folder: | Filename | Description | | --- | --- | | `env.properties` | The default configuration used when no profile is specified. | | `env.test.properties` | The test configuration used when the test profile is active. | | `env.prod.properties` | The production configuration used when the production profile is active. | **Note:** These three configuration files do not represent the functionality of build profiles but are used for the purpose of this test. For example, when I specify the build profile as `prod`, the project uses the `env.prod.properties` file. **Note:** The following example still uses the AntRun plugin because this plugin can bind to Maven lifecycle phases and, through Ant's tags, can output information, copy files, etc., without writing any code. The rest is unrelated to this build profile. ### 1. Profile Activation A profile allows us to define a set of configuration information and then specify its activation conditions. This way, we can define multiple profiles, each corresponding to different activation conditions and configuration information, thereby achieving the effect of using different configuration information for different environments. In the following example, we add the `maven-antrun-plugin:run` goal to the test phase. This allows us to output text information in different profiles. We will use `pom.xml` to define different profiles and use Maven commands in the command console to activate a profile. The `pom.xml` file is as follows: 4.0.0com.jsoft.testtestprojectjar0.1-SNAPSHOTtestprojecthttp://maven.apache.orgjunitjunit3.8.1testtestorg.apache.maven.pluginsmaven-antrun-plugin1.8testrunUsing env.test.propertiesnormalorg.apache.maven.pluginsmaven-antrun-plugin1.8testrunUsing env.propertiesprodorg.apache.maven.pluginsmaven-antrun-plugin1.8testrunUsing env.prod.properties **Note:** The **build profiles** are defined using the **``** node. **Explanation:** Three **``** are created above, where the **``** distinguishes different **``** to execute different AntRun tasks. The AntRun task can be understood as follows: AntRun listens to the `test` Maven lifecycle phase. When Maven executes `test`, it triggers the AntRun task. The task outputs text and copies files to the specified location. As for which AntRun task to execute, the **build profile** acts as the specifier. For example, by specifying the **``** via command-line parameters. Execute the command: mvn test -Ptest **Hint:** The first `test` is the Maven lifecycle phase, and the second `test` is the `` parameter specified by the **build profile**. This parameter is passed via `-P`. Of course, it can be `prod` or `normal`, which are the **``** values you defined. The result of the execution is as follows: !(#) It can be seen that the AntRun task was successfully triggered. And it corresponds to the task under the **build profile** with `` as `test`. Test the other two commands, the results are as follows: !(#) !(#) ### 2. Activating Profiles via Maven Settings Open the `settings.xml` file in the `%USER_HOME%/.m2` directory, where `%USER_HOME%` represents the user's home directory. If the `settings.xml` file does not exist, simply copy `%M2_HOME%/conf/settings.xml` to the `.m2` directory, where `%M2_HOME%` represents the Maven installation directory. Configure the `settings.xml` file by adding the `` property: ... test Execute the command: mvn test **Hint 1:** You no longer need to use `-Ptest` to input parameters. The `` in the above `settings.xml` file has already specified the `test` parameter. **Hint 2:** You can also configure the file in `%M2_HOME%/conf/settings.xml`, with the same effect. Execution result: !(#) ### 3. Activating Profiles via Environment Variables First, remove all the values from the `settings.xml` tested in the previous step. Then, in the `pom.xml`, add an `` node to the `` node with `` as `test`: 4.0.0com.jsoft.testtestprojectjar0.1-SNAPSHOTtestprojecthttp://maven.apache.orgjunitjunit3.8.1testtestenvtestorg.apache.maven.pluginsmaven-antrun-plugin1.8testrunUsing env.test.propertiesnormalorg.apache.maven.pluginsmaven-antrun-plugin1.8testrunUsing env.propertiesprodorg.apache.maven.pluginsmaven-antrun-plugin1.8testrunUsing env.prod.properties Execute the command: mvn test -Denv=test **Hint 1:** The above uses `-D` to pass environment variables, where `env` corresponds to the `` value just set, and `test` corresponds to the ``. **Hint 2:** Tested on Windows 10 with system environment variables, but it did not work. Therefore, it can only be passed via `-D`. Execution result: !(#) ### 4. Activating Profiles via Operating System The `activation` element contains the following operating system information. When the system is Windows XP, the `test` Profile will be triggered. testWindows XPWindowsx865.1.2600 Now open the command console, navigate to the directory where `pom.xml` is located, and execute the following `mvn` command. Do not use the `-P` option to specify the Profile name. Maven will display the results of the activated `test` Profile. mvn test ### 5. Activating Profiles via File Presence or Absence Now use the `activation` element containing the following operating system information. When `target/generated-sources/axistools/wsdl2java/com/companyname/group` is missing, the `test` Profile will be triggered. testtarget/generated-sources/axistools/wsdl2java/ com/companyname/group Now open the command console, navigate to the directory where `pom.xml` is located, and execute the following `mvn` command. Do not use the `-P` option to specify the Profile name. Maven will display the results of the activated `test` Profile. mvn test * * * > Reference: https://www.cnblogs.com/EasonJim/p/6828743.html
← Maven Build Test ProjectMaven Tutorial β†’