pom.xml中使用“import”的scope来解决Maven项目单继承问题
测试环境 maven 3.3.9
想必大家在做SpringBoot应用的时候,都会有如下代码:
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>1.3.3.RELEASE</version>
- </parent>
继承一个父模块,然后再引入相应的依赖
假如说,我不想继承,或者我想继承多个,怎么做?
我们知道Maven的继承和Java的继承一样,是无法实现多重继承的,如果10个、20个甚至更多模块继承自同一个模块,那么按照我们之前的做法,这个父模块的dependencyManagement会包含大量的依赖。如果你想把这些依赖分类以更清晰的管理,那就不可能了,import scope依赖能解决这个问题。你可以把dependencyManagement放到单独的专门用来管理依赖的pom中,然后在需要使用依赖的模块中通过import scope依赖,就可以引入dependencyManagement。例如可以写这样一个用于依赖管理的pom:
- <project>
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.test.sample</groupId>
- <artifactId>base-parent1</artifactId>
- <packaging>pom</packaging>
- <version>1.0.0-SNAPSHOT</version>
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactid>junit</artifactId>
- <version>4.8.2</version>
- </dependency>
- <dependency>
- <groupId>log4j</groupId>
- <artifactid>log4j</artifactId>
- <version>1.2.16</version>
- </dependency>
- </dependencies>
- </dependencyManagement>
- </project>
然后我就可以通过非继承的方式来引入这段依赖管理配置
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>com.test.sample</groupId>
- <artifactid>base-parent1</artifactId>
- <version>1.0.0-SNAPSHOT</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
- <dependency>
- <groupId>junit</groupId>
- <artifactid>junit</artifactId>
- </dependency>
- <dependency>
- <groupId>log4j</groupId>
- <artifactid>log4j</artifactId>
- </dependency>
注意:import scope只能用在dependencyManagement里面
这样,父模块的pom就会非常干净,由专门的packaging为pom来管理依赖,也契合的面向对象设计中的单一职责原则。此外,我们还能够创建多个这样的依赖管理pom,以更细化的方式管理依赖。这种做法与面向对象设计中使用组合而非继承也有点相似的味道。
那么,如何用这个方法来解决SpringBoot的那个继承问题呢?
配置如下:
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-dependencies</artifactId>
- <version>1.3.3.RELEASE</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- </dependencies>
这样配置的话,自己的项目里面就不需要继承SpringBoot的module了,而可以继承自己项目的module了。
转载地址:https://blog.csdn.net/mn960mn/article/details/50894022
pom.xml中使用“import”的scope来解决Maven项目单继承问题的更多相关文章
- Maven pom.xml中的元素modules、parent、properties以及import
前言 项目中用到了maven,而且用到的内容不像利用maven/eclipse搭建ssm(spring+spring mvc+mybatis)用的那么简单:maven的核心是pom.xml,那么我就它 ...
- Maven pom.xml中的元素modules、parent、properties以及import(转)
前言 项目中用到了maven,而且用到的内容不像利用maven/eclipse搭建ssm(spring+spring mvc+mybatis)用的那么简单:maven的核心是pom.xml,那么我就它 ...
- (转)如何在maven的pom.xml中添加本地jar包
1 maven本地仓库认识 maven本地仓库中的jar目录一般分为三层:图中的1 2 3分别如下所示: 1 groupId 2 artifactId 3 version 4 jar包的依赖 如果要将 ...
- (转)如何在maven的pom.xml中添加本地jar包
转载自: https://www.cnblogs.com/lixuwu/p/5855031.html 1 maven本地仓库认识 maven本地仓库中的jar目录一般分为三层:图中的1 2 3分别如下 ...
- eclipse中基于maven构建的web项目pom.xml中指定的jar包无法发布到tomcat中
eclipse运行maven web项目报错: 信息: Starting Servlet Engine: Apache Tomcat/7.0.57 一月 07, 2015 11:50:44 下午 or ...
- 只要项目是maven构建的,pom.xml中依赖的jar包全都默认去你电脑本地仓库去找
只要项目是maven构建的,pom.xml中依赖的jar包全都默认去你电脑本地仓库去找
- 在maven pom.xml中加载不同的properties ,如localhost 和 dev master等jdbc.properties 中的链接不一样
[参考]:maven pom.xml加载不同properties配置[转] 首先 看看效果: 点开我们项目中的Maven projects 后,会发现右侧 我们profile有个可勾选选项.默认勾选l ...
- pom.xml中添加远程仓库
maven的pom.xml中添加远程仓库 <repositories> <repository> <id>mvnrepository</id> < ...
- maven 在pom.xml 中指定仓库位置
...... 在pom.xml 中添加 仓库位置(这样遇到私服没有的依赖,就会去这下载) </properties> <repositories><!-- 代码库 --& ...
随机推荐
- HTTP漫谈
一.说明 1.1 当前背景说明 很多web的书包括web安全的书都会有一章介绍http协议,我就总恶意揣测作者是在凑字数,一般都直接跳过去. 相比TCP/IP这种各字段基于数值代号的协议,http这种 ...
- [转帖]Linux chattr 命令详解
Linux chattr 命令详解 https://www.cnblogs.com/ftl1012/p/chattr.html 常见命令参数 1 2 3 4 5 6 7 8 9 10 11 12 A: ...
- 第七节:Asp.Net Core内置日志和整合NLog(未完)
一. Asp.Net Core内置日志 1. 默认支持三种输出方式:控制台.调试(底部输出窗口).EventSource,当然也可以在Program类中通过logging.ClearProviders ...
- spring扩展点之五:ApplicationContextInitializer实现与使用
ApplicationContextInitializer是Spring框架原有的东西,这个类的主要作用就是在ConfigurableApplicationContext类型(或者子类型)的Appli ...
- Debian Stretch升级当前最新稳定版内核
Why update kernel ? Update the kernel to new version fixed some newer hardware has no driver softwar ...
- Django中一些常用的文档段落
1. Settings¶ STATIC_URL¶ MEDIA_ROOT¶ MEDIA_URL¶ AUTH_USER_MODEL¶ USE_I18N¶ USE_L10N¶ USE_TZ¶ (三者默 ...
- Redis 获取和设置密码
1.config get reuqirepass //获取当前密码 2.config set requirepass "password"//设置当前密码,双引号里面为密码
- C# ThreadStart中如何带参数
1.ThreadStart 线程执行带参数的方法,new Thread(new ThreadStart(delegate { ThreadTask(firstPage, lastPage); })); ...
- drf之接口规范
web接口 # 请求工具:postman => https://www.getpostman.com/ # 接口:url链接,通过向链接发生不同的类型请求与数据得到相应的响应数据 # http: ...
- 【Java】调用摄像头进行拍照并保存【详细】以及处理no jniopencv_core in java.library.path的一种方法
[之前困扰笔者的问题描述] date:2019.12.18 网上教程很多,但是没有看见完整的,所以写一个出来. 调用摄像头需要javaCV的jar包和openCV的jar包,现在已经不需要安装包了 ...