The question is raised:
When a project relies on multiple other projects or modules, the A module references the JUnit jar,b module and the JUnit jar, which relies on the A and B modules, so there is no need to repeatedly add junit jar coordinate dependencies. At this point, we can separate a project to manage the jar's coordinate dependencies, which is the dependency inheritance below.
Inherited
There are inheritance in object-oriented thinking, and subclasses inherit methods and properties that are not private to the parent class.
in Maven, we can extract all the common jar coordinates and then build a parent project that contains only one pom.xml file, and the parent project is packaged in Pom. Add other modules to the parent module so that you can run the parent module with other modules, using tags <modules></modules> implementations with other modules. As shown in the following code:
<span style= "FONT-SIZE:14PX;" ><span style= "Font-family:microsoft yahei;font-size:14px;" ><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>org.summer.user</groupId> <artifactId> User-parent</artifactid> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging > <modules> <module> /user-core</module> <module> /user-dao</module> <module> /user-log</module> <module> /user-service</module> </modules> <url>http://maven.apache.org</url> <properties> &L T;project.build.sourceencoding>utf-8</project.build.sourceencoding> <junit.version>4.10</ Junit.version> </properties> <DEPENDENCIES> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version >${junit.version}</version> <scope>test</scope> </dependency> <dependency> < Groupid>${project.groupid}</groupid> <artifactId>user-core</artifactId> <version>${ project.version}</version> </dependency> <dependency> <groupId>${project.groupId}< /groupid> <artifactId>user-dao</artifactId> <version>${project.version}</version> < type>jar</type> <scope>compile</scope> </dependency> <dependency> <gro Upid>${project.groupid}</groupid> <artifactId>user-log</artifactId> <version>${ project.version}</version> <type>jar</type> <scope>compile</scope> <EXCLUSIONS&G T <exclusion> <groupid>log4j</groupid> <artifactId>log4j</artifactId> </exclusion> </exclusions> </depe Ndency> <dependency><groupid>org.hibernate</groupid><artifactid>hibernate-core</ Artifactid><version>3.6.10.final</version></dependency><dependency><groupid> commons-logging</groupid><artifactid>commons-logging</artifactid><version>1.1.1</ Version></dependency><dependency><groupid>org.ow2.orchestra.eclipse.birt</groupid> <artifactid>org.ow2.orchestra.eclipse.birt.chart.engine</artifactid><version>3.7.0</ Version></dependency><dependency><groupid>maven</groupid><artifactid> Maven-abbot-plugin</artifactid><version>1.1</version></dependency><dependency> <groupid>mockobjects</groupid><artifactid>alt-jdk1.3</artifactid><version>0.07 </versioN></dependency><dependency> <groupId>org.springframework</groupId> <artifactId> Spring-web</artifactid> <version>3.1.1.RELEASE</version></dependency> </ Dependencies></project></span></span>
where references to other modules are in,<module></module>: /user-dao indicates that the parent module and the submodule are at the same level.
The Pom.xml of the submodule can inherit all the jar coordinates of the parent class simply by adding <parent></parent> nodes, and the Submodule Pom.xml code is as follows:
<span style= "FONT-SIZE:14PX;" ><span style= "Font-family:microsoft yahei;font-size:14px;" ><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> <parent> <groupId>org.summer.user</groupId> < Artifactid>user-parent</artifactid><version>0.0.1-snapshot</version><relativepath>, .... /user-parent/pom.xml</relativepath> </parent> <artifactId>user-dao</artifactId> <name >user-dao</name> <dependencies> <dependency> <groupId>junit</groupId> < artifactid>junit</artifactid> </dependency> <dependency> <groupId>org.konghao.user< /groupid> <artifactId>user-core</artifactId> </dependency> <dependency> <groupId>org.konghao.user</groupId> <artifactId>user-log</artifactId> </dependenc Y> </dependencies></project></span></span>
</pre><span style= "font-family: ' Microsoft Yahei ';" ><span style= "FONT-SIZE:14PX;" ></span></span><p><span style= "font-family: ' Microsoft Yahei ';" ><span style= "FONT-SIZE:14PX;" ><span style= "font-family: ' Microsoft Yahei '; font-size:14px;" > </span><relativepath>: The relative path of the parent module Pom.xml. Because the Submodule User-dao inherits the parent module User-parent, User-dao owns the entire jar of the parent module. If the module User-log also inherits the User-parent parent module, and User-log need to add additional jar packages, according to the principle of inheritance, it is necessary to continue to add the jar in the parent module, which results in redundancy of the parent module. We solve this problem by means of aggregation of modules. Aggregate we add the <dependencyManagement></dependencyManagement> node in the Pom.xml file in the parent module, as shown in the following code: </span></ Span></p><p><span style= "FONT-SIZE:14PX;" ><span style= "font-family: ' Microsoft Yahei ';" ></span></span></p><pre name= "code" class= "HTML" ><span style= "FONT-SIZE:14PX;" ><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>org.summer.user</groupId> <artifactId>user-parent</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <modules> <module A. /user-core</module> <module> /user-dao</module> <module> /user-log</module> <module> /user-service</module> </modules> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <junit.version>4.10</ Junit.version> <mysql.driver>com.mysql.jdbc.Driver</mysql.driver> <mysql.url>jdbc:mysql:// Localhost:3306/mysql</mysql.url> <mysql.username>root</mysql.username> <mysql.password> 123456</mysql.password> </properties> <DEPendencymanagement> <dependencies> <dependency> <groupId>junit</groupId> <artifactid >junit</artifactId> <version>${junit.version}</version> <scope>test</scope> < /dependency> <dependency> <groupId>${project.groupId}</groupId> <artifactid>user-cor e</artifactid> <version>${project.version}</version> </dependency> <dependency> <groupId>${project.groupId}</groupId> <artifactId>user-dao</artifactId> <version>$ {project.version}</version> <type>jar</type> <scope>compile</scope> </dependency > <dependency> <groupId>${project.groupId}</groupId> <artifactid>user-log</arti factid> <version>${project.version}</version> <type>jar</type> <scope>compile< ;/scope> <exclusions> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusio n> </exclusions> </dependency> <dependency><groupId>org.hibernate</groupId> <artifactid>hibernate-core</artifactid><version>3.6.10.final</version></dependency ><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId>< version>1.2.16</version></dependency><dependency><groupid>mysql</groupid>< Artifactid>mysql-connector-java</artifactid><version>5.1.18</version></dependency> <dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId> <version>1.6.4</version></dependency><dependency><groupid>javassist</groupid ><artifactid>javassist</artifactid><version>3.12.1.ga</version></dependency><dependency><groupid>commons-logging</groupid><artifactid>commons-logging</ Artifactid><version>1.1.1</version></dependency><dependency><groupid> Org.ow2.orchestra.eclipse.birt</groupid><artifactid>org.ow2.orchestra.eclipse.birt.chart.engine </artifactId><version>3.7.0</version></dependency><dependency><groupId> Maven</groupid><artifactid>maven-abbot-plugin</artifactid><version>1.1</version> </dependency><dependency><groupid>mockobjects</groupid><artifactid>alt-jdk1.3 </artifactId><version>0.07</version></dependency><dependency> <groupId> Org.springframework</groupid> <artifactId>spring-web</artifactId> <version>3.1.1.release </version></dependency> </dependencies> </dependencyManagement> </project></span >
a sub-module can selectively select a jar in the parent module. and remove the duplicated parts, such as version, the Submodule Pom.xml as shown in the following code:
<span style= "FONT-SIZE:14PX;" ><span style= "Font-family:microsoft yahei;font-size:14px;" ><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> <parent> <groupId>org.summer.user</groupId> < Artifactid>user-parent</artifactid><version>0.0.1-snapshot</version><relativepath>, .... /user-parent/pom.xml</relativepath> </parent> <artifactId>user-core</artifactId> <name >user-core</name> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> <dependency><groupid>org.hibernate< ;/GROUPID><ARTIFACTID>HIBERNATE-CORE</ARTIFACTID></DEPENDENCY><DEPENDENCY≫<groupid>log4j</groupid><artifactid>log4j</artifactid></dependency>< Dependency><groupid>mysql</groupid><artifactid>mysql-connector-java</artifactid> </dependency><dependency><groupid>org.slf4j</groupid><artifactid>slf4j-log4j12 </artifactid></dependency><dependency><groupid>javassist</groupid><artifactid >javassist</artifactId></dependency> </dependencies> </project></span></span >
This enables "decoupling" from the parent module.
Next: Maven conventions and common commands
Maven in Action (v) Maven inheritance and aggregation