English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The build configuration file is a series of values of configuration items that can be used to set or override the default values of Maven builds.
By using build configuration files, you can customize the build method for different environments, such as production (Production) and development (Development) environments.
The configuration file is specified in the pom.xml file using the activeProfiles or profiles elements, and can be triggered in various ways. The configuration file modifies the POM during the build and is used to set different target environments for parameters (for example, the address of the database server in the development (Development), testing (Testing), and production (Production) environments).
The build configuration file generally has three types:
Type | Where to define |
---|---|
Project level (Per Project) | Defined in the project's POM file pom.xml |
User level (Per User) | Defined in the Maven settings xml file (%USER_HOME%/.m2/settings.xml) |
Global(Global) | Defined in the Maven global settings xml file (%M2_HOME%/conf/settings.xml) |
The Maven build configuration file can be activated in various ways.
Use the command console to input explicit activation.
Through Maven settings.
Based on environment variables (user or system variables).
Operating system settings (such as, Windows series).
The existence or absence of files.
Assuming the project structure is as follows:
Among them, in src/main/There are three test files under the resources folder:
Filename | Description |
---|---|
env.properties | The default configuration used when no configuration file is specified. |
env.test.properties | Test configuration when the test configuration file is used. |
env.prod.properties | Production configuration when the production configuration file is used. |
Note:These three configuration files do not represent the function of the build configuration file, but are used for the purpose of this test; for example, when I specify the build configuration file as prod, the project will use env.prod.properties file.
Note:The following example is still using the AntRun plugin, because this plugin can bind the Maven lifecycle phase and output information, copy files, etc., without writing any code using Ant tags, which is just about it. The rest are irrelevant to the build configuration file of this build.
profiles allow us to define a series of configuration information and then specify the activation conditions. This allows us to define multiple profiles, each corresponding to different activation conditions and configuration information, achieving the effect of using different configuration information in different environments.
In the following example, we will use maven-antrun-The plugin:run target is added to the test phase. This allows us to output text information in different profiles. We will use pom.xml to define different profiles and activate profiles using Maven commands in the command console.
The pom.xml file is as follows:
<project xmlns="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"> <modelVersion>4.0.0</modelVersion> <groupId>com.jsoft.test</groupId> <artifactId>testproject</artifactId> <packaging>jar</packaging> <version>0.1-SNAPSHOT</version> <name>testproject</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <profiles> <profile> <id>test</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.8</version> <executions> <execution> <phase>test</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>Using env.test.properties</echo> <copy file="src/main/resources/env.test.properties" tofile="${project.build.outputDirectory}"/env.properties" overwrite="true"/> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> <profile> <id>normal</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.8</version> <executions> <execution> <phase>test</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>Using env.properties</echo> <copy file="src/main/resources/env.properties" tofile="${project.build.outputDirectory}"/env.properties" overwrite="true"/> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> <profile> <id>prod</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.8</version> <executions> <execution> <phase>test</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>Using env.prod.properties</echo> <copy file="src/main/resources/env.prod.properties" tofile="${project.build.outputDirectory}"/env.properties" overwrite="true"/> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles> </project>
Note:build configuration fileis adopted <profiles> nodes.
Description:three nodes were newly created above. <profiles>, among which <id> distinguishes between different <profiles> to execute different AntRun tasks; while the tasks of AntRun can be understood as, AntRun listens to the Maven lifecycle phase of test, when Maven executes test, it will not only send AntRun tasks, but also tasks inside for outputting text and copying files to the specified location; as for which AntRun task to execute, at this timebuild configuration fileplays a role in transmitting the specified information, for example, by entering the specified information through the command line parameters <id>.
Execute the command:
mvn test -Ptest
Tip: The first test is the Maven lifecycle phase, the 2 a test isbuild configuration fileThe specified <id> parameter is passed through -P can be used to transmit, of course, it can be prod or normal, which is defined by you<id>.
The running result is as follows:
It can be seen that the AntRun task was successfully triggered, and it corresponds.build configuration fileThe task with <id> test under
Test the other two commands again, the results are as follows:
open %USER_HOME%/.m2 directory under settings.xml file, among which %USER_HOME% representing the user's home directory. If the setting.xml file does not exist, it will be copied directly %M2_HOME%/conf/settings.xml to .m2 directory, among which %M2_HOME% representing the installation directory of Maven.
Configure the setting.xml file and add the <activeProfiles> attribute:
<settings xmlns="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"> ... <activeProfiles> <activeProfile>test</activeProfile> </activeProfiles> </settings>
Execute the command:
mvn test
Prompt 1:At this time, there is no need to use -Ptest to input parameters, the <activeprofile> in the above setting.xml file has already specified the test parameter instead.
Prompt 2:Similarly, it can be used in %M2_HOME%/conf/settings.xml The file is configured with the same effect.
Execution Result:
First, remove all values from the setting.xml file tested in the previous step.
Then add the <activation> node to the <profile> section with <id> test in the pom.xml file:
<project xmlns="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"> <modelVersion>4.0.0</modelVersion> <groupId>com.jsoft.test</groupId> <artifactId>testproject</artifactId> <packaging>jar</packaging> <version>0.1-SNAPSHOT</version> <name>testproject</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <profiles> <profile> <id>test</id> <activation> <property> <name>env</name> <value>test</value> </property> </activation> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.8</version> <executions> <execution> <phase>test</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>Using env.test.properties</echo> <copy file="src/main/resources/env.test.properties" tofile="${project.build.outputDirectory}"/env.properties" overwrite="true"/> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> <profile> <id>normal</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.8</version> <executions> <execution> <phase>test</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>Using env.properties</echo> <copy file="src/main/resources/env.properties" tofile="${project.build.outputDirectory}"/env.properties" overwrite="true"/> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> <profile> <id>prod</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.8</version> <executions> <execution> <phase>test</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>Using env.prod.properties</echo> <copy file="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
Prompt 1:Above uses -Pass the environment variable, where evn corresponds to the <name> value set just now, and test corresponds to <value>.
Prompt 2:in Windows 10 The system environment variables were tested above, but they did not take effect, so, only through -D Passing.
Execution Result:
The activation element includes the following operating system information. When the system is Windows XP, the test Profile will be triggered.
<profile> <id>test</id> <activation> <os> <name>Windows XP</name> <family>Windows</family> <arch>x86</arch> <version>5.1.2600</version> </os> </activation> </profile>
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 specifies the name of the Profile. Maven will display the results of the activated test Profile.
mvn test
Now use the activation element to include the following operating system information. When the target/generated-sources/axistools/wsdl2java/com/companyname/If the group is missing, the test Profile will be triggered.
<profile> <id>test</id> <activation> <file> <missing>target/generated-sources/axistools/wsdl2java/ com/companyname/group</missing> </file> </activation> </profile>
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 specifies the name of the Profile. Maven will display the results of the activated test Profile.
mvn test
Reference: https://www.cnblogs.com/EasonJim/p/6828743.html