Maven Study Notes (11): flexible Building

Source: Internet
Author: User
A typical project includes a development environment, a test environment, and a product environment. The database configurations of these environments are different. Therefore, when building a project, you must be able to identify the environment and use the correct configuration. Another scenario is that a project has developed a large number of integration tests, which are time-consuming and not suitable for running each time a project is built, therefore, we need a means to activate these integration tests at specific times. To support the flexibility of building, Maven has three built-in features: attribute, profile, and resource filtering. This chapter describes these features. Maven attributes Six attributes of Maven:Maven has six types of attributes: 1. built-in attributes: There are two common built-in attributes-$ {basedir} indicates the project root directory, that is, including pom. the Directory of the XML file; $ {version} indicates the project version.
2. Pom attributes: You can use these attributes to reference the values of corresponding elements in the POM file. For example, $ {project. artifactid} corresponds to the value of the <project> <artifactid> element. Common pom attributes include:
    • $ {Project. Build. sourcedirectory}: main source code directory of the project. The default value is src/main/Java /.
    • $ {Project. Build. testsourcedirectory}: directory of the project's test source code. The default value is src/test/Java /.
    • $ {Project. Build. Directory}: Project Build output directory. The default value is target /.
    • $ {Project. outputdirectory}: Main code compilation output directory of the project. The default value is target/classes /.
    • $ {Project. testoutputdirectory}: Project test code compilation output directory. The default value is target/test-classes /.
    • $ {Project. groupid}: groupid of the project.
    • $ {Project. artifactid}: artifactid of the project.
    • $ {Project. Version}: version of the project.
    • $ {Project. Build. finalname}: name of the project packaging output file. The default value is $ {project. artifactid}-$ {project. Version }.
