) Several ways to activate Maven Profile

Source: Internet
Author: User

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

  1. <Profiles>
  2. <Profile>
  3. <ID> jdk16 </ID>
  4. <Activation>
  5. <JDK> 1.6 </JDK>
  6. </Activation>
  7. <Modules>
  8. <Module> simple-script </module>
  9. </Modules>
  10. </Profile>
  11. </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

  1. <Profile>
  2. <ID> Dev </ID>
  3. <Activation>
  4. <Activebydefault> false </activebydefault>
  5. <JDK> 1.5 </JDK>
  6. <OS>
  7. <Name> Windows XP </Name>
  8. <Family> Windows </family>
  9. <Arch> x86 </arch>
  10. <Version> 5.1.2600 </version>
  11. </OS>
  12. <Property>
  13. <Name> mavenversion </Name>
  14. <Value> 2.0.5 </value>
  15. </Property>
  16. <File>
  17. <Exists> file2.properties </exists>
  18. <Missing> file1.properties </missing>
  19. </File>
  20. </Activation>
  21. ...
  22. </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

  1. <Profile>
  2. <ID> Dev </ID>
  3. <Activation>
  4. <Activebydefault> true </activebydefault>
  5. </Activation>
  6. ...
  7. </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

  1. <Settings>
  2. ...
  3. <Activeprofiles>
  4. <Activeprofile> local_db </activeprofile>
  5. </Activeprofiles>
  6. </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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.