07. Inheritance and Aggregation

Source: Internet
Author: User


Inheritance and Aggregation aggregation

The so-called aggregation, as the name implies, is the aggregation of multiple modules or projects together, a command to build multiple modules.

Why aggregation?

With the rapid development of technology and the requirements of various types of users more and more high, the software itself has become more and more complex, software designers began to use a variety of ways to develop, so there is our layered architecture, sub-module development, to improve code clarity and reuse.

For this feature, MAVEN also gives the appropriate configuration.

Demo

There are project demo below with a, B two modules:

MVN archetype:generate-dgroupid=org.demo-dartifactid=demoa-dversion=1.0.0-snapshot-darchetypeartifactid= MAVEN-ARCHETYPE-QUICKSTART-DINTERACTIVEMODE=FALSEMVN Archetype:generate-dgroupid=org.demo-dartifactid=demob- Dversion=1.0.0-snapshot-darchetypeartifactid=maven-archetype-quickstart-dinteractivemode=false

Create a new demo directory with the following structure:

├─demoa│  └─src│      ├─main│      │   └─java│      │      └─org│       │          └─demo│       └─test│          └─java│               └─org│                   └─demo└─demoB     └─src        ├─main        │   └─java        │      └─org         │           └─demo        └─test             └─java                └─org                      └─demo

Writing Demo/pom.xml

<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/maven-v4_0_0.xsd" > <modelversion  >4.0.0</modelVersion> <groupId>org.demo</groupId> <artifactId>Demo</artifactId> <packaging>pom</packaging> <version>1.0.0-SNAPSHOT</version> <name>demo</name          > <url>http://maven.apache.org</url> <modules> <module>demoA</module> <module>demoB</module> </modules></project>

Note The packaging node value in the file is Pom, which means that the POM is an aggregate pom,modules node that contains a list of Submodules, and the module node defines the relative path of the submodule.

To facilitate construction, the aggregation module is usually placed at the top level of the project directory layer, and other aggregation modules exist as subdirectories.

At this point, mvn clean package both module A and Module B will be built when executed in the demo directory.

650) this.width=650; "title=" Polymerization_01.png "src=" http://s5.51cto.com/wyfs02/M02/80/01/ Wkiol1c0nwatzw-yaaaa_4fo16m602.png "alt=" Wkiol1c0nwatzw-yaaaa_4fo16m602.png "/>

the content of the aggregation module is only a pom.xml file , it does not contain directories such as Src/main/java, Src/test/java, because it is only used to help other modules to build the tool, itself does not have the actual content.

Why do inheritance inherit?

Inheritance is designed to avoid duplication and simplify configuration, and it has the added benefit of making the project more secure

Demo

In the example above, both Demoa and demob use junit dependencies, adding a node description to their POM configuration dependencies .

Modify Demo/pom.xml

<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/ Maven-v4_0_0.xsd ">    <modelVersion>4.0.0</modelVersion>     <groupId>org.demo</groupId>    <artifactId>Demo</artifactId>     <packaging>pom</packaging>    <version>1.0.0- Snapshot</version>    <name>demo</name>    <url >http://maven.apache.org</url>    <modules>         <module>demoa</module>        <module >demoB</module>    </modules>    <dependencies>         <dependency>            < groupid>junit</groupid>            < artifactid>junit</artifactid>             <version>3.8.1</version>             <scope>test</scope>        </dependency>     </dependencies></project>

Modify Demo/demoa/pom.xml

<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/ Maven-v4_0_0.xsd ">    <modelVersion>4.0.0</modelVersion>     <artifactId>demoA</artifactId>    <packaging>jar</packaging>     <name>demoa</name>    <url>http:// maven.apache.org</url>    <parent>         <groupid>org.demo</groupid>        <artifactid >demo</artifactid>        <version>1.0.0-snapshot</ Version>        <relativepath&gt, .... /pom.xml</relativepath>    </parent></pRoject> 

Modify Demo/demob/pom.xml

<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/ Maven-v4_0_0.xsd ">    <modelVersion>4.0.0</modelVersion>     <artifactId>demoB</artifactId>    <packaging>jar</packaging>     <name>demob</name>    <url>http:// maven.apache.org</url>    <parent>         <groupid>org.demo</groupid>        <artifactid >demo</artifactid>        <version>1.0.0-snapshot</ Version>        <relativepath&gt, .... /pom.xml</relativepath>    </parent></pRoject> 

The sub-module parent declares the parent module, under the child node, with the element, which specifies the coordinates of the parent groupId artifactId version parent module, which are required by the three elements. Node relativePath Specifies the path of the parent module POM, the default value is: /pom.xml

The POM elements that can be inherited:
    • GROUPID: Project ID, core element of project coordinates

    • Version: Project versions, core elements of project coordinates

    • Description: Project description information

    • Organization: Project organization Information

    • Inceptionyear: Project Creation Date

    • Developers: Project developer Information

    • Contributors: Project Contributor Information

    • Distributionmanagement: Project Deployment Configuration

    • SCM: Version control information for projects

    • MailingLists: Project Mailing list information

    • Properties: Custom Attributes

    • Dependencies: Dependency configuration of the project

    • Dependencymanagement: Dependency Management configuration for projects

    • Repositories: Warehouse configuration for projects

    • Build: Project source directory configuration. Output directory configuration, plug-in configuration, and so on.

MAVEN's dependency management

As can be seen from the above: dependencies configuration can be inherited, the project's common dependency elements can be transferred to the parent, which simplifies the configuration. But the problem also comes, if there is a new module, but this module does not need the dependency of these parent, how to deal with it?

Maven's dependency management (dependencymanagement) is a solution to this problem.

dependencymanagement : The element configured in Dependencymanagement does not introduce a dependency on the parent, nor does it introduce dependencies to its submodule, but simply its configuration is inheritable

Demo:

Parent Module Add Declaration:

<dependencyManagement> <dependencies> <dependency> <groupid>yourgroupid</g Roupid> <artifactId>yourartifactId</artifactId> <version>${target.version}</ Version> </dependency> </dependencies></dependencyManagement>

The Pom of the submodule inherits these configurations:

<dependencies> <dependency> <groupId>yourgroupId</groupId> <artifactid>your Artifactid</artifactid> </dependency></dependencies>

When the submodule inherits these configurations, it still declares GroupID and Artifactid, which means that the current configuration is inherited from the parent Pom and thus directly uses the corresponding version of the parent POM component.

The components used in dependencyManagement the parent module will only be valid if the child module is configured with inherited elements, otherwise maven will not load the elements declared in the parent module.

This can effectively prevent multiple sub-modules from using dependent version inconsistencies, helping to reduce the chance of dependency collisions.

MAVEN's plugin Management

Plug-ins are also frequently introduced in our project development process, so the way to solve these complex configurations is to use plug-in management

The parent module can be added pluginManagement to manage plug-ins. This element is dependencyManagement similar to the same.

Demo:

<pluginManagement>    <plugins>         <plugin>            <groupId> </groupId>            <artifactId>< /artifactid>            <version></ version>            <executions>                 <execution>                      <id></id>                     <goals>                         <goal></goal>                     </goals>                      <phase></phase>                     <configuration>                          <source></source>                         <target></target>                      </configuration>&nbsP;               </execution>             </executions>         </plugin>    </plugins></pluginManagement>


This article is from the "Happiness Password" blog, please be sure to keep this source http://programmersky.blog.51cto.com/5326405/1772697

07. Inheritance and Aggregation

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.