These attributes correspond to a pom element. The default values of these attributes are defined in the super pom. 3. Custom Attributes: You can customize Maven attributes under the <Properties> element of POM. For example:
<project>    ...     <properties>          <my.prop>hello</my.prop>     </properties>     ...</project>     
Then, when $ {My. Prop} is used elsewhere in POM, It will be replaced with hello. 4. settings property: similar to the POM property, you can use settings. attribute reference at the beginning of settings. values of XML elements in XML files, such as commonly used $ {settings. localrepository} points to the address of the user's local repository. 5. java system attributes: All Java System attributes can be referenced using Maven attributes. For example, $ {user. Home} points to the user directory. You can use MVN help: System to view all Java System attributes. 6. environment variable attributes: All environment variables can be referenced using Maven attributes starting with Env. For example, $ {env. java_home} indicates the value of the java_home environment variable. You can use MVN help: System to view all Java System attributes.
Example:In a multi-module project, dependencies between modules are common. These modules usually use the same groupid and version. In this case, you can use the POM attribute.
<dependencies>     <dependency>          <groupId>${project.groupId}</groupId>          <artifactId>account-email</artifacId>          <version>${project.version}</version>     </denpendency>          <dependency>          <groupId>${project.groupId}</groupId>          <artifactId>account-persist</artifacId>          <version>${project.version}</version>     </denpendency>     </dependencies>
Generally, even the groupid and version of the sub-module are agreed in the aggregation module, so the groupid and version of these modules are completely consistent.
Differences in build environments:In different environments, the source code of the project should be constructed in different ways. For example, we may use a set of database configurations in the development environment, and the online environment may use another set of database configurations, which are similar, the cache configuration and RFC links for other applications may have different configurations in different production environments. Resource Filtering:To cope with environment changes, we need to extract the changed parts. For example, for database configuration, we generally add the database configuration file. properties under the src/main/resources directory. The driver classes, URLs, usernames, and passwords used to connect to the database may change. Therefore, replace them with Maven attributes:
Database. JDBC. driverclass =$ {dB. driver} database. JDBC. connectionurl =$ {dB. URL} database. JDBC. username =$ {dB. username} database. JDBC. password =$ {dB. password}
Now that Maven attributes are used, we need to define them. Here we need to use profile to wrap them up. for different project configurations, we should define the profile in pom.
<profiles>     <profile>          <id>dev</id>               <properties>               <db.driver>com.mysql.jdbc.Driver</db.driver>               <db.url>jdbc:mysql://192.168.1.100:3306/test</db.url>               <db.username>dev</db.username>               <db.password>dew-pwd</db.password>          </properties>     </profile></profiles>    
This profile is named Dev to distinguish the configuration in the development environment from that in other environments. By default, Maven attributes will only be parsed in POM. What we need now is that they will also be parsed in the resource file, therefore, we need to customize the maven-resources-plugin behavior so that it can filter Maven attributes when processing resource files, that is, enable resource filtering. Enable Filtering for the main resource directory:
<resources>     <resource>          <directory>${project.basedir}/src/main/resources</directory>          <filtering>true</filtering>     </resource></resouces>
Enable Filtering for test resources:
<testResources>     <testResource>          <directory>${project.basedir}/src/test/resources</directory>          <filtering>true</filtering>     </testResource></testResouces>
There can be more than one main resource directory and one test Resource Directory. MAVEN allows you to declare multiple resource directories and provides different filtering configurations for each resource directory.
<resources>     <resource>          <directory>${project.basedir}/src/main/resources</directory>          <filtering>true</filtering>     </resource>     <resource>          <directory>/src/main/sql</directory>          <filtering>false</filtering>     </resource></resouces>
Finally, we only need to activate the profile on the command line when using MVN. Maven can replace the attribute reference in the database configuration file with the profile attribute value during project construction. The command is as follows: $ MVN clean install-pdevThe-p Parameter of MVN indicates that a profile is activated on the command line. The ID is Dev profile. You can see in the processed resource file that all Maven attributes have been replaced.
Maven profile:The construction in different environments is likely to be different. A typical case is the database configuration. In addition, in some environments, you may need to configure the plug-in to perform some special operations, use dependencies of Special versions, or use a special component name. Maven introduces the concept of profile to facilitate the migration of building in various environments. Profile can be used to modify a subset of POM during construction, or add additional configuration elements. You can activate a profile in many ways to build a profile in different environments. Activate profile:1. You can use the MVN command line parameter-P and the profile ID to activate the profile. Multiple IDs are separated by commas. For example, the following command activates the dev-X and Dev-y profiles: $ MVN clean install-pdev-X, Dev-y2. the settings file is explicitly activated. If you want a profile to remain active by default, you can configure settings. the activeprofiles element of the XML file indicates that the configured profile is activated for all projects. The Code is as follows:
<settings>     ...     <activeProfiles>          <activeProfile>dev-x</activeProfile>     </activeProfiles></settings>
3. System attribute activation 4. Operating System Environment activation 5. Whether the file exists or not 6. By default, the following four methods of activation are not commonly used, so we will not introduce them much. Profile type:You can declare the profile in the following locations as needed:
  • Pom. xml: Obviously, the profile declared in POM. XML is only valid for the current project.
  • User settings. xml: The profile in. m2/settings. XML in the user directory is valid for all Maven projects of the user on this machine.
  • Global settings. xml: The profile in CONF/settings. xml under the maven installation directory is valid for all Maven projects on this machine.
In order not to affect other users and to upgrade Maven easily, you should configure settings. XML in the user scope to avoid modifying the global settings. xml file. For this reason, the profile is generally not added to the global settings. xml file. The pom elements that can be declared in different types of profiles are also different. Pom. the profile in XML can be used with POM. XML is submitted to the code repository together, installed to the local repository by Maven, and deployed to the remote Maven repository. In other words, you can ensure that the profile exists with a specific Pom. XML, so it can modify or add many pom elements, as shown below:
<project>     <repositories></repositories>     <pluginRepositories></pluginRepositories>     <distributionManagement></distributionManagement>     <dependencies></dependencies>     <dependencyManagement></denpendencyManagement>     <modules></modules>     <properties></properties>     <reporting></reporting>     <build>          <plugins></plugins>          <defaultGoal></defaultGoal>          <resources></resources>          <testResources></testResources>          <finalName></finalName>     </build></project>
Because settings. XML has nothing to do with a specific project, there are few profile elements that can be configured in it:
<project>     <repositories></repositories>     <pluginRepositories></pluginRepositories>     <properties></properties><project>

Maven Study Notes (11): flexible Building

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.