11.5. Maven build profile

发布时间 : 2025-10-25 13:35:09 UTC      

Page Views: 9 views

The build configuration file is the value of a series of configuration items that can be used to set or override Maven build defaults.

Using the build configuration file, you can customize the build method for different environments, such as production (Production) and development (Development) environments.

Configuration files are specified using activeProfiles or profiles elements in the pom.xml file and can be triggered in a variety of ways. The configuration file modifies the POM at build time and is used to set parameters to different target environments (for example, addresses of database servers in development (Development), test (Testing), and production (Production) environments).

11.5.1. Type of build profile

There are generally three types of build configuration files:

Types

Where is it defined?

Project level (Per Project)

定义在项目的POM文件pom.xml中

User level (Per User)

Defined in the settings xml file of Maven (% USER_HOME%/.m2/settings.xml)

Global (Global)

Defined in the Maven global settings xml file (% M2_HOME%/conf/settings.xml)

11.5.2. Profile Activation

Maven’s build configuration file can be activated in a number of ways.

  • Use the command console to enter explicit activation.

  • Set through maven.

  • Based on environment variables (user or system variables).

  • Operating system settings (for example, Windows series).

  • The existence or absence of a file.

11.5.3. Profile Activation instance

Assume that the project structure is as follows:

image0

There are three test files under the src/main/resources folder:

File name

Description

env.properties

If you do not specify a configuration file, the configuration to use by default.

env.test.properties

The test configuration when the test profile is used.

env.prod.properties

The production configuration when the production profile is used.

注意: These three configuration files do not represent the functions of the build configuration file, but are used for the purpose of this test; for example, when I specify that the build configuration file is prod, the project uses the env.prod.properties file.

注意: The following example still uses the AntRun plug-in because it binds the Maven lifecycle phase and outputs information, copies files, and so on without writing a bit of code through Ant tags. The rest has nothing to do with this build profile.

11.5.4. 1. Profile activation

Profile allows us to define a series of configuration information and then specify its activation conditions. In this way, we can define multiple profile, and then each profile corresponds to different activation conditions and configuration information, thus achieving the effect that different environments use different configuration information.

In the following example, we add the maven-antrun-plugin:run target to the test phase. This allows us to output text information in different profile. We will use pom.xml to define different profile and use the maven command to activate profile in the command console.

The pom.xml file is as follows:

<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">4.0.0com.jsoft.testtestprojectjar0.1-SNAPSHOTtestprojecthttp://maven.apache.orgjunitjunit3.8.1testtestorg.apache.maven.pluginsmaven-antrun-plugin1.8testrunUsing env.test.propertiesecho><copyfile="src/main/resources/env.test.properties"tofile="${project.build.outputDirectory}/env.properties"overwrite="true"/>tasks>configuration>execution>executions>plugin>plugins>build>profile><profile><id>normalid><build><plugins><plugin><groupId>org.apache.maven.pluginsgroupId><artifactId>maven-antrun-pluginartifactId><version>1.8version><executions><execution><phase>testphase><goals><goal>rungoal>goals><configuration><tasks><echo>Using env.propertiesecho><copyfile="src/main/resources/env.properties"tofile="${project.build.outputDirectory}/env.properties"overwrite="true"/>tasks>configuration>execution>executions>plugin>plugins>build>profile><profile><id>prodid><build><plugins><plugin><groupId>org.apache.maven.pluginsgroupId><artifactId>maven-antrun-pluginartifactId><version>1.8version><executions><execution><phase>testphase><goals><goal>rungoal>goals><configuration><tasks><echo>Using env.prod.propertiesecho><copyfile="src/main/resources/env.prod.properties"tofile="${project.build.outputDirectory}/env.properties"overwrite="true"/>tasks>configuration>execution>executions>plugin>plugins>build>profile>profiles>project>    

注意:构建配置文件 The use of Node.

说明: There are three new ones on it. , of which Distinguish between different Perform different AntRun tasks; while the task of AntRun can be understood this way, AntRun listens on the Maven lifecycle phase of test, and when Maven executes test, it triggers the AntRun task, which outputs the text and copies the file to the specified location; as for which AntRun task to execute, 构建配置文件 It plays the role of transmitting the specified, for example, by entering the specified .

Execute the command:

