问题的提出:
当一个项目依赖多个其他项目或者模块的时候,可以A模块引用了junit jar,B模块也引用了junit jar,而这个项目依赖于A、B模块,这样就没有必要重复添加junit jar坐标依赖。这时候,我们可以单独的一个项目来管理jar的坐标依赖,也就是下面要说的依赖的继承。
继承
面向对象思想中有继承,子类继承父类不是私有的方法和属性。
在maven中的继承,我们可以抽取出所有公共的jar坐标,然后建立一个parent的项目,该项目中仅包含有一个pom.xml文件,并且parent项目的打包方式为pom。在父模块中添加上其他模块,以便运行父模块的时候可以把其他模块一起运行,使用标签<modules></modules>实现与其他模块的结合。如下代码所示:
<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> <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> <groupId>${project.groupId}</groupId> <artifactId>user-log</artifactId> <version>${project.version}</version> <type>jar</type> <scope>compile</scope> <exclusions> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> </exclusions> </dependency> <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>
其中对其他模块的引用,<module></module>中../user-dao 表示父模块和子模块处在同一级别。
而子模块的pom.xml 只需要添加<parent></parent>节点就可以继承父类的全部jar坐标,子模块pom.xml代码如下所示:
<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> </dependency> </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>:父模块pom.xml的相对路径。因为子模块user-dao继承了父模块user-parent,所以user-dao拥有父模块全部的jar。如果模块user-log也同样继承了user-parent父模块,且user-log中需要添加其他的jar包,根据继承的原则,就需要在父模块中继续添加jar,这样造成了父模块的冗余。我们通过模块的聚合方式来解决这个问题。聚合我们在父模块中的pom.xml文件中添加<dependencyManagement></dependencyManagement>节点,如下代码是所示:</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>../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-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> <groupId>${project.groupId}</groupId> <artifactId>user-log</artifactId> <version>${project.version}</version> <type>jar</type> <scope>compile</scope> <exclusions> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> </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>
子模块可以有选择性的选择父模块中的jar。并去掉重复的部分,例如version,子模块pom.xml如下代码所示:
<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>
这样就实现了与父模块之间的“解耦”。
下一篇:maven约定和常用命令
时间: 2024-11-10 20:45:59