Maven项目继承实现过程图解

网友投稿 229 2023-03-28

Maven项目继承实现过程图解

多个maven项目之间难免有重复的pom配置,重复的配置没必要重复写,maven提供了父子继承的关系,重复的依赖直接放在父项目的pom中。

所以不希望每个开发者随意定义maven版本依赖,可以在父项目中进行说明,然后子项目沿用即可。

idea创建父项目(这是一个父项目,也是一个空项目,只需要pom.xml,编写相关的依赖, 父项目必须用pom打包的方式):

编辑父项目pom.xml

xmlns:xsi="http://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">

4.0.0

com.linewell

maven-parent

1.0-SNAPSHOT

pom

2.6

4.3.6.RELEASE

commons-io

commons-io

${common.version}

spring-context-support

org.springframework

${spring.version}

xmlns:xsi="http://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">

4.0.0

com.linewell

maven-parent

1.0-SNAPSHOT

pom

2.6

4.3.6.RELEASE

commons-io

commons-io

${common.version}

spring-context-support

org.springframework

${spring.version}

这边需要说明下,dependencyManagement,这边的依赖不会被继承,如果子项目导入了这个依赖,可以不用写版本号,会以父项目的为主,因为有的子项目不一定会用父项目中的所有依赖。个别子项目依赖到的包可以放在这里,然后不需要写版本号,会自动引用父项目。

创建一个子项目,编辑子项目的pom.xml如下:

xmlns:xsi="http://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">

4.0.0

com.linewell

maven-children

1.0-SNAPSHOT

com.linewell

maven-parent

1.0-SNAPSHOT

../mavenparent/pom.xml

xmlns:xsi="http://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">

4.0.0

com.linewell

maven-children

1.0-SNAPSHOT

com.linewell

maven-parent

1.0-SNAPSHOT

../mavenparent/pom.xml

可以看到commons-io进来了,spring-context-support没进来。

我现在不添加spring-context-support的版本,然后看下结果,是会以父项目的版本为主。可以看到如下引入的也是父项目中的4.3.6

xmlns:xsi="http://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">

4.0.0

com.linewell

maven-parent

1.0-SNAPSHOT

../mavenparent/pom.xml

com.linewell

maven-children

1.0-SNAPSHOT

org.springframework

spring-context-support

xmlns:xsi="http://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">

4.0.0

com.linewell

maven-parent

1.0-SNAPSHOT

../mavenparent/pom.xml

com.linewell

maven-children

1.0-SNAPSHOT

org.springframework

spring-context-support

那么问题来了,如果子项目指定了版本会怎么样?

编辑子项目pom.xml, 如下可以发现,如果子项目有明确指定依赖以及具体版本,与父项目发生冲突会以子项目的依赖为准。

xmlns:xsi="http://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">

4.0.0

com.linewell

maven-parent

1.0-SNAPSHOT

../mavenparent/pom.xml

com.linewell

maven-children

1.0-SNAPSHOT

commons-io

commons-io

2.5

org.springframework

spring-context-support

5.2.5.RELEASE

xmlns:xsi="http://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">

4.0.0

com.linewell

maven-parent

1.0-SNAPSHOT

../mavenparent/pom.xml

com.linewell

maven-children

1.0-SNAPSHOT

commons-io

commons-io

2.5

org.springframework

spring-context-support

5.2.5.RELEASE

ps:如果父项目中执行了mvn install安装到了本地仓库,然后子项目中引入父GAV的时候可以不用写路径relativePath属性。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:统一数据开发平台包括(统一数据开发平台包括哪些)
下一篇:网红开发数据平台是什么(网红开发数据平台是什么软件)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~