mvn test -Ptest 

Tip: the first test is the Maven lifecycle phase, and the second test is 构建配置文件 The specified < id > parameter, which is transmitted by-P, of course, it can be prod or normal, which you define. .

The result of the operation is as follows:

image1

You can see that the task of AntRun was successfully triggered. And it’s corresponding. 构建配置文件 The < id > under is the task of test.

Test the remaining two commands, and the results are as follows:

image2

image3

11.5.5. 2. Activate the configuration file through Maven settings

open %USER_HOME%/.m2 Under the directory settings.xml Files, where %USER_HOME% Represents the user’s home directory. If the setting.xml file does not exist, copy it directly. %M2_HOME%/conf/settings.xml To the .m2 directory, where %M2_HOME% Represents the installation directory for Maven.

Configure setting.xml files, adding 属性:

<settingsxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">...test 

Execute the command:

mvn test 

提示 1: At this point, there is no need to use-Ptest to enter parameters, the < activeprofile > of the above setting.xml file has specified the test parameter instead.

提示 2: It can also be used in the %M2_HOME%/conf/settings.xml The file is configured with the same effect.

Execution result:

image4

11.5.6. 3. Activate the configuration file through the environment variable

First, remove all the setting.xml values tested in the previous step.

Then in pom.xml, < id > is the < profile > node of test, and add the < activation > node:

<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">4.0.0com.jsoft.testtestprojectjar0.1-SNAPSHOTtestprojecthttp://maven.apache.orgjunitjunit3.8.1testtestenvtestorg.apache.maven.pluginsmaven-antrun-plugin1.8testrunUsing env.test.propertiesecho><copyfile="src/main/resources/env.test.properties"tofile="${project.build.outputDirectory}/env.properties"overwrite="true"/>tasks>configuration>execution>executions>plugin>plugins>build>profile><profile><id>normalid><build><plugins><plugin><groupId>org.apache.maven.pluginsgroupId><artifactId>maven-antrun-pluginartifactId><version>1.8version><executions><execution><phase>testphase><goals><goal>rungoal>goals><configuration><tasks><echo>Using env.propertiesecho><copyfile="src/main/resources/env.properties"tofile="${project.build.outputDirectory}/env.properties"overwrite="true"/>tasks>configuration>execution>executions>plugin>plugins>build>profile><profile><id>prodid><build><plugins><plugin><groupId>org.apache.maven.pluginsgroupId><artifactId>maven-antrun-pluginartifactId><version>1.8version><executions><execution><phase>testphase><goals><goal>rungoal>goals><configuration><tasks><echo>Using env.prod.propertiesecho><copyfile="src/main/resources/env.prod.properties"tofile="${project.build.outputDirectory}/env.properties"overwrite="true"/>tasks>configuration>execution>executions>plugin>plugins>build>profile>profiles>project>    

Execute the command:

mvn test -Denv=test 

提示 1: Above, use-D to pass the environment variable, where env corresponds to the < name > value just set, and test corresponds to < value >.

提示 2: The environment variable of the system is tested on Windows 10, but it does not take effect, so it can only be passed through-D.

Execution result:

image5

11.5.7. 4. Activate the configuration file through the operating system

The activation element contains the following operating system information. When the system is windows XP, test Profile will be triggered.

<profile><id>testid><activation><os><name>Windows XPname><family>Windowsfamily><arch>x86arch><version>5.1.2600version>os>activation>profile>    

Now open the command console, jump to the directory where pom.xml is located, and execute the following mvn command. Do not use the-P option to specify the name of the Profile. Maven displays the results of the activated test Profile.

mvn test 

11.5.8. 5. Activate the configuration file through the existence or absence of the file

Now use the activation element to contain the following operating system information. When target/generated-sources/axistools/wsdl2java/com/companyname/group is missing, test Profile will be triggered.

<profile><id>testid><activation><file><missing>target/generated-sources/axistools/wsdl2java/ com/companyname/groupmissing>file>activation>profile>    

Now open the command console, jump to the directory where pom.xml is located, and execute the following mvn command. Do not use the-P option to specify the name of the Profile. Maven displays the results of the activated test Profile.

mvn test 

Reference: https://www.cnblogs.com/EasonJim/p/6828743.html

《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。