About MAVEN (iii) Introduction to--profile

Source: Internet
Author: User
about MAVEN (iii) Introduction to--profile Blog Category:MAVEN Maven profile activates active 4 Profile 4.1 Profile

Profile allows us to define a series of configuration information and then specify its activation criteria. This allows us to define multiple profiles, and then each profile corresponds to different activation conditions and configuration information to achieve the effect of using different configuration information in different environments. For example We can use a set of configuration information through the profile definition above jdk1.5, use another set of configuration information under jdk1.5, or sometimes we can use different configuration information through the different operating system, such as a set of information under Windows, Linux is another set of information, and so on. The specific activation conditions I will refer to in the following article. definition position of 4.2 profile

For the use of MAVEN3, we can have more than one place to define the profile. Where the definition is different, its scope is different.

(1) The profile configuration for a particular project can be defined in the pom.xml of the project.

(2) For a specific user profile configuration, we can define the profile in the user's settings.xml file. The file is under the ". M2" directory in the user's home directory.

(3) Global profile configuration. The global profile is defined in the "Conf/settings.xml" file in the Maven installation directory. information that can be defined in 4.3 profile

The configuration information that can be defined in profile is related to the location where the profiles are located. The following are discussed in two cases, one is defined in Settings.xml and the other is defined in Pom.xml. 4.3.1 profile defined in Settings.xml

When a profile is defined in settings.xml, it means that it is global, and it works for all projects or for all projects of a user. Because it is global, you can define only a relatively broad range of configuration information, such as remote repositories, in Settings.xml. And some of the more detailed needs to be defined according to the project's different needs to be defined in the project's Pom.xml. Specifically, the information that can be defined in Settings.xml is <repositories>, <pluginRepositories>, and <properties>. Key-value pairs defined in <properties> can be used in pom.xml. 4.3.2 Profile defined in Pom.xml

Profiles defined in Pom.xml can define more information. There are mainly the following:

L <repositories>

L <pluginRepositories>

L <dependencies>

L <plugins>

L <properties>

L <dependencyManagement>

L <distributionManagement>

L also have the following sub-elements of the build element, which mainly include:

<defaultGoal>

<resources>

<testResources>

<finalName> How 4.4 profile is activated

Maven gives us a number of different ways to activate the profile. For example, we can activate a profile using the-p parameter, or it can be activated automatically, depending on the setting of the ambient condition. Each of them is described below: 4.4.1 activation with Activebydefault settings

Let's look at one of the following configuration XML codes    <profiles>           < profile>                <id >profileTest1</id>                 <properties>                        

We can specify the activation condition in the activation element in the profile, when no condition is specified, and then specify that Activebydefault is true to indicate that the profile is activated by default when no other profile is specified as active. So when we call the MVN package the above profileTest1 will be activated, but when we use MVN package–p profileTest2 the profileTest2 will be activated, And this time profileTest1 will not be activated. 4.4.2 using Activeprofiles in settings.xml to specify the profile in the active state

We can use Activeprofiles in settings.xml to specify the profile that needs to be activated, which will be activated in all cases. For example, now we have defined the following two profile XML codes    <profiles>           <profile>               <id> profiletest1</id>                <properties>                       

The profile here can be defined in settings.xml, or it can be defined in Pom.xml. This time if we need to specify ProfileTest1 as active, then we can define the Activeprofiles in Settings.xml, which is defined as follows: XML code <activeProfiles> <ACTI Veprofile>profiletest1</activeprofile> </activeProfiles>

Consider a situation in which we define several profiles that need to be activated at the same time under Activeprofiles. We also use the above profile definition for example, we define the simultaneous activation of ProfileTest1 and ProfileTest2. XML code <activeProfiles> <activeProfile>profileTest1</activeProfile> <activeProfile> Profiletest2</activeprofile> </activeProfiles>

        from ProfileTest1 and profileTest2 we can see that they collectively define the property hello. So when I use the property hello in Pom.xml, what value does it take? is based on the order defined by the Activeprofile, followed by the previous overwrite. According to my test, the answer is no, it is overridden according to the order of the profile definition, and then the definition later overrides the one defined earlier. 4.4.3  activates a profile using the-p parameter

Suppose we now have the Profiles XML code defined below    <profiles>           <profile>                  <id>profileTest1</id>                  <properties>                         

So when we do maven, we can use the-p parameter to specify which profile is currently active. For example, we need to use the ID ProfileTest1 profile when we package the project, we can do this: the cmd code mvn package–p profileTest1

When we use Activebydefault or settings.xml to define an active profile, but when we do something we don't want it to be active, we can do this: the cmd code MVN package–p! ProfileTest1

This assumes that ProfileTest1 is the active profile using the activeprofile tag in settings.xml, then when we use "-P!profile" Indicates that the profile will not be active in the current operation. 4.4.4 Activate profile according to the environment

A very important feature of profile is that it can be activated according to different environments, such as activating different profiles depending on the operating system, or activating different profiles depending on the JDK version, and so on. 4.4.4.1 Activate profile XML code <profiles> <profile> <ID>PROFILETEST1&L based on JDK t;/id> <jdk>1.5</jdk> </profile> <profiles>

The above indicates that the PROFILETEST1 is activated when the JDK is in the 1.5 release series. XML code <profiles> <profile> <id>profileTest1</id> <JD k>[1.4,1.7) </jdk> </profile> <profiles>

        above indicates that the PROFILETEST1 is activated when the JDK is 1.4, 1.5, and 1.6. 4.4.4.2 Activate Profile according to the operating system XML code    <profiles>           <profile>                <id>profileTest1</id>                <activation>                  <os>                       <name>Windows XP</name>    &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NB

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.