Maven Learning Summary (v)--Aggregation and inheritance

Source: Internet
Author: User

First, aggregation

If we want to build multiple project modules at once, we need to aggregate multiple project modules

1.1. Aggregation Configuration Code
<modules>       <module> Module one </module>       <module> module two </module>       <module> Module III </module> </modules>

For example: The three modules for the Hello, hellofriend, makefriends of the project are aggregated

<modules>       <module> ... /hello</module>         <module> /hellofriend</module>               <module> /makefriends</module> </modules>                                                                                                                               

  

Where module's path is a relative path.

Ii. inheritance

Inheritance in order to eliminate duplication, we have many of the same configuration extracted, for example: grouptid,version, etc.

2.1. Inherit the configuration code
<parent>           <groupId>me.gacl.maven</groupId>         <artifactid>parentproject</ artifactid>         <version>0.0.1-SNAPSHOT</version>         <relativepath> ... /parentproject/pom.xml</relativepath>  </parent>
2.2. Defining attributes in Inheritance code

In the process of inheriting code, you can define properties such as:

<properties>     <project.build.sourceEncoding>UTF-</project.build.sourceEncoding>     < junit.version>4.9</junit.version>     <maven.version>0.0.1-SNAPSHOT</maven.version> </ Properties>

The property is accessed by ${junit.version}, for example:

<dependency>    <groupId>junit</groupId>    <artifactId>junit</artifactId>    <version>${junit.version}</version>    <scope>test</scope></dependency>
2.3. The parent module is managed with Dependencymanagement
<dependencyManagement>    <dependencies>    <dependency>        <groupid>junit</ groupid>        <artifactId>junit</artifactId>        <version>${junit.version}</version>        <scope>test</scope>    </dependency>        <dependency>            <groupId> cn.itcast.maven</groupid>            <artifactId>HelloFriend</artifactId>            <version>${ maven.version}</version>            <type>jar</type>            <scope>compile</scope>       </dependency>     </dependencies></dependencyManagement>

The advantage of this is that the submodule can have the inheritance of the selection row, and not all inheritance.

Iii. the relationship between aggregation and inheritance

Aggregations are primarily designed to build projects quickly, inheriting primarily to eliminate duplication

Iv. Aggregation and succession practical exercises

Create four MAVEN projects as shown in:

These four items are placed in the same directory for easy aggregation and inheritance later

  

The parent project is the parents of the other three projects, mainly for configuring some public configuration, and the other three projects inherit the configuration in the parent project, first configuring the parent project's pom.xml, adding the Hello, hellofriend, Makefriends these three modules for aggregation and jar dependency, the Pom.xml configuration information is as follows:

Pom.xml configuration of the parent project

<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/xsd/maven-4.0.0.xsd" > <modelversion>4.0.0 </modelVersion> <groupId>me.gacl.maven</groupId> <artifactId>Parent</artifactId> &L T;version>0.0.1-snapshot</version> <packaging>pom</packaging> <name>parent</name > <url>http://maven.apache.org</url>        <!--the three modules of Hello, hellofriend, makefriends for the project--<modules> <module&gt ... /hello</module> <module> /hellofriend</module> <module> /makefriends</module> </modules> <!--define Properties--<properties> <project.build.        Sourceencoding>utf-8</project.build.sourceencoding> <junit.version>4.9</junit.version> <maven.version>0.0.1-SNAPSHOT</maven.version> </properties> <!-- Using Dependencymanagement for Jar package Dependency Management-<dependencyManagement> <!--configuring Jar pack Dependencies--<dependenc ies> <dependency> <groupId>junit</groupId> <artifactid&gt ;junit</artifactid> <!--Access Junit.version Properties--&LT;VERSION&GT;${JUNIT.VERSION}&L  T;/version> <scope>test</scope> </dependency>          <dependency> <groupId>me.gacl.maven</groupId> <artifactId> hello</artifactid> <!--Access Maven.version Properties--<version>${maven.version}&lt ;/version> <scope>compile</scope> </dependency> &LT;DEPENDENCY&G                T                <groupId>me.gacl.maven</groupId> <artifactId>HelloFriend</artifactId> <!--Access Maven.version Properties--<version>${maven.version}</version> </dependen Cy> </dependencies> </dependencyManagement></project>

