From: bytes.
First, let's briefly introduce the maven profile. For a person, profile refers to the portrait and outline of a person. For example, after an account is registered for each person in a forum, you can set your profile, put a photo, and introduce it. What about Maven? The entire project has defined the project Object Model (POM), just as the Forum provides the default behavior function for everyone. What if I want to change the POM on my machine? In this case, you can use profile. The following is an example:
XML Code
- <Profiles>
- <Profile>
- <ID> jdk16 </ID>
- <Activation>
- <JDK> 1.6 </JDK>
- </Activation>
- <Modules>
- <Module> simple-script </module>
- </Modules>
- </Profile>
- </Profiles>
<profiles> <profile> <id>jdk16</id> <activation> <jdk>1.6</jdk> </activation> <modules> <module>simple-script</module> </modules> </profile> </profiles>
This profile indicates that when JDK 1.6 on the machine is used, the simple-script sub-module will be built. If it is 1.5 or 1.4, it will not be built, this profile is automatically activated by the environment.
We need to use the appropriate profile in the right place and activate it in the right way when appropriate. You cannot activate a Non-Public Profile on the build server, you cannot require developers to write complicated commands to use the regular profile. This section describes the activation methods of several profiles.
1. automatically activated according to the environment.
As in the previous example, when JDK 1.6 is used, Maven automatically constructs the simple-script module. In addition to JDK, we can also automatically activate the profile based on Operating System Parameters and Maven attributes, such:
XML Code
- <Profile>
- <ID> Dev </ID>
- <Activation>
- <Activebydefault> false </activebydefault>
- <JDK> 1.5 </JDK>
- <OS>
- <Name> Windows XP </Name>
- <Family> Windows </family>
- <Arch> x86 </arch>
- <Version> 5.1.2600 </version>
- </OS>
- <Property>
- <Name> mavenversion </Name>
- <Value> 2.0.5 </value>
- </Property>
- <File>
- <Exists> file2.properties </exists>
- <Missing> file1.properties </missing>
- </File>
- </Activation>
- ...
- </Profile>
<profile> <id>dev</id> <activation> <activeByDefault>false</activeByDefault> <jdk>1.5</jdk> <os> <name>Windows XP</name> <family>Windows</family> <arch>x86</arch> <version>5.1.2600</version> </os> <property> <name>mavenVersion</name> <value>2.0.5</value> </property> <file> <exists>file2.properties</exists> <missing>file1.properties</missing> </file> </activation> ... </profile>
2. Activate it using command line parameters.
This is the most direct and simple method. For example, if you define a profile named myprofile, you only need to enterMVN clean install-pmyprofileIt can be activated. The advantages of this method are obvious, but there is a major drawback. When there are many profiles, entering the-p parameter in the command line will be annoying. If you are tired of using this method all the time, consider using other automatic activation methods.
3. The configuration is automatically activated by default.
The method is simple. You can add an attribute When configuring the profile, for example:
XML Code
- <Profile>
- <ID> Dev </ID>
- <Activation>
- <Activebydefault> true </activebydefault>
- </Activation>
- ...
- </Profile>
<profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> ... </profile>
In a special environment, the profile automatically activated by default overwrites the default pom configuration, which is very simple and effective.
4. Configure the settings. xml file profile activation.
The settings. xml file can be found in ~ The/. m2 directory serves the custom behaviors of a user, or all users of the entire machine in the m2_home/conf directory. The configuration of the former will overwrite the latter. Similarly, settings. the XML-activated profile is intended to provide a specific environment configuration for the user or the entire machine. For example, you can configure a profile pointing to the local database URL on a machine and then use the settings of the machine. XML to activate it. The activation method is as follows:
XML Code
- <Settings>
- ...
- <Activeprofiles>
- <Activeprofile> local_db </activeprofile>
- </Activeprofiles>
- </Settings>
<settings> ... <activeProfiles> <activeProfile>local_db</activeProfile> </activeProfiles></settings>
The profile function provided by Maven is very powerful and flexible. If it is used well, it can effectively isolate many special configurations so that the entire project can be smoothly built in different environments. However, problems arising from the power and flexibility are relatively difficult to grasp. I hope this article will help Maven users.