Maven继承

时间:2024-05-02 01:42:04

继承为了消除重复,可以把pom 中很多相同的配置提取出来;如:grouptId, version 等。

在使用的时候子工程直接继承父工程的依赖版本号,子工程中不再需要指定具体版本号,方便统一管控项目的依赖版本问题。

创建一个父工程,

Maven继承

父工程的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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.sm1234</groupId>
<artifactId>sm1234-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<description>这是一个父工程,锁定版本</description> <!-- 抽取相同的属性,如版本号,集中定义版本依赖号 -->
<properties>
<!-- 名称需要自定义 -->
<servlet.version>2.5</servlet.version>
<spring.version>5.1.3.RELEASE</spring.version>
</properties> <!-- 抽取子工程的依赖配置 -->
<!-- 版本锁定,当子工程有需要并且自行添加了具体依赖后才有效 -->
<dependencyManagement>
<dependencies>
<!-- 依赖servlet的api -->
<!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>${servlet.version}</version>
<!-- privided依赖范围,编译时有效,但是运行发布的时候是无效的。 -->
<scope>provided</scope>
</dependency> <!-- 依赖spring框架 -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency> </dependencies>
</dependencyManagement> </project>

子工程的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/xsd/maven-4.0.0.xsd"> <!-- 引用父工程 -->
<parent>
<groupId>cn.sm1234</groupId>
<artifactId>sm1234-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 父项目的pom.xml文件的相对路径,一般可不指定 -->
<!-- <relativePath>../parent</relativePath> -->
</parent> <modelVersion>4.0.0</modelVersion>
<artifactId>sm1234-web</artifactId>
<packaging>war</packaging> <dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<!-- 继承父工程后,版本号由父工程里面统一指定,不再需要特别指定 -->
<!-- <version>${servlet.version}</version> -->
<!-- privided依赖范围,编译时有效,但是运行发布的时候是无效的。 -->
<scope>provided</scope>
</dependency> <!-- 依赖spring框架 -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
</dependencies> <!-- JDK编译插件,改变JDK编译环境 -->
<build>
<plugins>
<!-- 1.java编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin> <!-- 2.配置打包项目源码包的插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<!-- 完成绑定.执行完打包后执行 -->
<executions>
<execution>
<goals>
<goal>jar-no-fork</goal>
</goals>
<phase>verify</phase>
</execution>
</executions>
</plugin> <!-- 3.tomcat7插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8080</port>
<server>tomcat7</server>
</configuration>
</plugin>
</plugins>
</build>
</project>

说明:在子工程中引入父工程的内容时,会有重复内容,会有黄色感叹号提示,我们只需将子工程的重复内容删除即可。

注意:

relativePath:父项目的 pom.xml 文件的相对路径。默认值为../pom.xml。maven首先从当前构建项目开始查找父项目的 pom 文件,然后从本地仓库,最后从远程仓库。RelativePath 允许你选择一个不同的位置;一般Eclipse 找不到 parent 项目时可以先update project,还不行则可配置此项。
如果所有子工程都需要依赖某些包,父工程可以通过设置依赖,将依赖关系传递到子工程中。
<dependencies>
//添加公共依赖包
</dependencies>

【注意】dependencyManagement 与 dependencies 的区别