Inherit the Pom.xml configuration of the parent project in the pom.xml of the Hello Project

<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/xsd/maven-4.0.0.xsd" >    <modelVersion>4.0.0</modelVersion>  <artifactId>Hello</artifactId>        <!-- Inherit the Pom.xml configuration in the parent project-       <parent>            <groupId>me.gacl.maven</groupId>         < artifactid>parent</artifactid>          <version>0.0.1-SNAPSHOT</version>          <!--using relative paths-- >        <relativepath> ... /parent/pom.xml</relativepath>      </parent>        <dependencies>        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>        </ Dependency>    </dependencies></project>

Inherit the Pom.xml configuration of the parent project in the Pom.xml of the Hellofriend project

<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/xsd/maven-4.0.0.xsd" > <modelversion>4.0.0 </modelVersion> <artifactId>HelloFriend</artifactId> <name>HelloFriend</name> & lt;! --Inherit pom.xml configuration from parent project--<parent> <groupId>me.gacl.maven</groupId> <artifacti D>parent</artifactid> <version>0.0.1-SNAPSHOT</version> <relativepath&gt ... /parent/pom.xml</relativepath> </parent> <dependencies> <dependency> <! --The version number of JUnit that you want to use is already indicated in the Pom.xml file configuration of the parent project, so you can add junit dependencies here without specifying<version></version> and <scope>test</scope&gt, which are inherited directly from the pom.xml of the parent project--<groupid>ju Nit</groupid> <artifactId>junit</artifactId> </dependency> <!--The class in the Hello project is used in the Hellofriend project, so you need to add a dependency hello.jar to the Hello.jar<version> and <scope>It has already been indicated in the Pom.xml file configuration of the parent project, so you can omit it here too.-<dependency> <groupId>me.gacl.maven</groupId> <artifactid>hell O</artifactid> </dependency> </dependencies></project>

Inherit the Pom.xml configuration of the parent project in the Pom.xml of the Makefriends project

<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/xsd/maven-4.0.0.xsd" > <modelversion>4.0.0 </modelVersion> <artifactId>MakeFriends</artifactId> <!--inherit pom.xml configuration from parent project--<p arent> <groupId>me.gacl.maven</groupId> <artifactId>Parent</artifactId> &L T;version>0.0.1-snapshot</version> <relativepath> /parent/pom.xml</relativepath> </parent> <dependencies> <dependency> <!--The version number of JUnit that you want to use is already indicated in the Pom.xml file configuration of the parent project, so you can add junit dependencies here without specifying<version></version> and <scope>test</scope&gt, which are inherited directly from the pom.xml of the parent project--<groupid>ju nit</groupid> <artifactId>junit</artifactId> </dependency> <dependency > <!--The classes in the Hellofriend project are used in the Makefriends project, so you need to add a dependency hellofriend.jar for Hellofriend.jar<version> and <scope>It has already been indicated in the Pom.xml file configuration of the parent project, so you can omit it here too.-<groupId>me.gacl.maven</groupId> <artifactId>HelloFriend</artifactId> </dependency> </dependencies></project>

Pom.xml of the above four projects have been configured so that the three sub-items (submodules) of Hello, hellofriend, makefriends are aggregated in the parent project, while the Hello, Hellofriend, Makefriends these three sub-projects (submodules) also inherit the public configuration in the parent project so that all projects can be built using Maven once, as shown in:

  

Select the parent project's pom.xml file → "Run as" → "maven install" so that Maven builds the four projects of parent, Hello, Hellofriend, makefriends at once, as shown in:

  

Maven Learning Summary (v)--Aggregation and inheritance

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.