Maven Aggregation and Inheritance 5

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
1 <modules>2       <module> module </module>3       <module> module two </module>4       <module > Module Three </module>5 </modules>

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

1 <modules>2       <module> /hello</module>  3       <module>.. /hellofriend</module>        4       <module>.. /makefriends</module>5 </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
1 <parent>  2          <groupid>me.gacl.maven</groupid>3          <artifactid>parentproject </artifactid>4          <version>0.0.1-snapshot</version>5          <relativepath> ... /parentproject/pom.xml</relativepath>  6 </parent>
2.2. Defining attributes in Inheritance code

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

1 <properties>2     <project.build.sourceencoding>utf-8</project.build.sourceencoding>3     <junit.version>4.9</junit.version>4     <maven.version>0.0.1-snapshot</maven.version>5 </properties>

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

1 <dependency>2     <groupid>junit</groupid>3     <artifactId>junit</artifactId> 4     <version>${junit.version}</version>5     <scope>test</scope>6 </dependency >    
2.3. The parent module is managed with Dependencymanagement
1 <dependencyManagement> 2     <dependencies> 3     <dependency> 4         <groupid>junit</ Groupid> 5         <artifactId>junit</artifactId> 6         <version>${junit.version}</version > 7         <scope>test</scope> 8     </dependency>     9     <dependency>10             < Groupid>cn.itcast.maven</groupid>11             <artifactid>hellofriend</artifactid>12             < version>${maven.version}</version>13             <type>jar</type>14             <scope>compile </scope>15        </dependency>16      </dependencies>17 </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

 1 <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" 2 xsi : schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > 3 < Modelversion>4.0.0</modelversion> 4 5 <groupId>me.gacl.maven</groupId> 6 <artifactid>p Arent</artifactid> 7 <version>0.0.1-SNAPSHOT</version> 8 <packaging>pom</packaging> 9 <name>parent</name>11 <url>http://maven.apache.org</url>12 <!--Hel on the project Lo, hellofriend, makefriends these three modules are aggregated-->14 <modules>15 <module> /hello</module>16 <module> /hellofriend</module>17 <module> /makefriends</module>18 </modules>19 <!--defining properties-->21 <properties>22 &lt ;p roject.build.sourceencoding>utf-8</project.build.sourceencoding>23 < Junit.version>4.9</junit.version>24 <maven.version>0.0.1-snapshot</maven.version>25 < /properties>26 <!--using dependencymanagement for Jar package dependency management-->28 <dependencymanagement>29 <! --Configure JAR package dependent-->30 <dependencies>31 <dependency>32 <groupid>junit& Lt;/groupid>33 <artifactid>junit</artifactid>34 <!--access Junit.version Properties- ->35 <version>${junit.version}</version>36 <scope>test</scope>3 7 </dependency>38 <dependency>39 <groupid>me.gacl.maven</grou                 PID&GT;40 <artifactid>hello</artifactid>41 <!--access Maven.version Properties-->42             <version>${maven.version}</version>43 <scope>compile</scope>44 </dependency>45 <dependency>46 <groupid>me.gacl.maven</groupid>47 &lt ; artifactid>hellofriend</artifactid>48 <!--access Maven.version Properties-->49 <ver Sion>${maven.version}</version>50 </dependency>51 </dependencies>52 </depe ndencymanagement>53 </project>

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

 1 <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance "2 xsi:schemalocation=" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd "> 3 4 <m Odelversion>4.0.0</modelversion> 5 <artifactId>Hello</artifactId> 6 7 <!--inherit from the parent project Pom.xml Configuration-8 <parent> 9 <groupid>me.gacl.maven</groupid>10 <ARTIFAC Tid>parent</artifactid>11 <version>0.0.1-snapshot</version>12 <!--using relative paths--&G T;13 <relativepath> /parent/pom.xml</relativepath> </parent>15 <dependencies>17 <dependency> ; <groupid>junit</groupid>19 <artifactid>junit</artifactid>20 &L T;/dependency>21 </dependencies>22 </project> 

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

 1 <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" 2 xsi : schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > 3 < Modelversion>4.0.0</modelversion> 4 <artifactId>HelloFriend</artifactId> 5 <name>hellof Riend</name> 6 7 <!--inherit Pom.xml configuration from parent project--8 <parent> 9 <groupid>me.gacl . maven</groupid>10 <artifactid>parent</artifactid>11 <version>0.0.1-snapshot</v Ersion>12 <relativepath>             /parent/pom.xml</relativepath>13 </parent>14 <dependencies>15 <dependency>16 <!--the version number of JUnit to use is already indicated in the Pom.xml file configuration of the parent project, so if you add JUnit dependencies here, 17 can not specify &LT;VERSION&GT;&LT;/VERSION&GT , and <scope>test</scope> will inherit-->18 directly from the pom.xml of the parent project <groupid>junit</groupid>19            <artifactid>junit</artifactid>20 </dependency>21 <!--used h in the Hellofriend project Ello the class in the project, so the need to add a dependency on Hello.jar Hello.jar <version> and <scope> has already been identified in the parent project's Pom.xml file configuration for 23             This can also be omitted here not to write the-->25 <dependency>26 <groupid>me.gacl.maven</groupid>27 <artifactid>hello</artifactid>28 </dependency>29 </dependencies>30 </proje Ct>

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

 1 <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" 2 xsi : schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > 3 < Modelversion>4.0.0</modelversion> 4 <artifactId>MakeFriends</artifactId> 5 <!--inherit parent project Pom.xml Configuration in 6 <parent> 7 <groupId>me.gacl.maven</groupId> 8 <artifactid&gt ; parent</artifactid> 9 <version>0.0.1-snapshot</version>10 <relativepath>..         /parent/pom.xml</relativepath>11 </parent>12 <dependencies>13 <dependency>14 <!--the version number of JUnit to use is already indicated in the Pom.xml file configuration of the parent project, so when you add JUnit dependencies here, 15 can not specify <version></version> and &L             T;scope>test</scope&gt, which inherits-->16 directly from the pom.xml of the parent project <groupid>junit</groupid>17 <artifactId>junit</artifactId></dependency>19 <dependency>20 <!--the class in the Hellofriend project is used in the Makefriends project, you need to add the Llofriend.jar's reliance on Hellofriend.jar <version> and <scope> has already been indicated in the Pom.xml file configuration of the parent project, so it is also possible to save the 22 Slightly not write the-->24 <groupid>me.gacl.maven</groupid>25 <artifactid>hellofrie nd</artifactid>26 </dependency>27 </dependencies>28 </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 Aggregation and Inheritance 5

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.